1

I'm trying to display %age discount on product collection page, following these tutorials: http://www.pauldonnelly.net/magento-display-sale-icon-if-special-price/ http://shamimcse05.wordpress.com/2010/12/23/get-special-price-or-regular-price-in-magento/

I've declared these in the above code:

$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');

Then i've used this loop to get all the products:

<?php foreach ($_productCollection as $_product): ?>

Under this loop, i'm trying to add these lines, I've tried following the above mentioned tutorials as it is, but they starting giving errors, so i mixed, them up to get rid of errors, and here is what the code has become:

         <?php $specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice();
            $thisProduct= Mage::getModel('catalog/product')->load(product_id); 
            $regularprice = $thisProduct->getPrice();
            // Get the Special Price FROM date
            $specialPriceFromDate = $thisProduct->getSpecialFromDate();
            // Get the Special Price TO date
            $specialPriceToDate = $thisProduct->getSpecialToDate();
            // Get Current date
            $today = time();
            if ($specialprice):
                if($today >= strtotime( $specialPriceFromDate) && $today = strtotime( $specialPriceFromDate) && is_null($specialPriceToDate)):
                    $discount = (($regularprice-$specialprice)/$regularprice)*100 ;
                endif;
            endif; ?>

I'm using the code below to check output:

                echo "Discount is: ".$discount = (($regularprice-$specialprice)/$regularprice)*100 ."% OFF" ;
                echo "</br>Special Price is: ".$specialprice;
                echo "</br>Regular Price is: ".$regularprice;
                echo "</br>Special Price from date is: ".$specialPriceFromDate;

and this is the output on the page for the products, which have been assigned special price, with valid start and end dates:

Discount is: 0% OFF
Special Price is: 500.0000
Regular Price is:
Special Price from date is:

It is getting the values of Special Price, but it is not reading the regular price value,'From Data' and 'To Date'.

Please help me solve this problem.

3

This should work for you:

$myproductobject = Mage::getModel('catalog/product')->load($_product->getId());
$specialprice = $myproductobject->getSpecialPrice();
$regularprice = $myproductobject->getPrice();
$specialPriceFromDate = $myproductobject->getSpecialFromDate();
$specialPriceToDate = $myproductobject->getSpecialToDate();
$today = time();
if ($specialprice):
    if($today >= strtotime( $specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime( $specialPriceFromDate) && is_null($specialPriceToDate)):
        $discount = (($regularprice-$specialprice)/$regularprice)*100 ;
    endif;
endif;
  • Depending on your collection you can also try to delete line 1 and replace $myproductobject with already loaded $_product. Saves you one product model load. – Stefan Gregori Dec 19 '13 at 8:33
  • There seems to be a problem in the if statement, it isn't letting code let thru: if($today >= strtotime( $specialPriceFromDate) && $today = strtotime( $specialPriceFromDate) && is_null($specialPriceToDate)): endif; – anwartheravian Dec 19 '13 at 8:53
  • try edited version – Stefan Gregori Dec 19 '13 at 9:00
  • The original code wasn't able to run due to a typo here : $thisProduct= Mage::getModel('catalog/product')->load(product_id); product_id is not a variable. – dagfr Dec 19 '13 at 16:06

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.