【问题标题】:Get the products where specific product is related to获取与特定产品相关的产品
【发布时间】:2012-10-26 08:23:01
【问题描述】:

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

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

感谢您的建议。

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    我能想到的只有两种方法:

    1. 由于产品似乎紧密相关并依赖于主要产品,您可能需要考虑创建bundle products。对于在购物车中有 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(); 
             }
    
        }
    }
    
    ?>
    

    阅读更多@

    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/

    【讨论】:

    • 谢谢。不幸的是,无法使用捆绑包。就我而言,如果您订购的产品是主要产品的十倍以上,您将获得一些相关产品。这应该是对客户的特别优惠。你的第二种方式是我目前的方式。还有其他想法吗?
    • 客户拿到的相关商品,是否会改变订单总金额(增加运费等)?
    • 不,它们是免费的(如果您的购物车中的主要产品超过十倍,则额外下载)
    【解决方案2】:

    如果我是你,我可能不会首先添加产品,而是使用观察者来处理 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。附言。也许我应该在评论之前检查一下,哈哈。大声笑让你从这里被禁止吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-22
      • 2014-01-03
      • 2017-03-12
      • 1970-01-01
      • 2018-08-29
      • 2018-11-09
      • 2013-08-11
      • 2015-08-28
      相关资源
      最近更新 更多