0

I just don't get it. I have installed Magento 1.9.2.2 CE in my site.

But when I run following code as custom script from root directory, it doesn't set product custom option value price to my desired value, $v->setPrice is not working at all, while the other value functions do work, why is that ??

<?php
$magePath = 'app/Mage.php';
require_once $magePath;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product_ids  = array(897);
$productmodel = Mage::getModel('catalog/product');
foreach ($product_ids as $product_id) {
    /**i use this two arrays for collecte value because i uses inside setData of 
    current option*/
    $cos     = array();
    $co      = array();
    $product = $productmodel->load($product_id);
    $options = $product->getProductOptionsCollection();
    if (isset($options)) {
        foreach ($options as $o) {
            $title = $o->getTitle();
            $optionType = $o->getType();
            //test type
            if ($optionType == "drop_down" && $title == "New Custom Title") {
                //getting collection of value related to current option
                $values = $o->getValuesCollection();
                $found  = false;
                foreach ($values as $k => $v) {
                    //test existing of value for update
                    if (1 == preg_match("/Ja$/i", $v->getTitle())) {
                        //update and save
                        $v->setTitle("Ja")->setSku("kk45")->setPriceType("fixed")->setSortOrder(2)->setPrice(floatval(120.0000));
                        $v->setOption($o)->save();
                        /**
                        this ligne is important i collecte all value required for normalize save function 
                        related to current option
                        */
                        $cos[] = $v->toArray($co);
                    }
                }
            }
            $o->setData("values", $cos)->save();
            //var_dump($cos);
        }
    }
}
2

You need to set Store Id before saving option value price and title. This line will resolve your problem: $v->setStoreId('0'); Enjoy!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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