【问题标题】:overriding price model in magentoMagento中压倒一切的价格模型
【发布时间】:2014-07-25 06:54:30
【问题描述】:

我在 Magento 中覆盖 product-> type -> price.php 模型时遇到问题。

这是我的app/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <softweb_catalog>
        <active>true</active>
        <codepool>local</codepool>
    </softweb_catalog>
  </modules>
</config>

这里是

app/code/local/Softweb/Catalog/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
    <softweb_catalog>
        <version>0.1</version>
    </softweb_catalog>
</modules>
<global>
    <models>
        <catalog>
            <class>Softweb_Catalog_Model</class>
        </catalog>
        <catalog>
            <rewrite>
                <product_type_price>Softweb_Catalog_Model_Product_Type_Price</product_type_price>
            </rewrite>
        </catalog>
    </models>
</global>
</config>

现在 Price.php 路径是app/code/local/Softweb/Catalog/Model/Product/Type/Price.php

 class Softweb_Catalog_Model_Product_Type_Price extends Mage_Catalog_Model_Product_Type_Price
 {

   public function getPrice($product)
   {
      die('function called');
   }

  }

我不知道我错过了什么......

PS 我使用的是 magento 1.9.0.1

【问题讨论】:

标签: php magento magento-1.9


【解决方案1】:

问题出在这里

<models>
     <catalog>
          <class>Softweb_Catalog_Model</class>
     </catalog>
....
</models>

在这里您正在定义您的模型。 catalog 是您为模块模型提供的参考。假设您在模块的模型目录中有一个文件Foo.php。那就是

Softweb
|
 ----------Catalog
           |
            ----------- etc
           |             |
           |              ---------- config.xml
           |
            ----------- Model
                        |
                         ----------- Foo.php

假设您的 Foo.php 拥有一个方法 Foo() 方法。

<?php
class Softweb_Catalog_Model_Foo extends Mage_Core_Model_Abstract
{
      public function Foo()
      {
           //some foo codes here
           //returns something
      }
}

如何获得在模型中定义的 Foo() 方法?根据您的模型定义,它应该是这样的

 $foo = Mage::getModel('catalog/foo')->Foo();

但是,您的模型参考应该是唯一的。所以你不能使用catalog 作为你的模型参考。因为它已经在 Mage_Catalog 核心模块中使用。看到这个

Location:app/code/core/Mage/Catalog/etc/config.xml
<global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class>
                <resourceModel>catalog_resource</resourceModel>
            </catalog>
            ---------
        </models>
        --------
</global>

因此,如果您需要使用您的模型,您应该为您的模型提供一个唯一的参考。那是

    <models>
        <softweb_catalog>
            <class>Softweb_Catalog_Model</class>
        </softweb_catalog>
        <catalog>
            <rewrite>
                <product_type_price>Softweb_Catalog_Model_Product_Type_Price</product_type_price>
            </rewrite>
        </catalog>
    </models>

在这里,您的模块使用softweb_catalog 引用并且是唯一的。所以现在你可以像这样访问Foo()method。

  Mage::getModel('softweb_catalog/foo')->Foo();

它还会重写您所需的模型文件。但是由于您的模块不包含任何模型文件,因此不需要此代码。你只需要这个。

<global>
     <models>
       <catalog>
          <rewrite>
             <product_type_price>Softweb_ConstPrice_Model_Price</product_type_price>
          </rewrite>
       </catalog>
     </models>
  </global>

它将允许您重写Mage_Catalog_Model_Product_Type_Price 类。希望这将帮助您了解代码中的错误。

【讨论】:

  • 谢谢你的努力
  • 我想你理解这个错误。很高兴为您提供帮助
  • 接受您的回答不是问题。但是您问题的实际答案存在于我的答案中。如果您认为我是正确的,您可以通过对您自己的答案发表评论来指出我的答案,或者接受我的正确答案。这是因为,搜索此答案的人可能会错过我的答案。 :)
  • 现在接受了你的,但如果有人需要从头开始,那么我的解决方案将从那时起提供帮助,,,:)
  • 没有人寻找完整的答案。当他们遇到与您完全相同的困惑时,他们会遇到这个问题。所以这个答案清楚地说明了这个疑问。
【解决方案2】:

我就是通过这种方式整理出来的,

创建app/etc/Softweb_All.xml

<?xml version="1.0"?>
<config>
<modules>
<Softweb_ConstPrice>
  <active>true</active>
  <codePool>local</codePool>
</Softweb_ConstPrice>

然后app/code/local/Softweb/ConstPrice/etc/config.xml

<?xml version="1.0" encoding="utf-8"?>
 <config>
   <modules>
     <Softweb_ConstPrice>
        <version>0.1.0</version>
     </Softweb_ConstPrice>
   </modules>
   <global>
     <models>
       <catalog>
          <rewrite>
             <product_type_price>Softweb_ConstPrice_Model_Price</product_type_price>
          </rewrite>
       </catalog>
     </models>
  </global>
</config>

然后app/code/local/Softweb/ConstPrice/Model/Price.php下的Price.php

     class Softweb_ConstPrice_Model_Price extends Mage_Catalog_Model_Product_Type_Price
     {
         public function getPrice($product)
         {
             return 50.00;
         }
     }

不知道为什么我的问题代码不起作用。 :(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2021-08-25
    • 2015-09-24
    • 1970-01-01
    相关资源
    最近更新 更多