如何显示magento 2中保存的金额

时间:2016-03-25 07:54:30

标签: php magento price magento2.0.2

我正在尝试显示magento 2.0.2网站目录页面上保存的金额。但是我没有显示计算结果。我只是得到一个空的空间。我正在编辑主题文件中的final_price.phtml。请告诉我有什么问题。我没有在谷歌中找到任何信息,因为大多数结果都与magento 1相关,而且代码会抛出错误。这是我的代码在我尝试进行计算的部分中的样子。建议表示赞赏。感谢

<span class="special-price"><span class="only-text">Only: </span>
    <?php echo $block->renderAmount($finalPriceModel->getAmount(), [
        'display_label'     => __('Special Price'),
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
</span>
<br>
<span class="old-price"><span class="rrp-text">RRP: </span>
    <?php echo $block->renderAmount($priceModel->getAmount(), [
        'display_label'     => __('Regular Price'),
        'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
        'price_type'        => 'oldPrice',
        'include_container' => true,
        'skip_adjustments'  => true
    ]); ?>
</span>
<span class="saving-price"><span class="saving-text">Saving: </span>
<?php
$wasPrice = $block->renderAmount($priceModel->getAmount(), []);
$nowPrice = $block->renderAmount($finalPriceModel->getAmount(), []);
  if ($nowPrice < $wasPrice){
    $saving = $wasPrice - $nowPrice; 
    echo $saving;
  }
?>
</span>

3 个答案:

答案 0 :(得分:2)

而不是使用

$wasPrice = $block->renderAmount($priceModel->getAmount(), []);
$nowPrice = $block->renderAmount($finalPriceModel->getAmount(), []);

你应该使用

$wasPrice = $priceModel->getValue();
$nowPrice = $finalPriceModel->getValue();

$ priceModel和$ finalPriceModel引用/** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */,你可以在final_price.phtml的第17行看到

然后在PriceInterface.php里面我们看到getAmount()返回一个对象,而getValue()只返回一个数字。

/**
 * Get price value
 *
 * @return float
 */
public function getValue();

/**
 * Get Price Amount object
 *
 * @return AmountInterface
 */
public function getAmount();

答案 1 :(得分:1)

我发现这很好用

<?php if ($block->hasSpecialPrice()): ?>
    <span class="special-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
            'display_label'     => __('Special Price'),
            'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
            'price_type'        => 'finalPrice',
            'include_container' => true,
            'schema' => $schema
        ]); ?>
    </span>
    <span class="old-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [
            'display_label'     => __('RRP'),
            'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
            'price_type'        => 'oldPrice',
            'include_container' => true,
            'skip_adjustments'  => true
        ]); ?>
    </span>
</span>
<br>
<span class="saving-price"><span class="saving-text"></span>
<?php
$wasPrice = $priceModel->getValue();
$nowPrice = $finalPriceModel->getValue();
$saving = $wasPrice - $nowPrice; 
$saving = number_format((float)($wasPrice - $nowPrice), 2, '.', '');
$savingpct = number_format((float)(100*(($wasPrice - $nowPrice)/$wasPrice)), 0);


  if ($nowPrice < $wasPrice){

    echo "You save: $ ".$saving. "<br />";
    echo $savingpct. "% Off";
  }
?>
</span>

答案 2 :(得分:0)

我在Magento 2上的类别和搜索结果页面上遇到了同样的问题。这是我最终想到的。希望能帮助到你! (注意:这一切都属于 foreach 循环,它循环遍历 $ _ productCollection 中的每个 $ _ product ):

$regprice = $_product->getPrice();
$specialprice = $_product->getSpecialPrice();
$yousave = number_format((float)($regprice - $specialprice), 2, '.', '');
$yousavepct = number_format((float)(100*(($regprice - $specialprice)/$regprice)), 0);

if($yousave > 0): ?>
    <p class="you-save-statement">You save: $<?php echo $yousave; ?> (<?php echo $yousavepct; ?>%)</p>
<?php endif; ?>