简体   繁体   English

如何在magento中以编程方式更新自定义选项?

[英]How to update custom options programatically in magento?

I have lot of products with custom options, now I have requirement to update only custom options through csv file. 我有很多带有自定义选项的产品,现在我需要通过csv文件仅更新自定义选项。 so how we can do this programatically? 那么我们如何以编程方式执行此操作?

i found one solution for updating custom options programatically here is the solution 我找到了一个以编程方式更新自定义选项的解决方案

$product = Mage::getModel('catalog/product')->load($product_id);
$values = array();
foreach ($product->getOptions() as $o) {
           $p = $o->getValues();
        }
    }
  foreach($p as $v)
        {
            $values[$v->getId()]['option_type_id']= $v->getId();
                $values[$v->getId()]['title']= 'test';
                $values[$v->getId()]['price']= 23;
                $values[$v->getId()]['price_type']= 'fixed';
                $values[$v->getId()]['sku']= $value1;

          }
        $v->setValues($values);
        $v->saveValues();
$product->save();

hope this help someone this only update custom options value 希望这有助于此人只更新自定义选项值

I think this is also useful... 我觉得这也很有用......

If you are customize the products . 如果您是自定义产品。

<?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(1,2,167);

$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();

/**
this block is for changing information of specific option from collection options inside
current product
the save method for this option in end of code
*/

if ($title == "Line 1/Front") { 

$o->setProduct($product); 

$o->setTitle("Line 1/Ftont"); 

$o->setType("drop_down"); 

$o->setIsRequire(1);

$o->setSortOrder(0);


}

/**
this block for update or add information of specific value inside current option
*/

$optionType = $o->getType(); 

//test type

if ($optionType == "drop_down") { 

//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("/said$/i", $v->getTitle())) { 

//update and save

$v->setTitle("morad")

->setSku("kk")

->setPriceType("fixed")

->setSortOrder(0)

->setPrice(floatval(13.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);

} 
} 


/**
create new value object you can use $option->getValueInstance() for working with 
getSingleton
*/

$value = Mage::getModel('catalog/product_option_value'); 

$value->setOption($o) 

->setTitle('valueiwant') 

->setSku("nn")

->setPriceType("fixed")

->setSortOrder(1)

->setPrice(12)

/**
this ligne is important (relation forigien key) for related this new value
to specific option
*/

->setOptionId($o->getId());

$value->save();

/**
this ligne is important i collecte all value required for normalize save function   
related to current option
*/

$cos[]=$value->toArray($co);
} 


$o->setData("values",$cos)->save();

//var_dump($cos);

}


}
}

Do you create a module to do that ? 你创建了一个模块吗? If you do, you must use the cron system of Magento and call a method of a custom model : 如果这样做,您必须使用Magento的cron系统并调用自定义模型的方法:

<config>
    <!--...-->
    <crontab>
        <jobs>
            <company_test>
                <schedule>
                    <cron_expr>0,15,30,45 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>test/testmodel::testMethod</model>
                </run>
            </company_module>
        </jobs>
    </crontab>
</config>

When this is done, you can update the option of a specific product by using the model Mage_Catalog_Model_Product_Option . 完成此操作后,您可以使用模型Mage_Catalog_Model_Product_Option更新特定产品的选项。 I don't know how the CSV is made, but the algorithm can be something like that : 我不知道如何制作CSV,但算法可能是这样的:

// foreach option
/** @var $opt Mage_Catalog_Model_Product_Option */
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);

$optionArray = array(
    'is_delete' => 0,
    'title' => 'Blabla',
    'previous_group' => '',
    'previous_type' => '',
    'type' => 'field',  //can be radio, drop_down, file, area...
    'is_require' => 0,
    'sort_order' => 42,
    'values' => array()
);

$opt->addOption($optionArray);
$opt->saveOptions();
// end foreach

Also check this link : http://subesh.com.np/2009/12/adding-custom-options-product-magento/ 另请查看此链接: http//subesh.com.np/2009/12/adding-custom-options-product-magento/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM