2016-03-25 30 views
1

ile kaydedilen miktarı nasıl gösterebilirim Magento 2.0.2 web sitesi için katalog sayfasında kayıtlı miktarı görüntülemeye çalışıyorum. Ancak hesaplama sonucu görüntülenmiyor. Sadece boş bir yer buldum. Tema dosyasımda final_price.phtml dosyasını düzenliyorum. Lütfen bana neyin yanlış olduğunu bildir. Çoğu sonuç magento 1 ile ilgili olduğundan ve kodlar hata attığından, Google'da herhangi bir bilgi bulamadım. Hesaplamaları yapmaya çalıştığım bölümde kodumun nasıl göründüğü budur. Tavsiye takdir edildi. TeşekkürMagento 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> 

cevap

0

Ben Magento 2. kategori ve arama sonuçları sayfalarında aynı konuda koştu İşte sonunda anladım zaten. Umarım yardımcı olur! (Not: Bu bütün her $ _productCollection içinde $ _product dolaşır foreach döngü içine düşen):

$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

yerine

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

kullanarak kullanmanız gereken

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

$ fiyatModel ve $ finalPriceMode Ben final_price.phtml

'daki 17 numaralı satırda gördüğünüz gibi, daha sonra da PriceInterface.php'nin içinde, getValue() öğesi yalnızca bir sayı döndürürken getAmount() öğesinin bir nesne döndürdüğünü göreceğimiz /** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */'a bakın.

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

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

bunu iyi

<?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> 
çalışır bulundu