Magento 在将自定义选项添加到购物车之前更改自定义选项值 [英] Magento change Custom Option value before adding it to cart

查看:21
本文介绍了Magento 在将自定义选项添加到购物车之前更改自定义选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Magento 中为我的产品设置了一个自定义选项作为下拉菜单,即

I have setup a custom option for my product in Magento as dropdown i.e.

尺寸:小、中、大

在产品页面上,我使用 javascript 显示每个选项的附加信息.

On product page I show additional information for each option using javascript.

小 - 腰围 30,胸围 36,身长 42...
中等 - 腰围 32,胸围 38,长度 44...
大号 - 腰围 34,胸围 40,长度 48...

Small - Waist 30, Chest 36, Length 42...
Medium - Waist 32, Chest 38, Length 44...
Large - Waist 34, Chest 40, Length 48...

当我将产品添加到购物车时,我会在购物车中获得尺码标题(小、中或大),但我还想显示此附加信息(腰围 30、胸围 36、长度 42...)并将其保存为命令.

When I add product to cart i get the Size title (Small, Medium or Large) in cart but I also want to show this additional information (Waist 30, Chest 36, Length 42...) and get it saved with order.

最好的方法是什么?提前致谢.

What is best way to do it? Thanks in advance.

推荐答案

自定义选项仅作为选项 ID 和值存储在报价中.每次渲染选项时,它们基本上都是从数据库中重新加载.
如果修改这些值,则需要保存它们,这样才能为每个人设置它们.

The custom options are only stored on the quote as option ID's and values. Every time the options are rendered, they are basically reloaded from the database.
If you modify the values, you would need to save them, and that would set them for everybody.

也就是说,我通过使用事件观察器动态添加具有修改值的附加自定义选项来解决该问题.为此,我使用了其他选项.
然后我从报价项中删除原始自定义选项.

That said, I work around the issue by adding an additional custom option with the modified value on the fly, using an event observer. For this I use additional options.
Then I remove the original custom option from the quote item.

直到 1.4 Magento 负责其余的工作,但从那时起您需要手动将附加选项复制到订单项中,并且还需要注意在重新订购项时再次设置它.

Up to 1.4 Magento took care of the rest, but since then you need to copy the additional options to the order item manually, and also need to take care it's set again if an item is reordered.

这里是一个示例观察者配置.

So here is an example observer configuration.

<frontend>
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <customoptions>
                    <type>singleton</type>
                    <class>customoptions/observer</class>
                    <method>checkoutCartProductAddAfter</method>
                </customoptions>
            </observers>
        </checkout_cart_product_add_after>
        <sales_convert_quote_item_to_order_item>
            <observers>
                <customoptions>
                    <type>singleton</type>
                    <class>customoptions/observer</class>
                    <method>salesConvertQuoteItemToOrderItem</method>
                </customoptions>
            </observers>
        </sales_convert_quote_item_to_order_item>
    </events>
</frontend>

其余的在观察者类中处理.

The rest is handled in the observer class.

/**
 * Add additional options to order item product options (this is missing in the core)
 *
 * @param Varien_Event_Observer $observer
 */
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
        $orderItem = $observer->getOrderItem();
        $options = $orderItem->getProductOptions();
        $options['additional_options'] = unserialize($additionalOptions->getValue());
        $orderItem->setProductOptions($options);
    }
}

/**
 * Manipulate the custom product options
 *
 * @param Varien_Event_Observer $observer
 * @return void
 */
public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
    $item = $observer->getQuoteItem();
    $infoArr = array();

    if ($info = $item->getProduct()->getCustomOption('info_buyRequest')) {
        $infoArr = unserialize($info->getValue());
    }

    // Set additional options in case of a reorder
    if ($infoArr && isset($infoArr['additional_options'])) {
        // An additional options array is set on the buy request - this is a reorder
        $item->addOption(array(
            'code' => 'additional_options',
            'value' => serialize($infoArr['additional_options'])
        ));
        return;
    }

    $options = Mage::helper('catalog/product_configuration')->getCustomOptions($item);

    foreach ($options as $option)
    {
        // The only way to identify a custom option without
        // hardcoding ID's is the label :-(
        // But manipulating options this way is hackish anyway
        if ('Size' === $option['label'])
        {
            $optId = $option['option_id'];

            // Add replacement custom option with modified value
            $additionalOptions = array(array(
                'code' => 'my_code',
                'label' => $option['label'],
                'value' => $option['value'] . ' YOUR EXTRA TEXT',
                'print_value' => $option['print_value'] . ' YOUR EXTRA TEXT',
            ));
            $item->addOption(array(
                'code' => 'additional_options',
                'value' => serialize($additionalOptions),
            ));

            // Update info_buyRequest to reflect changes
            if ($infoArr &&
                isset($infoArr['options']) &&
                isset($infoArr['options'][$optId]))
               {
                // Remove real custom option
                unset($infoArr['options'][$optId]);

                // Add replacement additional option for reorder (see above)
                $infoArr['additional_options'] = $additionalOptions;

                $info->setValue(serialize($infoArr));
                $item->addOption($info);
            }

            // Remove real custom option id from option_ids list
            if ($optionIdsOption = $item->getProduct()->getCustomOption('option_ids')) {
                $optionIds = explode(',', $optionIdsOption->getValue());
                if (false !== ($idx = array_search($optId, $optionIds))) {
                    unset($optionIds[$idx]);
                    $optionIdsOption->setValue(implode(',', $optionIds));
                    $item->addOption($optionIdsOption);
                }
            }

            // Remove real custom option
            $item->removeOption('option_' . $optId);
        }
    }

简而言之就是这样.添加错误检查并处理特殊情况,例如根据需要再次将相同产品添加到购物车.
希望这能让您开始使用自定义产品选项.熟悉它们后,还不错.

This is it in a nutshell. Add error checking and taking care of special cases like adding the same product to the cart again as needed.
Hope this gets you started working with custom product options. Not half bad once you get familiar with them.

这篇关于Magento 在将自定义选项添加到购物车之前更改自定义选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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