Magento 复制选项
我正在为 magento 中的产品生成自定义选项,如下所示:
$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();
}
我在一个循环中运行它,每次都有不同的 SKU,当我运行一次循环时,它会为第一个产品生成自定义选项,第二个产品有它自己的自定义选项,第一个产品自定义选项,第三个产品具有所有三个产品的自定义选项,等等。任何人都可以给我一些关于为什么会发生这种情况的见解吗?
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 the third product has custom options for all three, etc.. could anyone give me some insight on why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉回复晚了,但由于 Magento 将 Product_option 作为单例进行管理,您需要在每次迭代时重置它:
希望这会有所帮助。
纪尧姆
Sorry for the late response but as Magento manage the product_option as a Singleton, you need to reset it on each iteration :
Hope this helps.
Guillaume
请注意,您设置的不是 $options 而是 $option (末尾没有“s”)。也许它与代码片段中未显示的一些变量相交。
还
$选项=数组();
在这里没用,只需将其删除。
如果您在代码示例中包含迭代周期和 $customAttributeString、$sku、$uniqueId 的初始化,那就更好了。
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.
在循环迭代
Atif之前工作正常
Work fine just before the loop iteration
Atif