Reputation: 155
I need to remove cart products from a certain category (category with id = 13) when the page loads, if there is a product added to the cart from that category. I've been working on this for quite a while and researching it too, but I did not find anything that could help me to do the function to get the result I want. Thanks in advance for your help.
Upvotes: 2
Views: 1627
Reputation: 3934
You can achieve your requirement by using magento Observer event i.e checkout_cart_save_before. I have achieve this by creating following module that does not allow the user to add product into the cart of specific category.
You need to create a module for this :
Create Module registration file under app/etc/modules/Tanymart_Removecart.xml with following code :
<?xml version="1.0"?>
<config>
<modules>
<Tanymart_Removecart>
<active>true</active>
<codePool>community</codePool>
</Tanymart_Removecart>
</modules>
</config>
Now under the community codepool, create config.xml file. File Path is app/code/community/Tanymart/Removecart/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Tanymart_Removecart>
<version>0.1.0</version>
</Tanymart_Removecart>
</modules>
<global>
<models>
<tanyremovecart>
<class>Tanymart_Removecart_Model</class>
</tanyremovecart>
</models>
<events>
<checkout_cart_save_before>
<observers>
<remove_category_item>
<class>Tanymart_Removecart_Model_Observer</class>
<method>removeCategoryCart</method>
</remove_category_item>
</observers>
</checkout_cart_save_before>
</events>
</global>
<frontend>
<routers>
<tanyremovecart>
<use>standard</use>
<args>
<module>Tanymart_Removecart</module>
<frontName>tanyremovecart</frontName>
</args>
</tanyremovecart>
</routers>
</frontend>
</config>
Now create Observer.php inside Model directory. File Path app/code/community/Tanymart/Removecart/Model/Observer.php with following code :
<?php
/**
* @category Tanymart
* @package Tanymart_Removecart
* @author Bachcha Singh
* @copyright Copyright (c) 2017
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
class Tanymart_Removecart_Model_Observer {
public function removeCategoryCart($observer){
$items = $observer->getEvent()->getCart()->getItems();
$oCheckout = Mage::getSingleton( 'checkout/session' );
$oQuote = $oCheckout->getQuote();
foreach($items as $item) {
$_product = $item->getProduct();
if(in_array(13, $_product->getCategoryIds())) {
$itemId = $item->getItemId();
$oQuote->removeItem($itemId)->save();
}
}
}
}
Replace 13 above with your specific category.
That's it. Hope it will help you.
Upvotes: 2