题
我试图在产品收集页面上显示%年龄折扣,按照以下教程:http://www.pauldonnelly.net/magento-display-sale-icon-if-special-price/ http://shamimcse05.wordpress.com/2010/12/23/get-special-price-price-or-regular-price-in-magento/
我已经在上述代码中声明了这些:
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
然后,我使用此循环来获取所有产品:
<?php foreach ($_productCollection as $_product): ?>
在这个循环下,我正在尝试添加这些行,我尝试按照上述教程,但是他们开始给出错误,所以我混在一起,以摆脱错误,这就是代码已经成为:
<?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; ?>
我正在使用下面的代码检查输出:
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;
这是已分配特价价格的产品页面上的输出,并具有有效的开始和结束日期:
Discount is: 0% OFF
Special Price is: 500.0000
Regular Price is:
Special Price from date is:
它正在获得特殊价格的价值,但没有读取常规价格值“从数据”和“迄今为止”。
请帮助我解决这个问题。
解决方案
这应该对您有用:
$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;