Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The custom options are only stored on the quote as option ID's and values. Every time the options are rendered, they are basically reloaded from the database.<br> If you modify the values, you would need to save them, and that would set them for everybody. </p> <p>That said, I work around the issue by adding an additional custom option with the modified value on the fly, using an event observer. For this I use additional options.<br> Then I remove the original custom option from the quote item. </p> <p>Up to 1.4 Magento took care of the rest, but since then you need to copy the additional options to the order item manually, and also need to take care it's set again if an item is reordered.</p> <p>So here is an example observer configuration. </p> <pre><code>&lt;frontend&gt; &lt;events&gt; &lt;checkout_cart_product_add_after&gt; &lt;observers&gt; &lt;customoptions&gt; &lt;type&gt;singleton&lt;/type&gt; &lt;class&gt;customoptions/observer&lt;/class&gt; &lt;method&gt;checkoutCartProductAddAfter&lt;/method&gt; &lt;/customoptions&gt; &lt;/observers&gt; &lt;/checkout_cart_product_add_after&gt; &lt;sales_convert_quote_item_to_order_item&gt; &lt;observers&gt; &lt;customoptions&gt; &lt;type&gt;singleton&lt;/type&gt; &lt;class&gt;customoptions/observer&lt;/class&gt; &lt;method&gt;salesConvertQuoteItemToOrderItem&lt;/method&gt; &lt;/customoptions&gt; &lt;/observers&gt; &lt;/sales_convert_quote_item_to_order_item&gt; &lt;/events&gt; &lt;/frontend&gt; </code></pre> <p>The rest is handled in the observer class. </p> <pre><code>/** * Add additional options to order item product options (this is missing in the core) * * @param Varien_Event_Observer $observer */ public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer) { $quoteItem = $observer-&gt;getItem(); if ($additionalOptions = $quoteItem-&gt;getOptionByCode('additional_options')) { $orderItem = $observer-&gt;getOrderItem(); $options = $orderItem-&gt;getProductOptions(); $options['additional_options'] = unserialize($additionalOptions-&gt;getValue()); $orderItem-&gt;setProductOptions($options); } } /** * Manipulate the custom product options * * @param Varien_Event_Observer $observer * @return void */ public function checkoutCartProductAddAfter(Varien_Event_Observer $observer) { $item = $observer-&gt;getQuoteItem(); $infoArr = array(); if ($info = $item-&gt;getProduct()-&gt;getCustomOption('info_buyRequest')) { $infoArr = unserialize($info-&gt;getValue()); } // Set additional options in case of a reorder if ($infoArr &amp;&amp; isset($infoArr['additional_options'])) { // An additional options array is set on the buy request - this is a reorder $item-&gt;addOption(array( 'code' =&gt; 'additional_options', 'value' =&gt; serialize($infoArr['additional_options']) )); return; } $options = Mage::helper('catalog/product_configuration')-&gt;getCustomOptions($item); foreach ($options as $option) { // The only way to identify a custom option without // hardcoding ID's is the label :-( // But manipulating options this way is hackish anyway if ('Size' === $option['label']) { $optId = $option['option_id']; // Add replacement custom option with modified value $additionalOptions = array(array( 'code' =&gt; 'my_code', 'label' =&gt; $option['label'], 'value' =&gt; $option['value'] . ' YOUR EXTRA TEXT', 'print_value' =&gt; $option['print_value'] . ' YOUR EXTRA TEXT', )); $item-&gt;addOption(array( 'code' =&gt; 'additional_options', 'value' =&gt; serialize($additionalOptions), )); // Update info_buyRequest to reflect changes if ($infoArr &amp;&amp; isset($infoArr['options']) &amp;&amp; isset($infoArr['options'][$optId])) { // Remove real custom option unset($infoArr['options'][$optId]); // Add replacement additional option for reorder (see above) $infoArr['additional_options'] = $additionalOptions; $info-&gt;setValue(serialize($infoArr)); $item-&gt;addOption($info); } // Remove real custom option id from option_ids list if ($optionIdsOption = $item-&gt;getProduct()-&gt;getCustomOption('option_ids')) { $optionIds = explode(',', $optionIdsOption-&gt;getValue()); if (false !== ($idx = array_search($optId, $optionIds))) { unset($optionIds[$idx]); $optionIdsOption-&gt;setValue(implode(',', $optionIds)); $item-&gt;addOption($optionIdsOption); } } // Remove real custom option $item-&gt;removeOption('option_' . $optId); } } </code></pre> <p>This is it in a nutshell. Add error checking and taking care of special cases like adding the same product to the cart again as needed.<br> Hope this gets you started working with custom product options. Not half bad once you get familiar with them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload