Matheus Portela
Matheus Portela

Reputation: 155

Magento - removing products from the cart

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

Answers (1)

Bachcha Singh
Bachcha Singh

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 :

  1. 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>
    
  2. 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>
    
  3. 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

Related Questions