Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Displaying the Correct Price (Original or Special - Depending on the ‘to’ and ‘from’ Dates)
 
TwilightWebDesign
Jr. Member
 
Total Posts:  22
Joined:  2008-04-20
 

Hello Magento Community,

I have a module which lists products based on a certain criteria (on a frontend script in the template folder).
I am stuck on the part which selects the correct price, normal or special price (depending if todays date is within the special to and from dates). Because the getPrice() function just seems to return the normal product price.

Please look at my code to see why it is not functioning correctly, or please suggest another magento function to always give the price its currently being sold at smile

<?php
// Code to get the correct price for today
$specialToDate $_product->getResource()->getAttribute('special_to_date')->getFrontend()->getValue($_product);
$specialFromDate $_product->getResource()->getAttribute('special_from_date')->getFrontend()->getValue($_product);

$currentDate mktime(0,0,0,date("m"),date("d"),date("Y"));
$currentDate date("Y/m/d h:m:s"$currentDate);
//echo $currentDate;

if ($currentDate >= $specialFromDate && $currentDate $specialToDate && $_product->getSpecialPrice() != 0):
    
$price $_product->getSpecialPrice();
else:
    
$price $_product->getPrice();
endif
?>

Many Thanks,
Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
TwilightWebDesign
Jr. Member
 
Total Posts:  22
Joined:  2008-04-20
 

Oops, I’ve just seen the problem with it myself!

This line should be this:

$currentDate date("Y-m-d h:m:s"$currentDate);
Instead of this:
$currentDate date("Y/m/d h:m:s"$currentDate);

Full Working Code:

<?php
// Code to get the correct price for today
$specialToDate $_product->getResource()->getAttribute('special_to_date')->getFrontend()->getValue($_product);
$specialFromDate $_product->getResource()->getAttribute('special_from_date')->getFrontend()->getValue($_product);

$currentDate mktime(0,0,0,date("m"),date("d"),date("Y"));
$currentDate date("Y-m-d h:m:s"$currentDate);

if (
$currentDate >= $specialFromDate && $currentDate $specialToDate && $_product->getSpecialPrice() != 0):
    
$price $_product->getSpecialPrice();
else:
    
$price $_product->getPrice();
endif
?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top