4

I have over 40,000 products in store and am trying to add custom options to all. The script below works fine, but stops after about 1000 products, then starts again.. making duplicates rather than continuing to next products.

What would be the best way to check if product has option with title "Request Samples?". If so, skip and go to next (if else statement)?

Here is the code I have:

require_once 'app/Mage.php';
umask(0);

Mage::app('admin');

set_time_limit(0);

$option = array(
    'title' => 'Request Samples?',
    'type' => 'drop_down', 
    'is_require' => 0,
    'sort_order' => 0,
    'values' => getOptions()
    );

$collection = Mage::getModel('catalog/product')->getCollection();

foreach ($collection as $product_all) {


        $sku = $product_all['sku'];

        // retrieve product id using sku
        $product_id = Mage::getModel('catalog/product')->getIdBySku($sku);

        //Add option for to all products
        $product = Mage::getModel('catalog/product')->load($product_id);
        $optionInstance = $product->getOptionInstance()->unsetOptions();
        $product->setHasOptions(1);
        $optionInstance->addOption($option);
        $optionInstance->setProduct($product);
        $product->save();
        unset($product);
        echo "Done";
}

    function getOptions(){
       return array(
       array(
        'title' => 'Yes',
        'price' =>0,
        'price_type' => 'fixed',
        'sku' => 'samples001',
        'sort_order' => '1'
        )
    );
}
1

3 Answers 3

3

To get Products which has option with title Request Samples?, please use below code.

$Option=Mage::getModel('catalog/product_option')->getCollection();
$Option->getSelect()->joinLeft( array('option_title'=> Mage::getSingleton('core/resource')->getTableName('catalog/product_option_title')), 'main_table.option_id = option_title.option_id',array('title'))->where('option_title.title = ?','Request Samples?');

$products = $Option->getColumnValues('product_id');

This will give you all the products which has option added with title Request Samples?

Now you can filter your product collection which does not have above products.

$collection = Mage::getModel('catalog/product')->getCollection()
             ->addAttributeToFilter('entity_id', array('nin'=>$products));

This will give you all products which does not have custom option added already. Then you can loop through these products and add the option.

1

You can try this modified code:

require_once 'app/Mage.php';
 umask(0);

 Mage::app('admin');

 set_time_limit(0);

 $values = array(
 array(
  'title' => 'red',
  'price' => '',
  'price_type' => "fixed",
  'sku' => "",
  'sort_order' => 1,
  'is_delete' => 0,
  'option_type_id' => -1,
 ),
array(
  'title' => 'green',
  'price' => '',
  'price_type' => "fixed",
  'sku' => "",
  'sort_order' => 1,
  'is_delete' => 0,
  'option_type_id' => -1,
 ),
 array(
  'title' => 'blue',
  'price' => '',
  'price_type' => "fixed",
  'sku' => "",
  'sort_order' => 1,
  'is_delete' => 0,
  'option_type_id' => -1,
  )
 );


$optionArr = array(array(
  "sort_order" => 1,
  "title" => "color",
  "price_type" => "fixed",
  "price" => "",
  "type" => "drop_down",
  "is_require" => 0,
  "values" => $values
  ));

 $collection = Mage::getModel('catalog/product')->getCollection();

foreach ($collection as $productAll) {
    echo 'ID' . $productAll->getID();
    $productId = $productAll->getID();

 $isExist = 0;
 $product = Mage::getModel('catalog/product')->load($productId);
if ($product->getHasOptions()) {
   foreach ($product->getOptions() as $option) {
    if ($option->getTitle() == 'color' && $option->getType() ==  'drop_down') {
        $isExist = 1;
        continue;
       }
   }
}
 if ($isExist) {
    continue;
 }
   $product->setProductOptions($optionArr);
   $product->setCanSaveCustomOptions(true);
   $product->save();
 }
0

In the following example code, I am adding custom option with title ‘Color’ and I have added two values to it ‘Red’ and ‘Gold’.

$productId = YOUR_PRODUCT_ID;
$product = Mage::getModel('catalog/product')->load($productId);

$options = array(
                'title' => 'Color',
                'type' => 'drop_down',
                'is_required' => 1,
                'sort_order' => 0,
                'values' => array(
                                array(
                                    'title' => 'Red',
                                    'price' => 10.50,
                                    'price_type' => 'fixed',
                                    'sku' => '',
                                    'sort_order' => 0,
                                ),
                                array(
                                    'title' => 'Gold',
                                    'price' => 0,
                                    'price_type' => 'percent',
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 0,
                                )
                            )
            );

$product->setProductOptions(array($options));
$product->setCanSaveCustomOptions(true);
$product->save();

You can now see the following custom options when you edit your product in admin: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.