LinkedIn Twitter RSS Reset

Getting all custom options of a product in Magento

Custom options are the options that can be added to any product, which gives an option for the user of the site to select what type of product they actually want. Here is a scenario where custom option can be helpful. If you have a product say ball and your stores sells plastic,rubber,glass balls. So you need to give the user to select what type of ball they actually want. Yes, you can do this by using configurable product as well, but why create an attribute for one product only! In this case custom option might be very helpful.

Here’s the code to get the all the custom options of a product. Please see that you need to specify the id of the product for which you are retriving custom option for. I’ve echoed some text as well to give you an idea what exactly you are getting in each loop.

$product = Mage::getModel("catalog/product")->load(167);
	$i = 1;

	echo "<pre>";

	foreach ($product->getOptions() as $o) {

		echo "<strong>Custom Option:" . $i . "</strong><br/>";
		echo "Custom Option TYPE: " . $o->getType() . "<br/>";
		echo "Custom Option TITLE: " . $o->getTitle() . "<br/>";
		echo "Custom Option Values: <br/>";

		// Getting Values if it has option values, case of select,dropdown,radio,multiselect

		$values = $o->getValues();
		foreach ($values as $v) {

				print_r($v->getData());
		}

		$i++;

		echo "----------------------------------<br/>";
	}

Happy coding in Magento!

8 Responses to “Getting all custom options of a product in Magento”

  1. corp
    February 11, 2010 at 1:20 pm #

    Could you help me in telling that how can i display the custom option value of the particular product to admin side under Sales>order .

    I have add the column but how to display its value under it ,i have edited
    Grid.php file . please help me out

    • Subesh Pokhrel
      February 12, 2010 at 4:26 am #

      Hi Corp,
      Are you creating a custom type of custom option? Because Magento’s Core types of Custom option will automatically be seen in sales->order.
      If you are creating a new type of custom option then, I guess there is a problem in the Models somewhere..

  2. December 31, 2010 at 9:30 am #

    I’m thankful to you for this neat and clean code.

  3. Mark
    January 13, 2011 at 5:44 am #

    Thank you very helpful!

  4. Rishish
    May 16, 2011 at 7:58 am #

    Thank you so much, it helped me a lot,
    Many thanks

  5. Jay
    October 3, 2011 at 10:26 am #

    Hi, thank you for this very useful post. I am a bit struggling to show just the price chosen by customer, from a dropbox option in cart.

    What should I do in order to show value ‘price’ chosen by customer in cart ? If you have any recommandation I would be very thankful… Thanks you.

  6. Gz Chauhan
    November 5, 2011 at 2:07 pm #

    Thanx a lots its help me lots..!!!!!!!!!!!!

  7. Gz Chauhan
    December 7, 2011 at 7:18 am #

    Thanx for share..its help me lots..!!!!!!

Leave a Comment