Magento - 將用戶定義的產品屬性添加到報價和訂單
簡介
Magento提供一個可自訂的平台,讓使用者可以使用自訂屬性來擴展其功能。本指南將示範如何建立專為報價和訂單商品設計的產品屬性,使用戶能夠在結帳過程中配置和擷取此屬性。
建立和新增屬性
在訂單中保留屬性
顯示屬性
其他注意事項
範例程式碼
事件觀察器後載入目錄產品
<code class="php">public function catalogProductLoadAfter(Varien_Event_Observer $observer) { // Get the product $product = $observer->getProduct(); // Get the extra options $options = $action->getRequest()->getParam('extra_options'); // Add the extra options to the product $additionalOptions = array(); if ($additionalOption = $product->getCustomOption('additional_options')) { $additionalOptions = (array) unserialize($additionalOption->getValue()); } foreach ($options as $key => $value) { $additionalOptions[] = array( 'label' => $key, 'value' => $value, ); } $product->addCustomOption('additional_options', serialize($additionalOptions)); }</code>
<code class="php">public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer) { // Get the quote item and order item $quoteItem = $observer->getItem(); $orderItem = $observer->getOrderItem(); // Get the additional options from the quote item $additionalOptions = $quoteItem->getOptionByCode('additional_options'); // Set the additional options on the order item $options = $orderItem->getProductOptions(); $options['additional_options'] = unserialize($additionalOptions->getValue()); $orderItem->setProductOptions($options); }</code>
<code class="php">// Get the product options $options = $_item->getOptions(); // Check if additional_options is set if (isset($options['additional_options'])) { // Get the additional options $additionalOptions = $options['additional_options']; // Add a placeholder row for the custom attribute echo '<tr> <td class="options label" title=""><span>' . $this->__('Custom Attribute') . '</span></td> <td class="options value" title=""> <span>' . $additionalOptions[0]['value'] . '</span> </td> </tr>'; }</code>模板中的自訂屬性顯示
以上是如何在 Magento 中為報價和訂單添加自訂屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!