如何显示Magento 2中保存的金额?

ie3xauqp  于 2022-11-12  发布在  其他
关注(0)|答案(3)|浏览(150)

我正在尝试显示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>
5lhxktic

5lhxktic1#

而不是使用

$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();
wbgh16ku

wbgh16ku2#

我发现这个很好用

<?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>
4xy9mtcn

4xy9mtcn3#

我在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; ?>

相关问题