简体   繁体   English

Magento:从视图调用产品模型方法

[英]Magento: Call Product Model method from view

I've created a custom model to override core's Product Model. 我创建了一个自定义模型来覆盖核心的产品模型。

// app/code/local/Commerce121/Catalog/Model/Product.php

include('Mage/Catalog/Model/Product.php');

class Commerce121_Catalog_Model_Product extends Mage_Catalog_Model_Product
{

  public function getCompatibilityGrid()
  {
        return '<table width="100%"><tr><th>Year</th><th>Model</th><tr>Engine</tr></tr></table>';
  }

}

// app/code/local/Commerce121/Catalog/etc/config.xml
<?xml version="1.0"?>
<config>
    <modules>
       <Commerce121_catalog>
            <version>1.0</version>
       </Commerce121_catalog>
   </modules>
   <global>
      <models>
       <catalog>
         <rewrite>
           <product>Commerce121_Catalog_Model_Product</product>
         </rewrite>
        </catalog>
      </models>
   </global>
</config>

// app/etc/modules/Commerce121_Catalog.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Commerce121_catalog>
            <active>true</active>
            <codepool>local</codepool>
        </Commerce121_catalog>
    </modules>
</config>

In the admin the module is listed as enabled. 在管理员中,模块被列为已启用。

In app/design/frontend/.../default/template/catalog/product/view/view.phtml app/design/frontend/.../default/template/catalog/product/view/view.phtml

I added: 我补充说:

echo $_product->getCompatibilityGrid();

But nothing shows (this is the view, since echo 'bla' shows). 但是什么也没显示(这是视图,因为echo 'bla'显示了)。 Is $_product a reference to a controller? $ _product是对控制器的引用吗? If so, should I need to extend the controller as well? 如果是这样,我是否还需要扩展控制器?

Turs out Product model wasn't overriden. 未覆盖Turs out产品模型。 There was a part missing in the config.xml which declares the module: config.xml中缺少声明模块的部分:

<commerce121_catalog>
  <class>Commerce121_Catalog_Model</class>
</commerce121_catalog>

Should be like this: 应该是这样的:

<?xml version="1.0"?>
<config>
  <modules>
      <Commerce121_Catalog>
          <version>0.1.0</version>
      </Commerce121_Catalog>
  </modules>
  <global>
      <models>
        <commerce121_catalog>
            <class>Commerce121_Catalog_Model</class>
        </commerce121_catalog>
        <catalog>
          <rewrite>
            <product>Commerce121_Catalog_Model_Product</product>
          </rewrite>
        </catalog>
      </models>
  </global>
</config>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM