Magento php计算价格

时间:2014-12-10 11:10:13

标签: php magento

我提供50美元的免费提货,所以在结帐时使用此代码告知客户他们的总金额低于50美元他们应该购买更多来接收我的报价。

   <?php $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
         $grandtotal = $totals["grand_total"]->getValue(); //Grandtotal value 
     if ($grandtotal >= 0 && $grandtotal < 50) {
     echo ' your totals is '; 
     echo $formattedPrice = Mage::helper('core')->currency($grandtotal , true, false);
     echo ' .Add some more products and you will have free shipping!';
}  ?>

所以这将回应your totals is 16.50$ .Add some more products and you will have free shipping!
我想知道如何计算16.50 $ - 50 $的差异所以消息将是:
your totals is 16.50$ .Add 33.50$ more on your basket and you will have free shipping!

2 个答案:

答案 0 :(得分:1)

内部if:

echo $formattedPrice = Mage::helper('core')->currency(50-$grandtotal , true, false);

答案 1 :(得分:1)

我建议你先创建一个常量来定义值50,它可以在类的开头或你的常量类中,例如Constants::FREE_SHIPPING_VAL,这样做的原因是为了避免触及代码在未来,并且具有一个核心价值,您只能在一个地方进行更改,以便在其他地方进行复制。

然后您的代码将如下

 <?php $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
         $grandtotal = $totals["grand_total"]->getValue(); //Grandtotal value 
     if ($grandtotal >= 0 && $grandtotal < Constants::FREE_SHIPPING_VAL) {
     echo ' your totals is '; 
     $remaining = Mage::helper('core')->currency((Constants::FREE_SHIPPING_VAL-$grandtotal), true, false);
     echo $formattedPrice = Mage::helper('core')->currency($grandtotal , true, false);
     echo '.Add '.$remaining.' more on your basket and you will have free shipping!';
}  ?>