且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento - 根据用户输入报价/订购产品项目属性

更新时间:2023-11-30 10:05:52

Magento 提供了添加选项的功能,这些选项不是产品属性或产品自定义选项.它们通过选项代码 additional_options 在产品和报价项目上设置.

Magento provides a capability for adding options that aren't product attributes or product custom options. They are set on the product and quote items with the option code additional_options.

您需要执行两个步骤,每个步骤都可以通过事件观察器进行处理.如果您希望其他选项通过重新排序进行,您还需要观察第三个事件.

There are two steps you need to take, each can be handled via an event observer. If you want the additional options to carry through reordering, you will need also observe a third event.

第一步是添加事件观察器,在加载的产品被添加到购物车之前设置附加选项.一种选择是使用 catalog_product_load_after 事件.

The first step is to add the event observer to set the additional options on the loaded product before it is added to the cart. One option is to use the catalog_product_load_after event.

<catalog_product_load_after>
    <observers>
        <extra_options>
            <type>model</type>
            <class>extra_options/observer</class>
            <method>catalogProductLoadAfter</method>
        </extra_options>
    </observers>
</catalog_product_load_after>

在事件观察器中,您可以添加额外的检查请求的页面确实是添加到购物车的操作.这种观察者方法的要点是将您的特殊选项的选择添加到产品模型的 additional_options 选项中.

In the event observer you can add additional checks the requested page is indeed an add to cart action. The main point of this observer method is to add the selection of your special options to the additional_options option on the product model.

public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
    // set the additional options on the product
    $action = Mage::app()->getFrontController()->getAction();
    if ($action->getFullActionName() == 'checkout_cart_add')
    {
        // assuming you are posting your custom form values in an array called extra_options...
        if ($options = $action->getRequest()->getParam('extra_options'))
        {
            $product = $observer->getProduct();

            // add to the additional options array
            $additionalOptions = array();
            if ($additionalOption = $product->getCustomOption('additional_options'))
            {
                $additionalOptions = (array) unserialize($additionalOption->getValue());
            }
            foreach ($options as $key => $value)
            {
                $additionalOptions[] = array(
                    'label' => $key,
                    'value' => $value,
                );
            }
            // add the additional options array with the option code additional_options
            $observer->getProduct()
                ->addCustomOption('additional_options', serialize($additionalOptions));
        }
    }
}

附加选项将自动从产品移至报价项.有了这个观察者,您的选项就会出现在购物车和结账评论中.

The additional options will be moved from the product to the quote item automatically. With this observer in place, your options will appear in the cart and the checkout review.

为了让它们持久化,需要一个额外的观察者(仅自 Magento 1.5 起).

In order to have them persist, one additional observer is needed (only since Magento 1.5).

<sales_convert_quote_item_to_order_item>
    <observers>
        <extra_options>
            <type>model</type>
            <class>extra_options/observer</class>
            <method>salesConvertQuoteItemToOrderItem</method>
        </extra_options>
    </observers>
</sales_convert_quote_item_to_order_item>

这里我们将选项从报价项移到订单项.

Here we move the option from the quote item to the order item.

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

从现在开始,附加选项将在前端的客户订单历史记录和订单电子邮件以及管理界面订单视图、发票、发货、贷项通知单和 PDF 中可见.

From this point on the additional options will be visible in the customer order history in the frontend and the order emails, as well as in the admin interface order view, invoices, shipments, creditmemos and PDFs.

为了在重新订购期间将 oprions 转移到新订单,您需要小心地将它们复制过来.这是使用 checkout_cart_product_add_after 事件的一种可能性.

In order to carry the oprions over to the new order during a reorder, you need to take care to copy them over. Here is one possibility using the checkout_cart_product_add_after event.

<checkout_cart_product_add_after>
    <observers>
        <extra_options>
            <type>singleton</type>
            <class>extra_options/observer</class>
            <method>checkoutCartProductAddAfter</method>
        </extra_options>
    </observers>
</checkout_cart_product_add_after>

额外选项的解析和构建额外的选项数组应该移到一个单独的函数中以避免代码重复,但在这个例子中,为了清楚起见,我将保留每个方法所需的逻辑.

The parsing of the extra options and building the additional options array should be moved into a separate function to avoid code duplication, but for this example I'll leave the required logic for each method in place for clarity.

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
    $action = Mage::app()->getFrontController()->getAction();
    if ($action->getFullActionName() == 'sales_order_reorder')
    {
        $item = $observer->getQuoteItem();
        $buyInfo = $item->getBuyRequest();
        if ($options = $buyInfo->getExtraOptions())
        {
            $additionalOptions = array();
            if ($additionalOption = $item->getOptionByCode('additional_options'))
            {
                $additionalOptions = (array) unserialize($additionalOption->getValue());
            }
            foreach ($options as $key => $value)
            {
                $additionalOptions[] = array(
                    'label' => $key,
                    'value' => $value,
                );
            }
            $item->addOption(array(
                'code' => 'additional_options',
                'value' => serialize($additionalOptions)
            ));
        }
    }
}

翻译:

没有适当的机制来翻译这些选项标签或值.以下是一些可能在这方面有用的想法.

Translation:

There is no mechanism in place to translate these option labels or values. Here are a few ideas that might be useful in that regard.

在 quote_item_load_after 事件观察器中,获取附加选项数组并设置 $option['print_value'] = $helper->__($option['value']);.如果设置了 print_value,Magento 将使用它来渲染显示.
订单项也可以这样做.

In a quote_item_load_after event observer, get the additional options array and set $option['print_value'] = $helper->__($option['value']);. If print_value is set, Magento will use that for rendering the display.
The same can be done with order items.

没有像 print_label 这样的东西,但是您可以设置自定义索引(可能是 label_source)并使用它作为源即时设置标签,例如$option['label'] = $helper->__($option['label_source']);.

There is no such thing as a print_label, but you could set a custom index (label_source maybe) and set the label on the fly using that as the source, e.g. $option['label'] = $helper->__($option['label_source']);.

除此之外,您可能不得不求助于修改模板(grep for getItemOptions()),或覆盖块类(grep additional_options).

Beyond that you would probably have to resort to modifying the templates (grep for getItemOptions()), or overriding the block classes (grep additional_options).