1

I am saving products fetched from an XML document. I am using $product->save() but I need to do an update after the first save. But the $product->save() method is too much slow for update. I also tried updateAttribute to update product like:

public function productUpdate($productSkus=array(), $data, $storeId = 1){
            $ids=array();
            foreach($productSkus as $sku){
                $id = $this->_getid($sku);
                $attributesData = array("price" => $data['price']);
                array_push($ids,$id);
            }
             Mage::getSingleton('catalog/product_action')
                    ->updateAttributes($ids, $attributesData, $storeId);
        }

        public function _getid($sku){
        $product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 
        if (!$product){
            return false;
        }
            return $product->getId();        
    }

What should I do? How can I update products faster? Imagine there is an XML document that is keeping data of 8000 products (including custom attributes), and it is dynamic, which means it can change anytime. So I need to update products fast, but $product->save() and the above code are not suitable for this situation.

1
  • Are you trying to give all products the same price? Commented Oct 12, 2015 at 18:42

2 Answers 2

4

Firstly you could change _getid($sku) with Mage::getModel('catalog/product')->getIdBySku() which would be a bit more efficient.

Basically your function updates a group of products price yeah? When reading your code I can see that it should be able to simplify it to:

public function productUpdate($productSkus = array(), $data, $storeId = 1)
{
    $ids = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToFilter('sku', aray('in' => $productSkus))
        ->getAllIds();

    Mage::getSingleton('catalog/product_action')->updateAttributes(
        $ids,
        array('price' => $data['price']),
        $storeId
    );
}
2
  • thank you for your answer but this is not fast enough. I showed that function for an example . ıt can be anything to update product, but fast. Thats all i matter.Need faster update per product.
    – sj_magento
    Commented Oct 12, 2015 at 16:00
  • Oh right. Well you're not going to get a faster "magento" way to update the products. This way will be faster than your example as it doesnt need to load every product individually before applying the update. Commented Oct 12, 2015 at 16:10
1

You can try to make a external sql-request with INSERT ... ON DUPLICATE KEY UPDATE ... to the mage tables and call the reindex procedure. That's the fastet way. But: you need Deep knowing about the structure of the Database. In this case all internal update rules disabled. I think this is a Option, but not recommendable.

2
  • You are right . I thought that but i have no chance to crash something.There is too much table related each other. But thank you for your answer.
    – sj_magento
    Commented Oct 13, 2015 at 7:23
  • Take a loook to this: github.com/avstudnitz/AvS_FastSimpleImport
    – SFrey
    Commented Oct 13, 2015 at 8:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.