2

I found a lot of answers to the problem I have, but some are contradicting and many are not fully clear to me, so I hope a Magento expert can point me into the right direction.

For some Products in Magento, there is a complex customization process. There is a custom editor and every "design" is identified by a unique value. What I need is to store this ID with the product in the cart and the order. It shouldn't be shown directly, but accessible in the code (to show a different thumbnail in the cart or some information on the invoice, for example).

Additionally, because the product is customized, the same product should have a separate line in the cart and invoice if this id differs!

So for example, a customer opens the product page, does some customization and this customization gets an ID "1". The customer puts that into the cart. He opens the same product again, customizes it differently and it gets the id "2". The customer also puts this into the cart!

The cart should now be:

  • ProductX [custom text generated by code using id 1] 1 piece x€
  • ProductX [custom text generated by code using id 2] 1 piece x€

and NOT:

  • ProductX 2 piece x€

But still the customer should be able to order more than one of the same product with same ID! so e.g.

  • ProductX [custom text generated by code using id 1] 4 piece x€
  • ProductX [custom text generated by code using id 2] 2 piece x€

The customization happens outside of magento. Magento just has a link with a popup and then gets some ID that can then be attached using PHP. It just stores the ID and it has to be possible to get the ID for every product in cart/invoice/...

Important is that the ID is stored and that products with same sku/customizable options are not merged in the cart if the ID is different! Also the ID has to be a text string and not selected from a list like with magento customizable options!

Methods I found that might do what I want:

  1. http://www.atwix.com/magento/custom-product-attribute-quote-order-item/ This looks the best for me, there is a finished Module I could modify. But I am not sure if it is the correct approach. Will the products be separated in cart? Also the value of the ID is set as Product option here and visible in the backend?! There is no value before a customer customizes the product in my case!

    1. https://magento.stackexchange.com/a/1382 I am not quite sure... How can I get the ID, that is available on the product detail page, inside the observer...

    2. How can I add every product as a new item into cart Maybe I need to modify this in addition to 1. or 2.?

I would greatly appreciate it if a Magento Expert could shine some light on this for me!

4
  • Is the pop-up available to the customer ? In other words: is it your customer that by selecting something, set the external ID ? If yes, maybe what you want can be achieved more out of the box via 'custom options'. Jan 13, 2015 at 12:33
  • no, the external ID identifies a complex design and is generated randomly for every design! No designs have the same ID, ever! Every ID is unique!
    – Josef
    Jan 13, 2015 at 12:36
  • Yes but this is the customer choosing the requested design, isn't it ? Jan 13, 2015 at 13:16
  • he is creating the design in an graphics editor
    – Josef
    Jul 31, 2015 at 13:41

1 Answer 1

5
+50

You said you can take care of adding the ID to the product page. Which is great. When required, you need to write the value to a hidden input field within the add to cart form on the product page. Let's say the input looks like this;

<input type="hidden" name="myid" id="myid" value="123" />

Within your custom module, add an event observer. I am assuming here that you remain on the product page when you add to cart, rather than go direct to the cart.

<catalog_product_load_after>
<observers>
    <Company_Module_Model_Observer>
        <type>model</type>
        <class>Company_Module_Model_Observer</class>
        <method>addPostData</method>
    </Company_Module_Model_Observer>
</observers>
</catalog_product_load_after>   

Within your module folder, create or open Company/Module/Model/Observer.php and create this function;

class Company_Module_Model_Observer {


public function addPostData(Varien_Event_Observer $observer) {

      $action = Mage::app()->getFrontController()->getAction();
      if ($action->getFullActionName() == 'checkout_cart_add') {
         if($action->getRequest()->getParam('myid')) {
             // ID IS PRESENT, SO LETS ADD IT
             $item = $observer->getProduct();
             $additionalOptions = array();
             $additionalOptions[] = array(
                'label' => 'The unique ID',
                'value' => $action->getRequest()->getParam('myid')
            );
             $item->addCustomOption('additional_options', serialize($additionalOptions));
         }
      }

   }


}

With this in place, you will see the ID against the product in the cart and on the quote, just like if the value was from a super attribute. Any unique ID will be seen as a new line item.

To save this value against the sales order so it shows on sales emails and in the backend, add this observer;

<sales_convert_quote_item_to_order_item>
<observers>
    <Company_Module_Model_Observer>
        <type>model</type>
        <class>Company_Module_Model_Observer</class>
        <method>salesConvertQuoteItemToOrderItem</method>
    </Company_Module_Model_Observer>
</observers>
</sales_convert_quote_item_to_order_item>

And add this function to Company/Module/Model/Observer.php

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

}

3
  • Please mind that even this solution is perfectly fine, you would find yourself in trouble if the customer do edit the product in his cart. Then you will sadly loose your custom attribute added to the quote. Jan 15, 2015 at 9:23
  • if the user updates the multiple in cart it would hold the data, if they click to edit the product back on it's page, it sounds like the ID should change anyway as it relates the specification the user chooses, so I think it would need to change anyway.
    – PixieMedia
    Jan 15, 2015 at 9:27
  • works great. When the user opens a product page from the cart the id is a url parameter, so he gets back to that exact product.
    – Josef
    Jul 31, 2015 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.