获取特定产品所涉及的产品

时间:2012-10-26 08:23:01

标签: magento magento-1.7

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

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

感谢您的建议。

2 个答案:

答案 0 :(得分:1)

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

  1. 由于产品似乎紧密相关并且依赖于主要产品,因此您可能需要考虑创建bundle products。对于在购物车中有3个商品并删除其中一个商品(主要产品)的顾客来说也可能会造成混淆,现在他们的购物车是空的,由于某些未知原因,他们仍然需要2个相关产品(您自动删除)。

  2. 如上所述 - 关于购物车中所有产品的删除循环(请参阅下面的代码)

  3. 在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(); 
             }
    
        }
    }
    
    ?>
    

    了解更多@

    Observer for removed items in the cart

    Help with Magento and related products

    Magento - How to check if a product has already been removed from the cart

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

答案 1 :(得分:1)

如果我是你,我可能不会首先添加产品,而是使用观察者来检查checkout_cart_product_add_after事件。 根据您的预期产品检查产品。使用R.S.的方法。了解报价并检查购物车中产品的数量。 为产品添加一个自定义选项,让客户知道他是否有他的免费赠品。 这可能是合适的Magento - Quote/order product item attribute based on user input

然后为此事件sales_convert_quote_to_order添加一个观察者。 这个观察者可以检查数量并调整这样的东西,以便将产品交给客户Skip Checkout in Magento for a downloadable product 你唯一获得单身人士和使用观察员,所以这种方法比购物车过程中的大量添加和删除要便宜得多。它也会看起来更好。

如果你想要我尝试实现它,但是使用你的网站和数据库的副本,因为我懒得设置产品。

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

PS。 PS。也许我应该检查之前我评论大声笑。大声笑你会被禁止在这里吗?

相关问题