2016-03-25 6 views
1

magento 2.0.2ウェブサイトのカタログページに保存された金額を表示しようとしています。しかし、私は計算結果を表示していません。私はちょうど空のスペースを取得します。私は私のテーマファイルのfinal_price.phtmlを編集しています。何が間違っているかアドバイスしてください。ほとんどの結果がmagento 1に関連しており、コードがエラーを投げているので、私はgoogleで情報を見つけられませんでした。これは私のコードが私が計算をしようとしているセクションでのように見えるものです。アドバイスありがとうございます。私は、Magentoの2のカテゴリと、検索結果ページに同じ問題に遭遇したおかげMagento 2で保存された金額を表示する方法

<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> 

答えて

0

は、ここで私は最終的に考え出したものです。それが役に立てば幸い! (注:このすべては、各$ _productCollection内の$ _productを反復処理のforeachループ内にある):

$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; ?> 
0

の代わりに

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

を使用して、あなたが使用する必要があります

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

$ priceModelと$ finalPriceMode私はfinal_price.phtmlの17行目にあるように/** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */を参照しています。

PriceInterface.phpの中でgetAmount()はオブジェクトを返し、getValue()は数値を返します。

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

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

私はこれがうまく

<?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> 
を働く見つけ
関連する問題