gpt4 book ai didi

Magento:更快地更新产品目录

转载 作者:行者123 更新时间:2023-12-03 06:18:43 24 4
gpt4 key购买 nike

我已经编写了很多脚本来根据某些或其他参数更新我的产品目录。其中每个的基本逻辑都与此类似......

     //Get collection
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('sku');
$collection->addAttributeToSelect('publihser');
$collection->addFieldToFilter(array(array('attribute'=>'publisher','eq'=>$publisher)));

// for each product in collection do a individual save
foreach ($collection as $product) {
$product->setSKU($newValue);
$product->save();
}

虽然这项工作有效,但每次保存都是一个 SQL 更新查询,而且事实是,如果目录非常大,速度相当慢。

我想知道是否可以通过在集合上而不是在产品上进行单一保存来加快速度。

最佳答案

您可以采取多种措施来编写更快的更新脚本。我不知道您如何获取一些变量,因此您需要进行修改才能使其在您的情况下工作,但下面的代码应该比您当前执行的方式快得多。例如:

// Set indexing to manual before starting updates, otherwise it'll continually get slower as you update
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');

// Get Collection
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('publihser')
->addFieldToFilter(array(array('attribute'=>'publisher','eq'=>$publisher)));


function productUpdateCallback($args){
$product = Mage::getModel('catalog/product');

$product->setData($args['row']);

$productId = $product->getId();

$sku = 'yourSku';

// Updates a single attribute, much faster than calling a full product save
Mage::getSingleton('catalog/product_action')
->updateAttributes(array($productId), array('sku' => $sku), 0);
}

// Walk through collection, for large collections this is much faster than using foreach
Mage::getSingleton('core/resource_iterator')->walk($collection->getSelect(), array('productUpdateCallback'));


// Reindex all
$processes->walk('reindexAll');
// Set indexing back to realtime, if you have it set to manual normally you can comment this line out
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');

关于Magento:更快地更新产品目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8167220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com