Mandatory custom options parameter in Magento

This is the case:

B2B Customers in a Magento website can buy products with custom options, for example “Box” or “Bag”.

Customers have a customer specific product price and a customer specific custom option preference. These are stored for each customer in a custom module (Anowave Product Price Per Customer).

The products in that module are used to generate a custom product list page for that customer where he/she can quickly reorder the products.

Displaying the product collection with their custom options (dropdown) is standard, setting the right custom option as standard value…hmm…his took some time and digging in the code.

Here is how we solved it. This is the short and dirty version I found during testing.

The select dropdown is processed through:

\app\code\core\Mage\Catalog\Block\Product\View\Options\Type\Select.php

Don’t mess with that page, just add a copy in your local folder

\app\code\LOCAL\Mage\Catalog\Block\Product\View\Options\Type\Select.php

This piece of code generates the dropdown option values before sending them toHtml();

 foreach ($_option->getValues() as $_value) {
 $priceStr = $this->_formatPrice(array(
 'is_percent' => ($_value->getPriceType() == 'percent'),
 'pricing_value' => $_value->getPrice(($_value->getPriceType() == 'percent'))
 ), false);
 
 $select->addOption(
 $_value->getOptionTypeId(),
 $_value->getTitle() . ' ' . $priceStr . '' array('price' => $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false))
 );

If you look at the $select->getHtml() function in this file you can find the following code on line 234.

\app\code\core\Mage\Core\Block\Html\Select.php
return sprintf('<option value="%s"%s %s>%s</option>',
 $this->escapeHtml($option['value']),
 $selectedHtml,
 $params,
 $this->escapeHtml($option['label']));

It seems that there are parameters available for the select -> option

You can add them in the first file

 foreach ($_option->getValues() as $_value) {
 $priceStr = $this->_formatPrice(array(
 'is_percent' => ($_value->getPriceType() == 'percent'),
 'pricing_value' => $_value->getPrice(($_value->getPriceType() == 'percent'))
 ), false);
 
 $select->addOption(
 $_value->getOptionTypeId(),
 $_value->getTitle() . ' ' . $priceStr . '' array('price' => $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false)),
'selected'
 );

And you need to correct the code in the second file so the data is actually processed. This second code corrections looks like a ‘bug’ in Magento? Why have params and not use them?

 return sprintf('<option value="%s"%s %s>%s</option>',
 $this->escapeHtml($option['value']),
 $selectedHtml,
 $this->escapeHtml($option['params']),
 $this->escapeHtml($option['label']));

The end


So, what do you think ?