Saturday, March 2, 2013

Getting magento product custom options

In one case custom options for products were supposed to be required but they were not set to required in the admin.As there were a lot of products it was impractical to set those manually so the only solution was a script. The easy way is to go into the database and set the is_require field to 1 in the catalog_product_option table. The following query can be used:
UPDATE `catalog_product_option` SET `is_require`=1

Getting the values for those options is difficult using sql queries so here's a bit of code to achieve that.
<?php
require_once (str_replace('//','/',dirname(__FILE__).'/') .'../app/Mage.php');
umask(0); 
Mage::app('admin');
$collection = Mage::getModel('catalog/product')->getCollection()->load();

echo $collection->count(),"<b r/>";
foreach ($collection as $product) {
 echo "<b>",$product->getId(), " - ",$product->getSku(),"</b><b r/>"; 
 $product->load();
 $i = 1;
 echo "<pre>";
 foreach ($product->getOptions() as $o) {
     echo "<strong>Custom Option:" . $i . "</strong><b r/>";
     echo "Custom Option TYPE: " . $o->getType() . "<b r/>";
     echo "Custom Option TITLE: " . $o->getTitle() . "<b r/>";
     echo "Custom Option Required: " . $o->getIsRequire() . "<b r/>";
     echo "Custom Option Values: <b r/>";
     $values = $o->getValues();
     foreach ($values as $v) {
         print_r($v->getData());
     }
     $i++;
     echo "----------------------------------<b r/>";
 }
 } 


Thanks to Subesh Pokhrel
Due to some unknown black magic the br tags in the code snippets get interpreted and having no time to deal with it now I've just  put a space between the b and the r

No comments:

Post a Comment