Magento捆绑包:选项'显示价格从默认选择价格计算,而不是从零开始


Magento Bundles: Options' displayed price calculated from default choice price, not from zero

产品myBundle有myColorOption和这些项目:

    <
  • 绿色50美元/gh><
  • 蓝色100美元/gh><
  • 红100美元/gh><
  • 黑色150美元/gh>

Magento 1.4.2.0默认情况下会向客户显示一个选择下拉菜单,其中包含如下选项:

    <
  • 绿色+ 50美元/gh><
  • 蓝色+ 100美元/gh><
  • 红+ 100美元/gh><
  • 黑色+ 150美元/gh>

我正在寻找的变化是当一个默认的项目已被管理员选择。当它是,每个显示的价格应该相对于默认选项的价格。如果admin设置Blue (price $100)作为该选项的默认项,那么下拉菜单现在应该是:

    <
  • 绿色- 50美元/gh>蓝色
  • 红色
  • <
  • 黑色+ 50美元/gh>

澄清一下:我只想改变下拉菜单中显示的价格,添加到购物车中并用于其他计算的实际价格保持不变。


Update:这是我到目前为止的代码,问题是在注释行。我需要帮助得到正确的模型等。

<?php
// From file app/code/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle/Option.php
// copied to app/code/local/Mage/...
public function getSelectionTitlePrice($_selection, $includeContainer = true)
{
    $defaultPrice = 0.00;
    $_product = $this->getProduct();
    /*
    $_mbmo = new Mage_Bundle_Model_Option();
    $_mbmo->load($_selection->getProductId());
    $_default = $_mbmo->getDefaultSelection();
    $defaultPrice = $_product->getPriceModel()->getSelectionPreFinalPrice($_product, $_default, 1);
    */
    $price = $_product->getPriceModel()->getSelectionPreFinalPrice($_product, $_selection, 1);
    if ($price == $defaultPrice)
    {
        return $_selection->getName();
    }
    else
    {
        $sign = ($price < $defaultPrice) ?  '-' : '+';
        $diff = ($price < $defaultPrice) ? $defaultPrice - $price : $price - $defaultPrice;
        return $_selection->getName() . ' &nbsp; ' .
            ($includeContainer ? '<span class="price-notice">':'') . $sign .
            $this->formatPriceString($diff, $includeContainer) . ($includeContainer ? '</span>':'');
    }
}

使用此代码

$defaultPrice = $_product->getPriceModel()->getSelectionPreFinalPrice($_product, $_default,1);

将上面一行替换为下面一行

$defaultPrice=$this->getOption()->getDefaultSelection()->getSelectionPriceValue();

只是为这个答案添加一些东西-我发现如果代码试图对没有默认值的选项调用getDefaultSelection()->getPrice(),则会失败。可以通过添加以下代码来修复此问题:

$_mbmo = new Mage_Bundle_Model_Option();
$_mbmo->load($_selection->getProductId());
$_default = $_mbmo->getDefaultSelection();
if (gettype($this->getOption()->getDefaultSelection())==object){
$defaultPrice=$this->getOption()->getDefaultSelection()->getPrice();
}

基本上只是检查$this上调用getDefaultSelection()返回的内容,然后继续设置默认价格,否则它只是继续执行其余的代码。