Magento-保存产品而未设置“使用默认值" [英] Magento - Save a Product without setting "Use Default Value"

查看:84
本文介绍了Magento-保存产品而未设置“使用默认值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多商店设置-StoreA,StoreB和StoreC.在控制器中(使用StoreA网址),我正在按以下方式编辑产品:

I have a multi-store setup - StoreA, StoreB and StoreC. And within a controller (using the StoreA url) I'm editing a product as follows:

$_product = new Mage_Catalog_Model_Product();
$_product->load($productId);
$_product->setData('attribute1','somevalue');
$_product->save();

如果我随后转到该特定产品的管理/编辑产品",则发现attribute1已正确设置,但我还发现,如果我在选择商店视图"中选择StoreA,则会发现所有使用默认值" (对于StoreA)值"复选框已设置为false.

If I then go to the Admin / Edit Product for that particular product, I find that attribute1 has been correctly set BUT I also find that if I select StoreA in the "Choose Shop View" I find that all the "Use Default Value" check-boxes (for StoreA) have been set to false.

问题:

  1. 如何修改上面的代码,以便使用默认值" StoreA的复选框保持为真
  2. 上述代码一经执行(使用默认 StoreA的值"设置为false),如何还原使用默认值" 值"对StoreA的值是否恢复为真?
  1. How do I modify the code above so the "Use Default Value" check-boxes for StoreA remain true
  2. Once that the above code has been executed (and the "Use Default Value" for StoreA set to false), how do I revert the "Use Default Value" values back to true for StoreA?

添加了屏幕截图:

推荐答案

我在看到的使用默认值"复选框上并不完全清楚,但是我注意到了两件事.

I'm not entirely clear on the 'Use Default Value' check-boxes you are seeing, but there are two things I noticed.

使用出厂模式

您的代码:

$_product = new Mage_Catalog_Model_Product();

使用工厂模式,这在Magento中是标准的:

Use the factory pattern, which is standard in Magento:

$_product = Mage::getModel('catalog/product');

这本身不是问题,但需要牢记.

That in itself isn't the problem, but it's something to keep in mind.

仅更新产品属性

接下来,如果仅保存特定属性,则仅更新该属性会更快(并可能避免出现问题).像这样:

Next, if you are only saving a specific attribute it would be quicker (and potentially avoid your problem) if you update only that attribute. Like so:

$attribute = array('attribute_code' => 'attribute_value');

Mage::getSingleton('catalog/product_action')
            ->updateAttributes($_product->getId(), $attribute, 0);

关于updateAttributes()方法的参考.我的理由是,这可能是默认值通过保存整个产品(而不只是一个特定的属性)来添加值.

Reference for the updateAttributes() method. My reasoning here is that it's possible default values are being added by saving the entire product, and not just a specific attribute.

如果这没有帮助,也许您所看到的屏幕截图可以更好地帮助我形象地查看问题.

If that doesn't help, perhaps a screenshot of what you are seeing could better help me visualize the issue.

这篇关于Magento-保存产品而未设置“使用默认值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆