【问题标题】:Magento - Loop through all products in category, change value and saveMagento - 遍历类别中的所有产品,更改价值并保存
【发布时间】:2015-03-20 06:28:13
【问题描述】:

我需要遍历特定类别中的所有产品,更改自定义属性,然后保存每个产品。

目前我有:

$_category = Mage::getModel('catalog/category')->load($_POST['id']);
$all_products = $_category->getProductCollection();

foreach ($all_products as $product) {
    $product = Mage::getModel('catalog/product')->load($product->getId());  
    $product->setCustomAttribute(xxxx);
    $product->save();
}

这是最佳做法吗?是否可以提高性能?每个集合平均大约有 15-20 个产品,我的加载时间非常慢,甚至脚本超时。

我已经使用set_time_limit(0); 尝试强制它通过,但我想我可以改进我的代码来阻止我必须这样做?最终,每个集合可能有超过 100-200 种产品,所以现在需要找到一个好的性能

【问题讨论】:

    标签: php magento loops collections foreach


    【解决方案1】:

    如果您要对相同的属性和相同的值进行大规模更新,请考虑尝试updateAttributes:

    // Iterate over the foreach and get all the product ids into an array
    // $attr = array("attribute_code" => "attribute_value");
    Mage::getSingleton("catalog/product_action")->updateAttributes($productIds, $attr, $storeId);
    

    应该会快很多。

    【讨论】:

    • 有用的答案老兄,它就像一个魅力。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多