Magento 1.9 分层定价计算

Magento 1.9 tierpricing calculation

我正在尝试显示客户节省的实际价格而不是百分比值,但我不是 php 专家,对如何解决这个问题有些困惑。

目前,我的商店页面上有一个分层价格显示,如下所示。

<!-- 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; ?>

我希望有人能给我指出正确的方向。

循环分层定价时,您可以通过从 $_product->getFinalPrice() 中减去 $_price['price'] 来计算节省的金额 请参阅下面我修改过的 foreach 循环,您可以将其粘贴到现有代码上

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() returns Mage_Catalog_Model_Product 的一个实例,我们可以在其中使用 getFinalPrice() 来获取产品的最终价格。

$this->getTierPrices() returns 分层价格数据的多维数组,其中包含的数据与直接从产品对象获取分层定价时的数据略有不同,下面是其中一个项目的示例数组

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