【问题标题】:Unable to call custom methods within extended Magento model无法在扩展 Magento 模型中调用自定义方法
【发布时间】:2014-04-04 01:36:53
【问题描述】:

我正在尝试自定义 Magento CE 1.6.0.0 中定价/分层定价的显示方式。

我已按照以下链接第二篇文章中的说明覆盖 Mage_Catalog_Model_Product_Type_Price

http://www.magentocommerce.com/boards/viewthread/16829/

以下是我的自定义模型类:

class PHC_Price_Model_Price extends Mage_Catalog_Model_Product_Type_Price {
    public function getPrice() {
        echo "overridden getPrice method called<br>";
    }

    public function getPHCDisplayPrice($product) {
        echo "custom price function called<br>";
    }
}

我能够成功地从我的模板文件中调用被覆盖的 getPrice() 函数,如下所示:

$product = Mage::getModel("catalog/product")->load($_product->entity_id);
$displayPrice = $product->getPrice();

但是,当我尝试使用

调用我的自定义价格函数时
$product = Mage::getModel("catalog/product")->load($_product->entity_id);
$displayPrice = $product->getPHCDisplayPrice();

我一无所获。谁能告诉我我错过了什么?

【问题讨论】:

    标签: magento overriding


    【解决方案1】:

    你没有得到结果是不正常的。如果这行得通,我会感到惊讶。

    您正在覆盖类Mage_Catalog_Model_Product_Type_Price,但在您的示例中,$product 变量是Mage_Catalog_Model_Product 的一个实例。该类没有getPHCDisplayPrice 方法,它调用__call 方法并返回null。

    不小心调用getPrice 会得到预期的结果。这是因为Mage_Catalog_Model_Product 中的getPrice 方法看起来像这样:

    public function getPrice()
    {
        if ($this->_calculatePrice || !$this->getData('price')) {
            return $this->getPriceModel()->getPrice($this);
        } else {
            return $this->getData('price');
        }
    }
    

    所以当你调用它时,它会调用$this-&gt;getPriceModel()-&gt;getPrice($this) 并且$this-&gt;getPriceModel() 返回你的类的一个实例。

    【讨论】:

    • 感谢您的解释 - 很高兴了解正在发生的事情。我能够通过扩展 Mage_Catalog_Model_Product 来完成我需要的工作。
    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 2019-05-10
    • 2012-09-10
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多