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.

protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode)
{
$product = $this->getProduct($product);
/* @var Mage_Catalog_Model_Product $product */
// try to add custom options
try {
$options = $this->_prepareOptions($buyRequest, $product, $processMode);
} catch (Mage_Core_Exception $e) {
return $e->getMessage();
}
...
}

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.

protected function _prepareOptions(Varien_Object $buyRequest, $product, $processMode)
{
$transport = new StdClass;
$transport->options = array();
foreach ($this->getProduct($product)->getOptions() as $_option) {
/* @var $_option Mage_Catalog_Model_Product_Option */
$group = $_option->groupFactory($_option->getType())
->setOption($_option)
->setProduct($this->getProduct($product))
->setRequest($buyRequest)
->setProcessMode($processMode)
->validateUserValue($buyRequest->getOptions());

$preparedValue = $group->prepareForCart();
if ($preparedValue !== null) {
$transport->options[$_option->getId()] = $preparedValue;
}
}

$eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);
Mage::dispatchEvent($eventName, array(
'transport' => $transport,
'buy_request' => $buyRequest,
'product' => $product
));
return $transport->options;
}

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:

public function itemToOrderItem(Mage_Sales_Model_Quote_Item_Abstract $item)
{
...
$options = $item->getProductOrderOptions();
if (!$options) {
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
}
$orderItem->setProductOptions($options);
Mage::helper('core')->copyFieldset('sales_convert_quote_item', 'to_order_item', $item, $orderItem);
...
}

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

public function getOrderOptions($product = null)
{
$optionArr = array();
if ($info = $this->getProduct($product)->getCustomOption('info_buyRequest')) {
$optionArr['info_buyRequest'] = unserialize($info->getValue());
}

if ($optionIds = $this->getProduct($product)->getCustomOption('option_ids')) {
foreach (explode(',', $optionIds->getValue()) as $optionId) {
if ($option = $this->getProduct($product)->getOptionById($optionId)) {

$confItemOption = $this->getProduct($product)->getCustomOption('option_'.$option->getId());

$group = $option->groupFactory($option->getType())
->setOption($option)
->setProduct($this->getProduct())
->setConfigurationItemOption($confItemOption);

$optionArr['options'][] = array(
'label' => $option->getTitle(),
'value' => $group->getFormattedOptionValue($confItemOption->getValue()),
'print_value' => $group->getPrintableOptionValue($confItemOption->getValue()),
'option_id' => $option->getId(),
'option_type' => $option->getType(),
'option_value' => $confItemOption->getValue(),
'custom_view' => $group->isCustomizedView()
);
}
}
}

if ($productTypeConfig = $this->getProduct($product)->getCustomOption('product_type')) {
$optionArr['super_product_config'] = array(
'product_code' => $productTypeConfig->getCode(),
'product_type' => $productTypeConfig->getValue(),
'product_id' => $productTypeConfig->getProductId()
);
}

return $optionArr;
}

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