Magento - attribute of goods / order of goods / goods based on user input

Summary

I want to create a product attribute that is not saved in the products, or is displayed on the product edit page, like the usual product attributes. Instead, I want it to be saved in the order / quote position and displayed on orders, invoices, and the like. It must also be configured by the client in the interface before adding the product to the cart.

More details

  • As with custom options, the form element must be added to the product page of the interface.
    • Unlike custom settings, this is not an actual product attribute. It should not appear on product pages or on the set of administrator attributes.
    • The customer must provide a valid value. I need to be able to perform server side validation.
    • I want to create a .phtml template that creates its html. Currently, I can override application / design / frontend / base / default / catalog / product / view / type / default.phtml with satisfactory (design) results. However, I do not know how to capture, confirm and ultimately retain its meaning.
  • The value of this form element must be saved using the quotation / order item.
    • This value should be displayed on all and all accounts, orders, trade letters.
    • I want to control the output using a template, or at least be able to return a string that is used to display the value

My questions

  • How to check and ultimately save the value with <input> on the frontend product page in the quotation element when adding the product to the basket, and then during the checkout process in the order element?
  • How to display this value in an order, invoice, sales letters and such pages?
  • How to filter a collection of orders to retrieve orders that have items with my value set for a specific value?

Update 1

I found that I can run this code in the catalog/product model (and probably sales/quote_item ) during events like sales_quote_item_qty_set_after

 $infoBuyRequest = $product->getCustomOption('info_buyRequest'); $buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue())); $myData = $buyRequest->getMyData(); 

Thus, I was able to get my user data provided by the client from my <input> on the product page.

I suspect that this info_buyRequest saved with quotation and order elements. If so, this partially resolved my problems 1 and 2. However, I still do not know where it is suitable for running this code, and I do not know how to display it on the backend / quote / report pages. In addition, I believe that since this is stored as a serialized value in the database, it will be very difficult to get quotes / orders selections based on my user data.

+62
php magento
Feb 23 '12 at 11:23
source share
3 answers

Magento provides the ability to add parameters that are not product attributes or custom product parameters. They are set on product elements and quotes with the option code additional_options .

Two steps must be completed, each of which can be processed through an event observer. If you want additional options to perform reordering, you will also need to watch the third event.

Add options to item

The first step is to add an event observer to set additional parameters for the downloaded product before adding it to the basket. 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> 

In case the observer can add additional checks, the requested page is really an addition to the action in the basket. The main theme of this observer method is to add your special options selection to the additional_options option in 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)); } } } 

Additional parameters will be automatically moved from the product to the quotation element. With the help of this observer, your options will appear in the basket and view the check.

Add order options

To keep them, another observer is needed (only with 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 position to the order element.

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

From this moment, additional parameters will be displayed in the history of customer orders in the interface and email of the order, as well as in the presentation of the administrator interface order, invoices, deliveries, loans and PDF files.

Add reordering support

To shift fingerprints to a new order during reordering, you need to take care to copy them. Here is one possibility with 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 analysis of additional parameters and the construction of an array of additional options should be transferred to a separate function to avoid code duplication, but for this example I will leave the 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) )); } } } 

Transfer:

There is no mechanism for translating these labels or values. Here are a few ideas that may be helpful in this regard.

In the event observer quote_item_load_after, get an additional array of parameters and set $option['print_value'] = $helper->__($option['value']); . If print_value set, Magento will use this to display the display.
You can do the same with order items.

There is no such thing as print_label , but you can set your own index ( label_source possible) and set the label on the fly, using this as a source, for example. $option['label'] = $helper->__($option['label_source']); .

In addition to this, you may have to resort to modifying the templates (grep for getItemOptions() ) or overriding block classes (grep additional_options ).

+136
Feb 29 '12 at 9:10
source share

In the Quote element, you can add custom fields. How to add custom fields for order items in Magento for starters. I recently used this instruction to add custom fields to Quote Magento, and the concept is great, but there are some practical examples in this article that are small. Things I will do differently:

  • Use a setup script to add fields to the database, rather than doing it directly.
  • Use the Magento Request object, and not directly access $ _REQUEST.
  • Use extensions and rewrite rather than modifying the core of Magento.
  • Make changes to config.xml from the extension, not change the kernel.

As a rule, it is better to avoid modifying the Magento kernel and apply your settings through the module, as this simplifies / improves the update capabilities in the future. If you have not created your own extension to moduleCreator , you can generate the required template.

+10
Feb 29 '12 at 2:33
source share

My solution in Magento 1.8

Set option to specify an item

 $quoteItem = $cart->getQuote()->getItemById($itemId); $quoteItem->addOption(array('label' => 'buymode', 'code' => 'buymode', 'value' => $data['buymode'])); $quoteItem->save(); 

Access options from QuoteItem

 $quoteItem->getOptionByCode('buymode')->getValue(); 

Transfer Option for OrderItem

Register event sales_convert_quote_item_to_order_item

 public function onConvertQuoteItemToOrderItem($observer) { $orderItem = $observer->getOrderItem(); $quoteItem = $observer->getItem(); $options = $orderItem->getProductOptions(); $options['buymode'] = $quoteItem->getOptionByCode('buymode')->getValue(); $orderItem->setProductOptions($options); } 

Access options from OrderItem

 $orderItem->getProductOptionByCode('buymode') 
+1
Mar 22 '17 at 8:28
source share



All Articles