dongtuo1482 2017-11-09 12:40
浏览 15
已采纳

更快地更新集合记录

i have a collection of records that i need to update stock. I heard that using "load" on magento makes it slower, and since i use it before updating my stock i wonder if there is a faster way, since i have thousand of products.

Here is code example:

$productCollection = Mage::getResourceModel('catalog/product_collection')
                   ->addFieldToFilter(array(
                    array('attribute'=>'reference','neq'=>''),
    ));
//Update Prices and Stock
foreach ($productCollection as $product) {
 try {
    $reference = $product->getReference();
    $res = $products_api->xpath("item/sku[.='{$reference}']/parent::*"); 

    if (count($res) <= 0){
       unset($product);
       unset($res);
       unset($reference);
       continue;
    }

    $product->load($product->getId());
    //Search for Stock
    $stock = $res[0]->stocks->quantity;
    //Update Stock
    $product->setStockData(array(
                'use_config_manage_stock' => 0,
                'manage_stock'            => 1, 
                'is_in_stock'             => $stock > 0 ? 1 : 0,
                'qty'                     => $stock,
            ));

    $product->save();
  • 写回答

1条回答 默认 最新

  • doulang9521 2017-11-09 13:58
    关注

    You may try to use the stock item loading instead of the product loading. Please, try to use the code below:

    $productCollection = Mage::getResourceModel('catalog/product_collection')
                       ->addFieldToFilter(array(
                        array('attribute'=>'reference','neq'=>''),
        ));
    //Update Prices and Stock
    foreach ($productCollection as $product) {
     try {
        $reference = $product->getReference();
        $res = $products_api->xpath("item/sku[.='{$reference}']/parent::*"); 
    
        if (count($res) <= 0){
           unset($product);
           unset($res);
           unset($reference);
           continue;
        }
    
    
        //Search for Stock
        $stock = $res[0]->stocks->quantity;
        //Update Stock
    
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
        if ($stockItem->getId()) {
            $isInStock = $stock > 0 ? 1 : 0;
            $stockItem->setQty($stock);
            $stockItem->setIsInStock($isInStock);
            $stockItem->setManageStock(1);
            $stockItem->setUseConfigManageStock(0);
            $stockItem->save();
        }
    

    Taken from Sonassi blog - Mass update stock levels in Magento - FAST

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改