Question

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.

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top