总结
我想创建一个不保存到产品中的产品属性,或者像普通产品属性一样显示在产品编辑页面上.相反,我希望将其保存到订单/报价项目并显示在订单、发票等上.在将产品添加到购物车之前,客户还应该在前端对其进行配置.
I want to create a product attribute that is not saved to products, or displayed on the product edit page like ordinary product attributes. Instead I want it to be saved to order/quote items and displayed on orders, invoices and such. It should also be configurable by the customer in the frontend before adding a product to the cart.
详情
我的问题
中的值保存到报价项中,然后在结帐过程中以订单项目?<input>
on the frontend product page to the quote item when the product is added to the cart, and later in the checkout process to the order item?更新 1
我发现我可以在 sales_quote_item_qty_set_after 等事件期间在
catalog/product
模型(可能还有 sales/quote_item
)上运行此代码
I've discovered that I can run this code on a catalog/product
model (and probably sales/quote_item
as well) during events such as sales_quote_item_qty_set_after
$infoBuyRequest = $product->getCustomOption('info_buyRequest');
$buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue()));
$myData = $buyRequest->getMyData();
通过这种方式,我能够从产品页面上的 <input>
中检索自定义的、客户提供的数据.
In this way I was able to retrieve my custom, customer supplied, data from my <input>
on the product page.
我怀疑此 info_buyRequest
与报价和订单项目一起保存.如果是这样,这部分解决了我的问题 1 和 2.但是,我仍然不知道在哪里运行这段代码是合适的,我也不知道如何在后端订单/报价/报告页面上显示它.我也相信,因为它是作为序列化值存储在数据库中的,所以根据我的自定义数据获取报价/订单项目集合将是最困难的.
I suspect this info_buyRequest
is saved with the quote and order items. If so, this partially solved my problems 1 and 2. However, I still dont know where it's suitable to run this code, and I dont know how to display it on the backend order/quote/report pages. Also I belive since this is stored as a serialized value in the database, it will be most difficult to get quote/order item collections based on my custom data.
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)
));
}
}
}
没有适当的机制来翻译这些选项标签或值.以下是一些可能在这方面有用的想法.
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
).
这篇关于Magento - 根据用户输入报价/订购产品项目属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!