1

我在 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

4

2 回答 2

3

问题出在这里

<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()像这样访问方法。

  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类。希望这将帮助您了解代码中的错误。

于 2014-07-26T06:23:01.610 回答
0

我通过这种方式整理,

已创建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>

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

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

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

于 2014-07-25T09:57:23.193 回答