我正在寻找一种特定产品的定价表(在首页),但有 3 种不同(预定义)数量。

在页面上显示产品并添加正确的添加到购物车链接相对容易:

$_productId = '498';
$_product   = Mage::getModel('catalog/product')->load($_productId);
$_url       = Mage::helper('checkout/cart')->getAddUrl($_product);

获取第 1 批价格(包括税费、货币和一些格式)也很容易:

$_price = Mage::helper('core')->currency(Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice()));

这导致 <span class="price">€15,00</span>

但是,我的目标是拥有一个如下的定价表:

┌───────────┬───────────┬───────────┐
│  Single   │ ValuePack │ FamilyPack│
├───────────┼───────────┼───────────┤
│   €15     │    €10    │    €7.50  │
├───────────┼───────────┼───────────┤
│  Qty 1    │   Qty 5   │   Qty 10  │ 
└───────────┴───────────┴───────────┘ 

获得数量 * X 价格也不难:

$_qty = 5; 
$_price = Mage::helper('core')->currency(Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice($_qty)));

正如预期的那样,这会导致 <span class="price">€10,00</span>

但是,即使我更改 $_qty,其他 2 个价格的输出始终与第一个相同。

插图:

$_price_1 = Mage::helper('core .... $_product->getFinalPrice(1))); 结果是 €15
$_price_2 = Mage::helper('core .... $_product->getFinalPrice(5))); 结果是 €15 还有
$_price_3 = Mage::helper('core .... $_product->getFinalPrice(10)); 结果是 €15 还有

我假设这与 Magento 在第一次查找后缓存产品价格有关。

有人知道如何以正确的方式创建此表吗?

有帮助吗?

解决方案

事实证明,获得分级定价也不是那么难,因为它可以在数组中使用 $_product->getFormatedTierPrice() (注意拼写错误!)。

这个数组看起来像这样:

Array
(
    [32000-2] => Array
        (
            [price_id] => 50
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 21.95
            [price_qty] => 2.0000
            [website_price] => 24.95
            [formated_price] => € 18,95
        )

    [32000-4] => Array
        (
            [price_id] => 52
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 23.95
            [price_qty] => 4.0000
            [website_price] => 23.95
            [formated_price] => € 17,95
        )

    [32000-10] => Array
        (
            [price_id] => 56
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 3200024.95
            [price] => 22.95
            [price_qty] => 10.0000
            [website_price] => 22.95
            [formated_price] => € 16,95
        )

    [32000-25] => Array
        (
            [price_id] => 57
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 19.95
            [price_qty] => 25.0000
            [website_price] => 21.95
            [formated_price] => € 15,95
        )

    [32000-50] => Array
        (
            [price_id] => 58
            [website_id] => 4
            [all_groups] => 1
            [cust_group] => 32000
            [price] => 19.95
            [price_qty] => 50.0000
            [website_price] => 19.95
            [formated_price] => € 14,95
        )

)

我只需要这个多维数组中的 2 个项目:[价格数量] 和 [格式化价格]。

所以我最终得到了以下代码:

$_productId = '498';
$_product   = Mage::getModel('catalog/product')->load($_productId);
$_single = Mage::helper('core')->currency(Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice())); // Just the single product price
$_prices = array_column($_product->getFormatedTierPrice(), 'formated_price', 'price_qty'); // Extract price_qty and formated_price from Array of formatted tier prices

在我的定价表中现在有:

<li class="price">
  <?php 
    $_qty = '10.0000'; 
    if (array_key_exists($_qty, $_prices)) { 
      echo $_prices[$_qty]; 
    } else { 
    echo $_single; 
    } 
  ?>
</li>

else { echo $_single;} 我确保如果有人在后端更改了层级数量(层级数量当前在我的 phtml 中硬编码),我至少会输出单个产品价格,而不是根本不输出任何价格。

许可以下: CC-BY-SA归因
scroll top