Magento 2 : How to clear mini cart programatically
Somehow I found solution.
In phtml file I have added customer-data js method
require([
'jquery',
'Magento_Customer/js/customer-data'
], function($,customerData){
$('#customerStore').change(function(){
var store_id = $(this).val();
var sections = ['cart'];
$.ajax({
url:'/customer/data/store/store_id/'+store_id,
type:'get',
dataType: 'html',
beforeSend:function(){
$('#loader').show();
},
success:function(response){
customerData.invalidate(sections);
$('#loader').hide();
location.reload();
}
});
});
});
In my controller file Store.php I have deleted quote item from table
public function deleteQuoteItems(){
$checkoutSession = $this->getCheckoutSession();
//$quote = $this->$checkoutSession->getQuote();
$quote_Id= $this->cart->getQuote()->getId();
//print_r($checkoutSession->getQuote()->getData());
$allItems = $checkoutSession->getQuote()->getAllVisibleItems();//returns all teh items in session
foreach ($allItems as $item) {
$itemId = $item->getItemId();//item id of particular item
$quoteItem=$this->getItemModel()->load($itemId);//load particular item which you want to delete by his item id
$quoteItem->delete();
}
if(!empty($quote_Id)){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// $quoteModel = $objectManager->create('Magento\Quote\Model\Quote');
// $quoteModel->delete($quote_Id);
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('quote');
$sql = "DELETE FROM " . $tableName." WHERE entity_id = ".$quote_Id;
$connection->query($sql);
}
}
Updated sections.xml. Remove vendor from action name
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
<action name="module/data/store">
<section name="cart"/>
</action>
</config>