0

I would like to do this in Magento 1.9. Can it be done?

1

You need to hook an event. You could do this as follows. Add the event to your module's config.xml file (app/code/local/Lpf/ModuleCookie/etc/config.xml):

<?xml version="1.0"?>
<config>
    <modules>
        <Lpf_ModuleCookie>
            <version>0.1</version>
        </Lpf_ModuleCookie>
    </modules>
    <global>
        <models>
            <lpf_modulecookie>
                <class>Lpf_ModuleCookie_Model</class>
            </lpf_modulecookie>
        </models>
    </global>
    <frontend>
        <events>
            <customer_login>
                <observers>
                    <lpf_modulecookie_customer_login>
                        <type>model</type>
                        <class>lpf_modulecookie/observer</class>
                        <method>customerLogin</method>
                    </lpf_modulecookie_customer_login>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

Now create a model Lpf_ModuleCookie_Model_Observer (app/code/local/Lpf/ModuleCookie/Model/Observer.php). Add a customerLogin() method to the class:

<?  

    class Lpf_ModuleCookie_Model_Observer
{

     /**
      * Run couple of 'php' codes after customer logs in
      *
      * @param Varien_Event_Observer $observer
      */
     public function customerLogin($observer)
     {
     $event = $observer->getEvent();
    $cart = Mage::helper('checkout/cart')->getCart();
    $items = $cart->getItems();
    $itemCount = count($items);
    if($itemCount > 1)
    {
        $i = 0;
        foreach($items as $item)
        {
            $i++;
            if($i == $itemCount)
            {
                if($item->getProduct()->getId() == $productId)
                {
                    $itemId = $item->getItemId();
                    $cart->removeItem($itemId)->save();
                }
            }
        }
    }
    else
    {
        foreach($items as $item)
        {
            if($item->getProduct()->getId() == $productId)
            {
                try{
                    $quote = Mage::getSingleton('checkout/session')->getQuote();
                    $quote->removeAllItems();
                    $quote->save();
                }catch (Exception $e)
                {
                    Mage::log('Failed to remove item from cart'.$e.'.');
                }
            }
        }
    }


}
1

To do so, you need to create a little module:

app/code/local/DigitalPianism/Emptycart/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <DigitalPianism_Emptycart>
            <version>0.0.1</version>
        </DigitalPianism_Emptycart>
    </modules>
    <global>
        <models>
            <class>DigitalPianism_Emptycart_Model</class>
        </models>
    </global>
    <frontend>
        <events>
            <customer_login>
                <observer>
                    <digitalpianism_emptycart_customer_login>
                        <type>model</type>
                        <class>emptycart/observer</class>
                        <method>emptyCustomerCart</method>
                    </digitalpianism_emptycart_customer_login>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

Then in app/code/local/DigitalPianism/Emptycart/Model/Observer.php

<?php

class DigitalPianism_Emptycart_Model_Observer
{
    public function emptyCustomerCart(Varien_Event_Observer $observer)
    {
        Mage::getSingleton('checkout/cart')->truncate();
        Mage::getSingleton('checkout/cart')->save();
    }
}

Don't forget to add the Mage_Checkout dependency in the app/etc/modules/DigitalPianism_Emptycart.xml so the quote cleaning is done after the customer quote is loaded:

<?xml version="1.0"?>
<config>
    <modules>
        <DigitalPianism_Emptycart>
            <active>true</active>
            <codePool>local</codePool>
            <depends><Mage_Checkout/></depends>
        </DigitalPianism_Emptycart>
    </modules>  
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.