【问题标题】:Magento Bundles: Options' displayed price calculated from default choice price, not from zeroMagento Bundles:期权的显示价格从默认选择价格计算,而不是从零开始
【发布时间】:2011-08-24 12:08:47
【问题描述】:

产品 myBundle 具有带有以下项目的 myColorOption:

  • 绿色 $50
  • 蓝色 $100
  • 红色 100 美元
  • 黑色 $150

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

  • 绿色 +$50
  • 蓝色 +$100
  • 红色 +$100
  • 黑色 +$150

我正在寻找的更改是管理员选择了默认项目。如果是,则每个显示的价格都应与默认选项的价格相关。如果管理员将蓝色(价格 100 美元)设置为选项的默认项目,下拉列表现在应显示为:

  • 绿色 -$50
  • 蓝色
  • 红色
  • 黑色+$50

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


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

<?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>':'');
    }
}

【问题讨论】:

    标签: php magento magento-1.4


    【解决方案1】:

    使用此代码

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

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

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

    【讨论】:

    • 出于某种原因 ->getSelectionPriceValue() 每次返回 0.000,但 ->getPrice() 工作正常!非常感谢您的回答,非常感谢!
    • 嗨,由于某种原因,这对我来说无法正常工作。我添加了代码,但它一直显示相同的价格,即绿色 50 美元 蓝色 100 美元 红色 100 美元 黑色 150 美元 我错过了什么?谢谢
    【解决方案2】:

    只是为了在这个答案中添加一些内容 - 我发现如果代码尝试针对没有默认值的选项调用 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() 是否返回了某些内容,然后继续设置默认价格,否则它只是继续执行其余代码。

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      相关资源
      最近更新 更多