且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式更新Magento属性

更新时间:2022-10-20 22:38:12

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

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