【问题标题】:Add custom options while adding grouped product to cart在将分组产品添加到购物车时添加自定义选项
【发布时间】:2015-01-12 15:29:28
【问题描述】:

将分组产品添加到购物车时遇到问题。

在将分组产品添加到购物车时,我需要为添加到购物车的所有产品设置自定义选项。

我最后尝试的(有点成功):

<checkout_cart_product_add_after>
    <observers>
        <customoptions>
            <type>singleton</type>
            <class>Company_CustomOptions_Model_Observer</class>
            <method>addCustomOptionGroupSku</method>
        </customoptions>
    </observers>
</checkout_cart_product_add_after>

public function addCustomOptionGroupSku(Varien_Event_Observer $observer) {
    $product = $observer->getProduct ();
    if ($product->isGrouped ()) { 
        $quoteItem = $observer->getQuoteItem ();
        $additionalOptions = array (
                'options' => array (
                        'label' => 'GROUPSKU',
                        'value' => $product->getSku () 
                ) 
        );
        $quoteItem->addOption ( new Varien_Object ( array (
                'product' => $quoteItem->getProduct (),
                'code' => 'additional_options',
                'value' => serialize ( $additionalOptions ) 
        ) ) );
    }
}

我创建了一个包含两个产品的分组产品。 但该代码仅将自定义选项“GROUPSKU”添加到购物车中的一项。另一个没有动过。

如何获取所有即将添加到购物车的 QuoteItems?

PS:我也将此问题添加到 StackExchange 的 Magento 部分:https://magento.stackexchange.com/questions/51883/add-custom-options-while-adding-grouped-product-to-cart

【问题讨论】:

    标签: magento magento-1.8


    【解决方案1】:

    我找到了一个不需要观察者的解决方案。

    我不得不重写Mage_Sales_Model_Quote。更具体的方法addProductAdvanced()

    坏消息:你不能简单地使用parent::addProductAdvanced() 然后做你自己的事情。您必须复制原始代码并用您的代码填写。

    这就是我所做的(注意以/******** 开头的评论):

    public function addProductAdvanced(Mage_Catalog_Model_Product $product, $request = null, $processMode = null) {
        if ($request === null) {
            $request = 1;
        }
        if (is_numeric ( $request )) {
            $request = new Varien_Object ( array (
                    'qty' => $request 
            ) );
        }
        if (! ($request instanceof Varien_Object)) {
            Mage::throwException ( Mage::helper ( 'sales' )->__ ( 'Invalid request for adding product to quote.' ) );
        }
    
        $cartCandidates = $product->getTypeInstance ( true )->prepareForCartAdvanced ( $request, $product, $processMode );
    
        /**
         * Error message
         */
        if (is_string ( $cartCandidates )) {
            return $cartCandidates;
        }
    
        /**
         * If prepare process return one object
         */
        if (! is_array ( $cartCandidates )) {
            $cartCandidates = array (
                    $cartCandidates 
            );
        }
    
        $parentItem = null;
        $errors = array ();
        $items = array ();
        foreach ( $cartCandidates as $candidate ) {
            // Child items can be sticked together only within their parent
            $stickWithinParent = $candidate->getParentProductId () ? $parentItem : null;
            $candidate->setStickWithinParent ( $stickWithinParent );
            $item = $this->_addCatalogProduct ( $candidate, $candidate->getCartQty () );
    
            /******** own modification for custom option GROUPSKU*/
            if($product->isGrouped()){
                $additionalOptions = array (
                        'options' => array (
                                'label' => 'GROUPSKU',
                                'value' => $product->getSku ()
                        )
                );
                $item->addOption ( new Varien_Object ( array (
                        'product' => $item->getProduct (),
                        'code' => 'additional_options',
                        'value' => serialize ( $additionalOptions )
                ) ) );
            }
            /******** own modification end*/
    
            if ($request->getResetCount () && ! $stickWithinParent && $item->getId () === $request->getId ()) {
                $item->setData ( 'qty', 0 );
            }
            $items [] = $item;
    
            /**
             * As parent item we should always use the item of first added product
             */
            if (! $parentItem) {
                $parentItem = $item;
            }
            if ($parentItem && $candidate->getParentProductId ()) {
                $item->setParentItem ( $parentItem );
            }
    
            /**
             * We specify qty after we know about parent (for stock)
             */
            $item->addQty ( $candidate->getCartQty () );
    
            // collect errors instead of throwing first one
            if ($item->getHasError ()) {
                $message = $item->getMessage ();
                if (! in_array ( $message, $errors )) { // filter duplicate messages
                    $errors [] = $message;
                }
            }
        }
        if (! empty ( $errors )) {
            Mage::throwException ( implode ( "\n", $errors ) );
        }
    
        Mage::dispatchEvent ( 'sales_quote_product_add_after', array (
                'items' => $items 
        ) );
    
        return $item;
    }
    

    这样,使用分组产品添加到购物车的所有产品现在都具有自定义选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 2012-06-09
      相关资源
      最近更新 更多