0

I have an custom attribute in my products that I put the discount amount in for filtering on the front end. So I created a small script that calculates the discount amount and saves for each product if it differs (so if a price is changed without changing the discount amount this will rectify it once a night). It runs through 14000 products so when there is nothing to update it takes about 2 minutes to run, however when there are products to update (first time run updated about 5000 products and took about 17 minutes).

are there any changes I can make that might streamline and get the run time down on this?

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$_products_collection = Mage::getModel('catalog/product')->getCollection()
                                                         ->addAttributeToSelect('*');
$time_start = microtime(true);
foreach($_products_collection as $_product)
{

$_disc_value =  ceil(($_product->getPrice() - $_product->getSpecialPrice()) / $_product->getPrice() * 100);

if($_product->getDiscount_int() == $_disc_value):
;

else:

$_product->setDiscount_int($_disc_value)
         ->save();
;
endif;

}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo 'Total Execution Time: '.$execution_time.' Seconds';
echo "\n";


?>`
1
  • Might try filtering out disabled and not-visible products. Commented Feb 3, 2016 at 12:52

1 Answer 1

0

The fact that it's so much slower when changes are made is an indication that the additional time taken is during the save call, which means it's probably the number of events that get triggered during a product save. If you are only setting that one attribute and updating it, you might see considerable gain from changing...

$_product->setDiscountInt($_disc_value)->save()

...to...

$_product->setDiscountInt($_disc_value);
$_product->getResource()->saveAttribute($_product, 'discount_int');

This assumes that you aren't relying on any indexing etc performed during the save. If you are, you may need to manually trigger those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.