php - Show Products Special From Date - Optimize Magento Script -


i trying list products have special date within last 72 hours. script works fine except on 400 products times out.

i think loading many collections or not correctly utilizing ones loaded need optimization advice please.

here script:

<?php   set_time_limit(0); require_once 'app/mage.php'; mage::app('default');  try {     $products = mage::getmodel('catalog/product')->getcollection()         ->setstoreid(1);     $products->addattributetofilter('status', 1);     $products->addattributetofilter('visibility', 4);     $products->addattributetoselect('*');     $prodids=$products->getallids();     $product_count = 0;     $products_changed = 0;      //$product = mage::getmodel('catalog/product');      echo "price changes last 72 hours<br><br>";      foreach($prodids $productid)      {         $product_count++;         //$products->load($productid);          // load product data         $product = mage::getmodel('catalog/product')->load($productid);          $special_price = round($product->getspecialprice(),2);         $human_readable_date = $product->getspecialfromdate();         $sku = $product->getsku();         $specialpricefromdate = strtotime($human_readable_date);         $time_now = microtime(true);         $product_link = "http://www.mydomain.com.au/".$product->geturlpath();          $date_array = explode( ' ', $human_readable_date);         $short_date = $date_array[0];          if (($time_now - $specialpricefromdate) < 259200)         {             $products_changed++;              echo "sku: " . '<a href=' . $product_link . '>' . $sku . '</a>' . "  date changed: " . $short_date . " new price $" . $special_price . "<br>";         }         //if ($product_count == 400) exit;       } }  catch(exception $e){     $headers = 'content-type: text/html; charset=iso-8859-1' . "\r\n";      error_log($e->getmessage(), 1, 'me@mydomain.com.au', $headers);     die($e->getmessage()); }   ?> 

you're first loading complete collection , loading products again, 1 one. result in huge memory consumption , slow.

additionally you're loading all product attributes (which can hundreds), seem interested in few of them.

to optimize both issues, i'd recommend directly filter products of interest:

$inow = strtotime(date('y-m-d h:i:s')); $ifrom = $inow - (60 * 60 * 24 * 3); $oproductcollecion = mage::getmodel('catalog/product')     ->getcollection()     ->setstoreid(1)     ->addattributetoselect(array('sku', 'special_price', 'url_path'))     ->addattributetofilter('status', 1)     ->addattributetofilter('visibility', 4)     ->addattributetofilter(         'special_from_date',          array('from' => date('y-m-d h:i:s', $ifrom))     );  foreach($oproductcollecion $oproduct) {     $product_link = "http://www.mydomain.com.au/" . $oproduct->geturlpath();     $sku = $oproduct->getsku();     $short_date = $oproduct->getspecialfromdate();     $special_price = round($oproduct->getspecialprice(), 2);     echo "sku: " . '<a href=' . $product_link . '>' . $sku . '</a>' . "  date changed: " . $short_date . " new price $" . $special_price . "<br>"; } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -