以编程方式更新Magento属性 [英] Updating Magento Attribute Programmatically

查看:58
本文介绍了以编程方式更新Magento属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写更新我的属性add_ten_pence的脚本,该属性是一个yes/no布尔值.目前,它通过数据库中的SKU's,但是不幸的是它没有更新数据库.我在下面粘贴了脚本,有人知道我要去哪里哪里吗?

该属性默认设置为否",我希望在脚本运行时将其更改为是",反之亦然.

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

解决方案

尝试保存单个属性而不是整个产品集合.

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');
}


更新

快得多的方法:

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

(感谢 https://magento.stackexchange.com/users/146/marius !)

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?

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

解决方案

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!)

这篇关于以编程方式更新Magento属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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