将自定义选项添加到报价项目(购物车中的产品)? [英] Add a custom option to a quote item (product in the cart)?

查看:68
本文介绍了将自定义选项添加到报价项目(购物车中的产品)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Magento 1.7.

I'm running Magento 1.7.

我正在尝试在sales_flat_quote_item_option中添加一个报价项选项(自定义选项).

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

我已经尝试过使用addOption和addCustomOption函数,但是似乎没有查询启动到数据库.

I have tried with addOption and addCustomOption functions, but it seems no query is launched to the database.

这是我的代码(在自定义模块帮助器中):

This is my code right now (in a custom module helper) :

public function assignDocumentToQuoteItem(Mage_Sales_Model_Quote_Item $quoteItem, $documentid)
{
    if (is_numeric($documentid) && $documentid > 0) {
        /** @var Mage_Catalog_Model_Product */
        $product = $quoteItem->getProduct();

        $quoteItem->addOption(array(
            'product_id' => $product->getId(),
            'product'    => $product,
            'code'       => 'documentid',
            'value'      => $documentid
        ));
        $quoteItem->save();
        return true;
    }

    throw new Exception(__METHOD__.' - Document id has to be a numeric value.');
}

推荐答案

是可以的,您需要使用观察器

Yes this is possible, you need to use the observer

我一直在为订单中的每种产品添加交货日期

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

因此,您可以将其更改为要添加到每个产品左右的Option.

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;
}

这篇关于将自定义选项添加到报价项目(购物车中的产品)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆