Add a custom option to a quote item (product in the cart)?

后端 未结 1 1423
梦如初夏
梦如初夏 2021-02-20 15:35

I\'m running Magento 1.7.

I\'m trying to add a quote item option, a custom one, in sales_flat_quote_item_option.

I have tried with addOption and addCustomOption

相关标签:
1条回答
  • 2021-02-20 16:13

    Yes this is possible, you need to use the observer

    I have been adding Delivery date with each product in the orders

    So you can change this to be the Option you want to add to each product or so.

             <controller_action_predispatch_checkout>
                <observers>
                    <options_observer>
                        <class>YOUR_CLASS_NAME</class>
                        <method>setProductInfo</method>
                    </options_observer>
                </observers>
            </controller_action_predispatch_checkout>
    

    public function setProductInfo($observer)
    {
        if ('checkout_cart_add' != $observer->getEvent()->getControllerAction()->getFullActionName()) {
            return;
        }
        $request = Mage::app()->getRequest();
        $prId = $request->getParams();
        $product = Mage::getModel('catalog/product')->load($prId['product']);
        // fixed spelling of cofigurable/configurable
        if ($product->getTypeId() == 'configurable') {
            return $this;
        }
    
        if (!$product->getHasOptions()) {
            $optionID = $this->saveProductOption($product);
        } else {
            $options = $product->getOptions();
            if ($options) {
                foreach ($options as $option) {
                    if ($option->getTitle() == 'Delivery Date') {
                        $optionID = $option->getOptionId();
                    }
                }
            }
            if (empty($optionID)) {
                $optionID = $this->saveProductOption($product);
            }
        }
    
        $deliveryDate = $prId['delivery_date'];
        if (!empty($deliveryDate)) {
            $opt['options'] = array($optionID => $deliveryDate);
            $request->setParams($opt);
        }
    
        return $this;
    }
    
    function saveProductOption($product)
    {
    
        $store = Mage::app()->getStore()->getId();
        $opt = Mage::getModel('catalog/product_option');
        $opt->setProduct($product);
        $option = array(
            'is_delete' => 0,
            'is_require' => false,
            'previous_group' => 'text',
            'title' => 'Delivery Date',
            'type' => 'field',
            'price_type' => 'fixed',
            'price' => '0.0000'
        );
        $opt->addOption($option);
        $opt->saveOptions();
        Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
        $product->setHasOptions(1);
        $product->save();
    
        $options = $product->getOptions();
        if ($options) {
            foreach ($options as $option) {
                if ($option->getTitle() == 'Delivery Date') {
                    $optionID = $option->getOptionId();
                }
            }
        }
        Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($store));
        return $optionID;
    }
    
    0 讨论(0)
提交回复
热议问题