問題描述
總結
我想創建一個不保存到產品中的產品屬性,或者像普通產品屬性一樣顯示在產品編輯頁面上.相反,我希望將其保存到訂單/報價項目并顯示在訂單、發票等上.在將產品添加到購物車之前,客戶還應該在前端對其進行配置.
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.
詳情
- 就像自定義選項一樣,應將表單元素添加到前端產品頁面.
- 與自定義選項不同,這不是實際的產品屬性.它不應顯示在管理產品頁面或屬性集上.
- 客戶需要提供有效值.我需要能夠進行服務器端驗證.
- 我想要一個 .phtml 模板來生成它的 html.目前我能夠以令人滿意的(設計)結果覆蓋 app/design/frontend/base/default/catalog/product/view/type/default.phtml.但是,我不知道如何捕獲、驗證并最終保存其價值.
- Just like with Custom Options, a form element should be added to the frontend product page.
- Unlike Custom Options, this is not an actual product attribute. It should not be displayed on the admin product pages or attribute sets.
- The customer is required to provide a valid value. I need to be able to do server-side validation.
- I want to have a .phtml template generating its html. Currently I'm able to override app/design/frontend/base/default/catalog/product/view/type/default.phtml with satisfactory (design) results. However I don't know how to capture, validate and eventually save its value.
- 該值應顯示在所有發票、訂單、銷售電子郵件中.
- 我想用模板控制輸出,或者至少能夠返回用于顯示值的字符串
我的問題
- 如何在產品添加到購物車時驗證并最終將前端產品頁面上的
中的值保存到報價項中,然后在結帳過程中以訂單項目?
- 如何在訂單、發票、銷售電子郵件和此類頁面上顯示此值?
- 如何過濾訂單集合以獲取包含我的值設置為特定值的項目的訂單?
- How do I validate and eventually save the value from a
<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? - How do I display this value on the order, invoice, sales emails and such pages?
- How do I filter an order collection to fetch orders that has items with my value set to a specific value?
更新 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 probablysales/quote_item
as well) during events such assales_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) )); } } }
翻譯:
沒有適當的機制來翻譯這些選項標簽或值.以下是一些可能在這方面有用的想法.
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']);
. Ifprint_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()
),或覆蓋塊類(grepadditional_options
).Beyond that you would probably have to resort to modifying the templates (grep for
getItemOptions()
), or overriding the block classes (grepadditional_options
).這篇關于Magento - 根據用戶輸入報價/訂購產品項目屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!