2

我需要获取相关产品中列出指定产品的所有产品。因此,在删除主要产品后,我会尝试从购物车中删除不必要的物品。

有没有更简单的方法来循环所有购物车项目?

谢谢你的建议。

4

2 回答 2

1

我能想到的唯一两种方法是:

  1. 由于产品似乎与主产品密切相关并依赖于主产品,您可能需要考虑创建捆绑产品。对于在购物车中有 3 件商品并删除其中 1 件(主要产品)的客户也可能会感到困惑,现在他们的购物车是空的,出于某种未知原因,他们仍然想要 2 件相关产品(您自动删除)。

  2. 正如您上面提到的 - 在您的购物车中的所有产品中删除循环(请参见下面的代码)

在 app/local/RWS/AutoDeleteRelatedCartProducts/etc/config.xml 中创建

<config>
    <global>
        <models>
            <autodeleterelatedcartproducts>
                 <class>RWS_AutoDeleteRelatedCartProducts_Model</class>
            </autodeleterelatedcartproducts>
         </models>
    </global>
    <frontend>
      <events>
        <sales_quote_remove_item>
            <observers>
                <autodeleterelatedcartproducts>
                    <type>singleton</type>
                    <class>autodeleterelatedcartproducts/observer</class>
                    <method>removeQuoteItem</method>
                </autodeleterelatedcartproducts>
            </observers>
        </sales_quote_remove_item>
      </events>
    </frontend>
</config>

在 app/local/RWS/AutoDeleteRelatedCartProducts/Model/Observer.php 中创建

<?php

class RWS_AutoDeleteRelatedCartProducts_Model_Observer
{
    public function removeQuoteItem(Varien_Event_Observer $observer)
    {
        //get deleted product
        $delete_product = $observer->getQuoteItem()->getProduct();

        // Get all related products
        $related_products = $delete_product->getRelatedProductCollection();

        // get all related product id and save in array
        $related_product_ids = array();
        foreach($related_products as $product){
            $related_product_ids[] = $product->getId(); // double check to make sure this product_id 
        }


         foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ) 
         { 
              // if getId is a related product remove it
              if(in_array($item->getId(), $related_product_ids))
                    Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save(); 
         }

    }
}

?>

阅读更多 @

观察购物车中已移除的物品

帮助 Magento 和相关产品

Magento - 如何检查产品是否已从购物车中删除

http://www.magentocommerce.com/boards/viewthread/30113/

于 2012-10-28T13:08:19.923 回答
1

如果我是你,我可能不会首先添加产品,而是使用观察者来查看 checkout_cart_product_add_after 事件。根据您的预期产品检查产品。使用 RS 的方法来获取报价并检查您购物车中产品的数量。为产品添加一个自定义选项,让客户知道他是否有免费的好东西。这可能是合适的 Magento - 基于用户输入的报价/订单产品项目属性

然后将观察者添加到此事件 sales_convert_quote_to_order。这个观察者可以检查数量并调整类似的东西,将产品提供给客户在 Magento 中跳过结帐以获取可下载的产品, 您只需获得单身人士并使用观察者,因此这种方法比在购物车过程中添加和删除的成本要低得多. 它也会看起来好多了。

如果您愿意,我会尝试实施它,但使用您的站点和数据库的副本,因为我懒得设置产品。

附言。也许你也需要观察这个事件 checkout_cart_update_items_before

附言。附言。也许我应该在评论之前检查一下,哈哈。大声笑让你从这里被禁止吗?

于 2012-10-29T22:58:46.690 回答