Dynamically Hide Custom Options in Magento

MagentoMagento offers a stable and extensible platform for online store development.  Its full-featured administration and reporting offer businesses the capability to leverage an open-source alternative to a fully custom system.  The stock cart can often handle most of the store requirements through its built-in configuration options, while the open-source backend can be modified as necessary for more advanced customizations.

One of these more advanced customizations that require PHP backend modification is the ability to dynamically hide custom options.  Custom options are an item-level feature that enables users to select item options when adding the item to the cart.  Custom options could range from drop-down or radio button options for shirt color or sweater size, to a fully editable text box for custom inscriptions on the product.  Magento also offers the ability for store administrators to assign a price to custom options, dynamically increasing the product price before it is added to the cart.

At times, however, it may be necessary to dynamically hide certain options based on events.  For instance, if selling tickets to an event, certain features might not be available after a time period has expired.  In order to accommodate for this capability, the store will need to dynamically change its option listing based on other variables.

The first step to dynamically hiding custom options is to calculate and store the hide logic condition into a global variable at the top of the “view.phtml” file.  This is often found in the current theme’s folder, as follows:

//Line 36, after $_product = $this->getProduct();
global $_disable_CUSTOM_OPTION_NAME;
$_disable_CUSTOM_OPTION_NAME = true; //Set the boolean based on predefined logic

This code defines the global variable, and enables the user to decide whether or not to follow the logic for disabling the custom option based on the product.  Often this will be based on the category, or on the product features.

Next, the Select.php file will need to be updated to actually hide the option.  In this example, the radio button list is updated, however other controls might need to be changed in a different part of the getValuesHtml function.

//Line 127, before $selectHtml .=
global $_disable_CUSTOM_OPTION_NAME;
  if(strpos($_value->getTitle(),'XXX_IDENTIFYING_TEXT_IN_OPTION_NAME_XXX') !== false) continue;
  else if (/* LOGIC FOR SELECTING THE DEFAULT ITEM */) $checked = 'checked';

The code first checks whether or not a subset of the options should be disabled in the select list, and then hides options based on identifying text in their title.  In addition, there is a feature for setting a default selected option.  This can be useful if all the other options are hidden except one.  In this case, the last option should be selected by default, in order to simplify the user’s ordering process.

By changing these two files, Magento admins can dynamically hide custom option items, and bring more flexibility to their stores.  This code may need to be tweaked depending on the implementation specifics, however it should serve as a good framework for most changes with similar requirements.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.

RSS Twitter LinkedIn Facebook Email

One thought on “Dynamically Hide Custom Options in Magento”

Leave a Reply

Your email address will not be published. Required fields are marked *