If you are interested in Magento catalog, you must have not skipped product option – one of interesting and useful tools in Magento. Product option on one hand helps admin to easily manage products. On the other hand, it makes customers able to choose exactly products that they need.

How does Magento process the product option when a product is added to cart and an order is placed? Just read on to find the detailed answer for this question.

  • Add product option to cart

When product is added to cart, the product type instance (model) will process the buy request data and prepare the option for product.

01protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
02{
03$product = $this->getProduct($product);
04/* @var Mage_Catalog_Model_Product $product */
05// try to add custom options
06try {
07$options = $this->_prepareOptions($buyRequest, $product, $processMode);
08} catch (Mage_Core_Exception $e) {
09return $e->getMessage();
10}
11...
12}

This function of a product type model runs before the product is added to cart. This function calls _prepareOptions to prepare the product option depended on customer’s buy request.

01protected function _prepareOptions(Varien_Object $buyRequest, $product, $processMode)
02{
03$transport = new StdClass;
04$transport->options = array();
05foreach ($this->getProduct($product)->getOptions() as $_option) {
06/* @var $_option Mage_Catalog_Model_Product_Option */
07$group = $_option->groupFactory($_option->getType())
08->setOption($_option)
09->setProduct($this->getProduct($product))
10->setRequest($buyRequest)
11->setProcessMode($processMode)
12->validateUserValue($buyRequest->getOptions());
13 
14$preparedValue = $group->prepareForCart();
15if ($preparedValue !== null) {
16$transport->options[$_option->getId()] = $preparedValue;
17}
18}
19 
20$eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);
21Mage::dispatchEvent($eventName, array(
22'transport' => $transport,
23'buy_request' => $buyRequest,
24'product' => $product
25));
26return $transport->options;
27}

To change the default process custom option (Foe example: your custom product type), you can rewrite this function or catch the event catalog_product_type_prepare_%s_options (%s can be full or lite depending on the process mode).

  • Convert product custom option from quote to order item

Magento converts product custom option from quote to order item similarly to converting order, address… It uses the model sales/convert_quote to process the convert:

01public function itemToOrderItem(Mage_Sales_Model_Quote_Item_Abstract $item)
02{
03...
04$options = $item->getProductOrderOptions();
05if (!$options) {
06$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
07}
08$orderItem->setProductOptions($options);
09Mage::helper('core')->copyFieldset('sales_convert_quote_item', 'to_order_item', $item, $orderItem);
10...
11}

And the custom option is taken from the product type instance by function getOrderOptions.

01public function getOrderOptions($product = null)
02{
03$optionArr = array();
04if ($info = $this->getProduct($product)->getCustomOption('info_buyRequest')) {
05$optionArr['info_buyRequest'] = unserialize($info->getValue());
06}
07 
08if ($optionIds = $this->getProduct($product)->getCustomOption('option_ids')) {
09foreach (explode(',', $optionIds->getValue()) as $optionId) {
10if ($option = $this->getProduct($product)->getOptionById($optionId)) {
11 
12$confItemOption = $this->getProduct($product)->getCustomOption('option_'.$option->getId());
13 
14$group = $option->groupFactory($option->getType())
15->setOption($option)
16->setProduct($this->getProduct())
17->setConfigurationItemOption($confItemOption);
18 
19$optionArr['options'][] = array(
20'label' => $option->getTitle(),
21'value' => $group->getFormattedOptionValue($confItemOption->getValue()),
22'print_value' => $group->getPrintableOptionValue($confItemOption->getValue()),
23'option_id' => $option->getId(),
24'option_type' => $option->getType(),
25'option_value' => $confItemOption->getValue(),
26'custom_view' => $group->isCustomizedView()
27);
28}
29}
30}
31 
32if ($productTypeConfig = $this->getProduct($product)->getCustomOption('product_type')) {
33$optionArr['super_product_config'] = array(
34'product_code' => $productTypeConfig->getCode(),
35'product_type' => $productTypeConfig->getValue(),
36'product_id' => $productTypeConfig->getProductId()
37);
38}
39 
40return $optionArr;
41}

Thus you can change the product custom option for each product type when storing the order item by changing this function.

Anybody finds our Magento certificate tutorials helpful? Please let us know by dropping a line below. Three comments for the next post, OK?

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

3 Comments

  1. Thank you David for very helpful Magento articles.

    I’ve read all your previous tutorials – all are good and interesting.

    Thanks a lot.

Write A Comment