开发者

Magento duplicating options

I'm generating custom options for products in magento with the following:

    $options = array();
    $options = array(
        'title' => 'Select Options',
        'type' => 'radio',
        'is_require' => 1,
        'sort_order' => 0,
        'values' => array()
    );
    $options['values'][] = array(
        'title' => $customAttributeString,
        'price' => 0.00,
        'price_type' => 'fixed',
        'sku' => $uniqueId,
        'sort_order' => '1'
    );

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

    if(!$product->getOptionsReadonly()) {
        $product->setProductOptions(array($options));
        $product->setCanSaveCustomOptions(true);
        $product->save();
    }

I have this running in a loop, with a different SKU everytime and when I run my loop once, it generates custom options for the first product just fine, the second product has its own custom options, and the first products custom options, and t开发者_Python百科he third product has custom options for all three, etc.. could anyone give me some insight on why this is happening?


Sorry for the late response but as Magento manage the product_option as a Singleton, you need to reset it on each iteration :

Mage::getSingleton('catalog/product_option')->unsetOptions();

Hope this helps.

Guillaume


$product->setProductOptions(array($option));

Notice, that you're setting not $options but $option (without "s" at the end). Maybe it intersects with some of your variables not shown in code snippet.

Also $options = array(); is useless here, just remove it

It would be nicer, if you include iteration cycle and initialization of $customAttributeString, $sku, $uniqueId to you code sample there.


Mage::getSingleton('catalog/product_option')->unsetOptions();

Work fine just before the loop iteration

Atif

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜