Magento在登录时从购物车中删除商品

时间:2015-10-22 13:44:39

标签: magento magento-1.9

我希望在退出用户后清除或删除购物车中的所有商品。 我找到了这个,但我不知道我要把它放在哪里

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
 Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}

3 个答案:

答案 0 :(得分:0)

您需要为“customer_logout”事件创建一个观察者。

Answer here describe how to create observer

您只需使用“customer_logout”而不是“catalog_product_save_before”。

答案 1 :(得分:0)

现在可行了

应用程序的/ etc /模块/ Amir_Meta.xml

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

应用程序/代码/本地/阿米尔/元的/ etc / config.xml中

    <?xml version="1.0"?>
<config>
    <modules>
        <Amir_Meta>
            <version>0.0.1</version>
        </Amir_Meta>
    </modules>
    <global>
        <models>
            <amir_meta>
                <class>Amir_Meta/Observer</class>
            </amir_meta>
        </models>
    </global>
    <adminhtml>
        <events>
            <customer_logout><!-- event observer -->
                <observers>
                    <amir_meta>
                        <class>amir_meta/observer</class>
                        <method>Clean_cart</method>
                    </amir_meta>
                </observers>
            </customer_logout>
        </events>
    </adminhtml>
</config>

应用程序/代码/本地/阿米尔/元/型号/ Observer.php

     <?php

class Amir_Meta_Model_Observer {

public function Clean_cart(Varient_Event_Observer $observer) {

        foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
        Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();

        }

    }

}

答案 2 :(得分:0)

以下代码对我来说很好:

在/app/etc/modules/Mycompany_Cleancart.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Mycompany_Cleancart>
      <active>true</active>
      <codePool>local</codePool>
      <depends>
            <Mage_Catalog />
      </depends>
      <version>0.1.0</version>
    </Mycompany_Cleancart>
  </modules>
</config>

在/app/code/local/Mycompany/Cleancart/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Mycompany_Cleancart>
            <version>0.1.0</version>
        </Mycompany_Cleancart>
    </modules>
    <global>
        <models>
            <mycompany_cleancart>
                <class>Mycompany_Cleancart_Model</class>
            </mycompany_cleancart>
        </models>
    </global>
    <frontend>
        <events>
            <customer_logout><!-- event observer -->
                <observers>
                    <Mycompany_Cleancart>
                        <class>mycompany_cleancart/observer</class>
                        <method>cleancart</method>
                    </Mycompany_Cleancart>
                </observers>
            </customer_logout>
        </events>
    </frontend>
</config>

在/app/code/local/Mycompany/Cleancart/Model/Observer.php

<?php

class Mycompany_Cleancart_Model_Observer {

    public function cleancart(Varien_Event_Observer $observer){
        foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
            Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();

        }
    }
}