我正在尝试显示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>
而不是使用
$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();
我发现运行良好
<?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>
我在Magento 2的分类和搜索结果页面上遇到了同样的问题。以下是我最终发现的。希望它能有所帮助!(注意:这一切都属于foreach循环,该循环遍历$_productCollection中的每个$0_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; ?>