Can this be added to an observer like in my code in: How to set products in stock after import ?

I currently have a module I created that will check the sites prices.
It is currently just a page inside the admin that will show all the products that have special prices in a table.
The code itself is simple.
Check if the special price is equal to the product price or if it is equal to 0.
If it is change it to a blank.

    $_productCollection = Mage::getModel('catalog/product')->getCollection(); /* get the collection */
    $_productCollection->addAttributeToSelect('*'); /* select all attributes */
    $_productCollection->addAttributeToFilter('special_price', ['notnull' => true]); /* check if special_price is not null or empty */
    $_productCollection->addAttributeToSort('special_price', 'asc'); /* sort the special price */
    $_productCollection->getSelect(); /* get selection from database */
    Mage::getModel('review/review')->appendSummary($_productCollection); /* i have no idea what this does */
    $_helper = $this->helper('catalog/output');
    foreach ($_productCollection as $_product):
        $price = $_product->getPrice(); /* value of price */
        $specialprice = $_product->getSpecialPrice(); /* value of special price */
        $tprice = trim($price);
        $tspecialprice = trim($specialprice);
        /* check if special price equal to price and set empty */
        if($tprice==$tspecialprice) {
            $_product->getResource()->saveAttribute($_product, 'special_price');
        /* check if special price equal to zero and set empty */
        if($tspecialprice==0) {
            $_product->getResource()->saveAttribute($_product, 'special_price');

Can this be optimized?


I'm not sure what you are trying to achieve, but you can get the products that have a discount using a single query like this:

$discounted = Mage::getModel('catalog/product')->getCollection();
$discounted->addExpressionAttributeToSelect('discount_value', '(({{price}} - final_price))', array('price'));
$this->getSelect()->having('discount_value > ?' , 0);

This should get you started.

This is even better than working with only special price since the discounts can come from discount rules also.

| improve this answer | |
  • I'm only editing the special_price since my client is the one who places the prices on the site. My client sometimes imports a product that has either a special price equal to the regular price or the special price is a zero. The code I created is really slow and I want to combine it with my other code added to an observer inside the admin to work after import. The discount_value is not connected to what i am doing. Can you review my code to see if it can be optimized? – fja3omega May 18 '17 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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