2017-07-25 81 views
2

我想顯示客戶保存的實際價格而不是百分比值,但是我不是PHP專家,並且對如何解決此問題存在困難。Magento 1.9 tierclricing calculation

目前,我的商店頁面上顯示了分層價格顯示,如下所示。

<!-- Display product tier price --> 
<?php 
    $_product = $this->getProduct(); 
    $_tierPrices = $this->getTierPrices(); 
    if (count($_tierPrices) > 0): 
     $_data = array(); 
     $_prevQty = 0; 
     $_counter = 0; 
     $_tierPrices = array_reverse($_tierPrices); 
     foreach ($_tierPrices as $_index => $_price){ 
      $_counter++; 
       $label = $_price['price_qty']; 
       $_data[] = array('prev'=>$_prevQty,'next'=>$_price['price_qty'],'label'=>$label,'price'=>$_price['formated_price'],'save'=>$_price['savePercent']); 
       $_prevQty = $_price['price_qty']; 
     } 
     $_data = array_reverse($_data); ?> 
     <table class="price-table" id="price-tier"> 
      <tbody> 
       <tr> 
        <th>Antal</th> 
        <th>Pris</th> 
        <th>Spar</th> 
       </tr> 
      <?php foreach ($_data as $_row): ?> 
       <tr> 
        <td><?php echo $_row['label']; ?></td> 
        <td><?php echo $_row['price']; ?></td> 
        <td><?php echo $_row['save']."%"; ?></td> 
       </tr> 
      <?php endforeach; ?> 
      </tbody> 
     </table> 
<?php 
    endif; ?> 

我希望有人在那裏可以指出我在正確的方向。

回答

0

,同時通過分級定價的循環,你可以工作,通過$_product->getFinalPrice() 扣除$_price['price']看我下面的修改foreach循環,節約了ammount的,你可以貼在你的現有代碼

foreach ($_tierPrices as $_index => $_price){ 
       $_counter++; 
        $label = $_price['price_qty']; 
        $_data[] = array('prev'=>$_prevQty,'next'=>$_price['price_qty'],'label'=>$label,'price'=>$_price['formated_price'],'save'=>$_product->getFinalPrice()-$_price['price']); 
        $_prevQty = $_price['price_qty']; 
      } 

要延長此,$this->getProduct()返回Mage_Catalog_Model_Product的實例,我們可以使用getFinalPrice()來獲得產品的最終價格。

$this->getTierPrices()返回分層價格數據的多維陣列,其中包含稍微不同的數據比如果直接從產品對象獲取的分層定價,下面是陣列

array(3) { 
    [0]=> 
    array(10) { 
    ["price_id"]=> 
    string(1) "1" 
    ["website_id"]=> 
    string(1) "1" 
    ["all_groups"]=> 
    string(1) "0" 
    ["cust_group"]=> 
    string(1) "0" 
    ["price"]=> 
    float(341.39) 
    ["price_qty"]=> 
    float(5) 
    ["website_price"]=> 
    float(341.39) 
    ["formated_price"]=> 
    string(47) "£341.39" 
    ["savePercent"]=> 
    float(3) 
    ["formated_price_incl_tax"]=> 
    string(47) "£341.39" 
    } 
    [1]=> 
    array(10){ 
    ... 
    } 
    ... 
} 

內的一個項目的例子全碼:

<!-- Display product tier price --> 
<?php 
    $_product = $this->getProduct(); 
    $_tierPrices = $this->getTierPrices(); 
    if (count($_tierPrices) > 0): 
     $_data = array(); 
     $_prevQty = 0; 
     $_counter = 0; 
     $_tierPrices = array_reverse($_tierPrices); 
     foreach ($_tierPrices as $_index => $_price){ 
      $_counter++; 
       $label = $_price['price_qty']; 
       $_data[] = array('prev'=>$_prevQty,'next'=>$_price['price_qty'],'label'=>$label,'price'=>$_price['formated_price'],'save'=>$_product->getFinalPrice()-$_price['price']); 
       $_prevQty = $_price['price_qty']; 
     } 
     $_data = array_reverse($_data); ?> 
     <table class="price-table" id="price-tier"> 
      <tbody> 
       <tr> 
        <th>Antal</th> 
        <th>Pris</th> 
        <th>Spar</th> 
       </tr> 
      <?php foreach ($_data as $_row): ?> 
       <tr> 
        <td><?php echo $_row['label']; ?></td> 
        <td><?php echo $_row['price']; ?></td> 
        <td><?php echo $_row['save']; ?></td> 
       </tr> 
      <?php endforeach; ?> 
      </tbody> 
     </table> 
<?php 
    endif; ?> 
+0

伊夫嘗試了上面的代碼,但我仍然得到的百分比值。這可能是因爲我在表代中迴應了錯誤的值? – MrB

+0

我會把你現在需要的完整的代碼片段發給你 –

+0

請看我更新的答案,我還需要在echo $ _row ['save']後刪除'%'字符串;基本上我們所做的是將$ _row ['save']設置爲原始價格減去分層價格,並刪除了此值後出現的百分比符號。 –