php 以编程方式更新 Magento 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15999439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating Magento Attribute Programmatically
提问by iamgraeme
I have been writing a script that updates my attribute add_ten_pence
, the attribute is a yes/no boolean value. At the moment it goes through the SKU's
in the database but unfortunately it's not updating the database. I have pasted the script below does anyone know where I'm going wrong?
我一直在编写一个脚本来更新我的属性add_ten_pence
,该属性是一个是/否布尔值。目前它通过SKU's
数据库中的,但不幸的是它没有更新数据库。我已经粘贴了下面的脚本有人知道我哪里出错了吗?
The attribute is set to 'No' as default, and I wish to change it to 'Yes' when the script runs, and vice versa.
该属性默认设置为“否”,我希望在脚本运行时将其更改为“是”,反之亦然。
require_once('app/Mage.php');
umask(0);
Mage::app('default');
Mage :: app ()->setCurrentStore(Mage_Core_Model_App :: ADMIN_STORE_ID);
$productCollection = Mage::getModel('catalog/product')->getCollection();
foreach($productCollection as $_product)
{
echo "\n".'updating '.$_product->getSku()."...\n";
$product = Mage::getModel('catalog/product')->load($_product->getEntityId());
$product->setAddTenPence(true);
$product->save();
}
回答by seanbreeden
Try saving the single attribute instead of the entire product collection.
尝试保存单个属性而不是整个产品集合。
require_once('app/Mage.php');
umask(0);
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productCollection = Mage::getModel('catalog/product')->getCollection();
foreach($productCollection as $product) {
echo 'updating ' . $product->getSku() . PHP_EOL;
$product->setData('add_ten_pence', 1);
$product->getResource()->saveAttribute($product, 'add_ten_pence');
}
UPDATE
更新
Much faster way:
更快的方法:
$productCollection = Mage::getModel('catalog/product')->getCollection();
$array_product = $productCollection->getAllIds();
Mage::getSingleton('catalog/product_action')->updateAttributes($array_product, array('add_ten_pence' => 1), Mage_Core_Model_App::ADMIN_STORE_ID);
(Thanks https://magento.stackexchange.com/users/146/marius!)