Magento: Call Product Model method from view - magento

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
I added:
echo $_product->getCompatibilityGrid();
But nothing shows (this is the view, since echo 'bla' shows). Is $_product a reference to a controller? If so, should I need to extend the controller as well?

Turs out Product model wasn't overriden. There was a part missing in the config.xml which declares the module:
<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>

Related

Extending Mage_Catalog_Block_Navigation

I'm new at Magento and stuggling with an issue that seems to me like it should be simple.
I want to override the Mage_Catalog_Block_Navigation class and I've created: app\code\local\Feno\Catalog\Block\Navigation.php that contains (snippet):
class Feno_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation
{
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '', $exclude_ids = array())
{
In app\code\local\Feno\Catalog\etc\config.xml I have:
<?xml version="1.0"?>
<config>
<modules>
<Feno_Catalog>
<version>1.0.1</version>
</Feno_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<navigation>Feno_Catalog_Block_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
</global>
</config>
However when I run the page, I get this error:
Invalid method Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml(Array ...
So it isn't even picking up my class..
I tried adding app\etc\modules\Feno_Catalog.xml with:
<?xml version="1.0"?>
<config>
<modules>
<Feno_Catalog>
<active>true</active>
<codePool>local</codePool>
</Feno_Catalog>
</modules>
</config>
But that doesn't work. Am I forgetting something?
As I said I'm new to Magento, so not sure where to look and Google and stackoverflow havent been able to help. FYI, I use PHPStorm so XML, etc are all valid (IDE checks)
You didn't add your new class name in your config.xml file, it should be,
<?xml version="1.0"?>
<config>
<modules>
<Feno_Catalog>
<version>0.0.0</version>
</Feno_Catalog>
</modules>
<global>
<helpers>
<catalog>
<class>Feno_Catalog_Helper</class>
</catalog>
</helpers>
<blocks>
<catalog>
<class>Feno_Catalog_Block</class>
</catalog>
<catalog>
<rewrite>
<navigation>Feno_Catalog_Block_Catalog_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
</global>
</config>
And you class in app/code/local/Feno/Catalog/Blcok/Catalog/Navigation.php
<?php
class Feno_Catalog_Block_Catalog_Navigation extends Mage_Catalog_Block_Navigation
{
}
And your helper file(it is optional)
app/code/local/Feno/Catalog/Helper/Data.php
<?php
class Feno_Catalog_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Update:
I forgot something to add. The module name you using Catalog is already used by magento core. So you need to change the module name or use <depends> ..</depends> tag. Otherwise module conflict may be happen or your module doesn't load. To use depends,
<Mage_Catalog>
<depends>
<Packagename_Modulename />
</depends>
</Mage_Catalog>
So your Feno_Catalog.xml (app/etc/modules) file should be,
<?xml version="1.0"?>
<config>
<modules>
<Feno_Catalog>
<active>true</active>
<codePool>local</codePool>
<version>0.0.0</version>
<depends>
<Mage_Catalog />
</depends>
</Feno_Catalog>
</modules>
</config>
I'm going to answer my own question here. I put it on hold after I didn't manage previous time and tried again today (cost me a couple of hours) and found it after searching and trying all sort of stuff.
The main issue appeared to be was that the block had to be registered in /app/etc/local.xml
...
<config>
<global>
<blocks>
<catalog>
<rewrite>
<navigation>Feno_Catalog_Block_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
<install>
...
After adding this it works perfect!
Found this short and sweet blog post: http://priyasmagento.blogspot.com/2010/07/override-navigation-block.html
Decided to add the blog post content also, the site doesn't seem very well maintained and would be a shame to lose the data.
© http://priyasmagento.blogspot.com/
Create mymodulepack folder in local folder.
Path: C:\wamp\www\triangeli\app\code\local\mymodulepack
Create catalog folder inside mymodulpack namespace.
Create block, etc, helper folder inside Catalog folder.
Create Navigation.php file in block folder. Override class and write functions inside this file.
<?php
class mymodulepack_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation
{
//write the function here,which add new functionality or override the original code
public function override()
{
//write the code here
}
}
Create config.xml file inside etc folder.
<?xml version="1.0" encoding="iso-8859-1"?>
<config>
<modules>
<mymodulepack_Catalog>
<version>0.1.0</version>
</mymodulepack_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<class>mymodulepack_Catalog_Block</class>
</rewrite>
</catalog>
</blocks>
<helpers>
<catalog>
<rewrite>
<class>mymodulepack_Catalog_Helper</class>
</rewrite>
</catalog>
</helpers>
</global>
</config>
Create Data.php file inside Helper folder.
<?php
class mymodulepack_Catalog_Helper_Data extends Mage_Core_Helper_Abstract
{ }
?>
To “activate” my new module “mymodulepack”:
In the file app\etc\local.xml reference mymodulepack under the global scope:
<config>
<global>
<blocks>
<catalog>
<rewrite>
<navigation>mymodulepack_Catalog_Block_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
<install>
<date><![CDATA[Thu, 17 Dec 2009 11:50:52 +0000]]></date>
</install>
<crypt>
<key><![CDATA[d89edae607842ce91b0e36456faed63e]]></key>
</crypt>
<disable_local_modules>false</disable_local_modules>
<resources>
<db>
<table_prefix><![CDATA[]]></table_prefix>
</db>
<default_setup>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[pizzaman]]></dbname>
<active>1</active>
</connection>
</default_setup>
</resources>
<session_save><![CDATA[files]]></session_save>
</global>
<admin>
<routers>
<adminhtml>
<args>
<frontName><![CDATA[admin]]></frontName>
</args>
</adminhtml>
</routers>
</admin>
</config>
Disable cache from admin section.

Magento - override Mage_Customer Address controller

I try to override Mage_Customer_AddressController, but my way doesn't work.
Used original documentation from magentocommerce.com and some blog posts (by Inchoo and Pfay) too.
Please explain me, what I'm doing wrong?
module config.xml:
<config>
<modules>
<companyname_General>
<version>1.0.5</version>
</Companyname_General>
</modules>
<global>
<helpers>
<Companyname_general>
<class>Companyname_General_Helper</class>
</Companyname_general>
</helpers>
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Companyname_General_Block_Catalog_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
<rewrite>
<companyname_general_customer_address>
<from><![CDATA[#^/customer/address/#]]></from>
<to>/general/customer_address</to>
</companyname_general_customer_address>
</rewrite>
</global>
<frontend>
<routers>
<customer>
<use>standard</use>
<agrs><companyname_general before="Mage_Customer">Companyname_General</companyname_general></agrs>
</customer>
</routers>
</frontend>
</config>
Companyname_General_AddressController:
<?php
require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AddressController.php');
class Companyname_General_Customer_AddressController extends Mage_Customer_AddressController
{
public function testAction()
{
die('works too!');
}
}
ps: Magento Enterprise 1.12
First of all issue in helper Companyname_general should be companyname_general
then helper Mage::helper('companyname_general')
<helpers>
<companyname_general>
<class>Companyname_General_Helper</class>
</companyname_general>
</helpers>
config.xml(Path Is app/code/local/Companyname/General/etc/) code is
<?xml version="1.0" ?>
<config>
<modules>
<Companyname_General>
<version>1.0.5</version>
</Companyname_General>
</modules>
<global>
<helpers>
<general>
<class>Companyname_General_Helper</class>
</general>
</helpers>
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Companyname_General_Block_Catalog_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
</global>
<frontend>
<routers>
<customer>
<args>
<modules>
<general before="Mage_Customer">Companyname_General</general>
</modules>
</args>
</customer>
</routers>
</frontend>
</config>
controllers AddressController.php(Path: app/code/local/Companyname/General/controllers )
code is
<?php
require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AddressController.php');
class Companyname_General_AddressController extends Mage_Customer_AddressController
{
/**
* Retrieve customer session object
*
* #return Mage_Customer_Model_Session
*/
protected function _getSession()
{
return Mage::getSingleton('customer/session');
}
public function preDispatch()
{
parent::preDispatch();
if (!Mage::getSingleton('customer/session')->authenticate($this)) {
$this->setFlag('', 'no-dispatch', true);
}
}
public function testAction()
{
die('works too!');
}
}
Module file is Companyname_General.xml path is app/etc/modules/
code are
<?xml version="1.0" ?>
<config>
<modules>
<Companyname_General>
<active>true</active>
<codePool>local</codePool>
</Companyname_General>
</modules>
</config>

Override Topmenu.php in magento

I'm trying to override the _getHtml function in Mage_Page_Block_Html_Topmenu magento class
I've created a new module in app/cod/local/MyModulo/Opage
My congig.xml is:
<config>
<modules>
<MyModulo_Opage>
<version>1.0.0</version>
</MyModulo_Opage>
</modules>
<global>
<blocks>
<page>
<rewrite>
<html_topmenu>MyModulo_Opage_Block_Html_Topmenu</html_topmenu>
</rewrite>
</page>
</blocks>
</global>
My class is:
But when I test it, the category top menu doesn't appear. What I'm doing wrong?
Try below
<?xml version="1.0"?>
<config>
<modules>
<MyModulo_Opage>
<version>1.0.0</version>
</MyModulo_Opage>
</modules>
<global>
<blocks>
<page>
<rewrite>
<html_topmenu>MyModulo_Opage_Block_Page_Html_Topmenu</html_topmenu>
</rewrite>
</page>
</blocks>
</global>
</config>
Call rewrite block class path app>code>local>MyModulo>Opage>Block>Page>Html>Topmenu.php
<?php
class MyModulo_Opage_Block_Page_Html_Topmenu extends Mage_Page_Block_Html_Topmenu
{
}

magento rewrite catalog block Product/View/Options/Type/Select.php

i want to rewrite the block file : /app/code/core/Mage/Catalog/Block/Product/View/Options/Type/Select.php, below is my steps, but it is not working :
ScreentShot: http://imm.io/J36p
Code: http://www.heypasteit.com/clip/0JJ8
who know what the problem is ?
In the linked code (below for posterity), the path global/blocks/catalog/rewrite/Product_View_Options_Select
should be
global/blocks/catalog/rewrite/product_view_options_select, because the block class is specified using lowercase in layout XML files (eg. https://github.com/benmarks/magento-mirror/blob/1.7.0.2/app/design/frontend/base/default/layout/catalog.xml#L228).
/app/code/local/Lbb/Catalog/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Lbb_Catalog>
<version>0.1.0</version>
</Lbb_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_view_options_type_select>Lbb_Catalog_Block_Product_View_Options_Type_Select</product_view_options_type_select>
</rewrite>
</catalog>
</blocks>
</global>
</config>
I get this code from a working example, so it should work
/app/code/local/RWS/CustomOptions/Options/Type/Select.php
<?php
class RWS_CustomOptions_Options_Type_Select extends Mage_Catalog_Block_Product_View_Options_Abstract
{
public function getValuesHtml()
{
.....
/app/code/local/RWS/CustomOptions/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<RWS_CustomOptions>
<version>0.1.0</version>
</RWS_CustomOptions>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_view_options_type_select>RWS_CustomOptions_Options_Type_Select</product_view_options_type_select>
</rewrite>
</catalog>
</blocks>
</global>
</config>
/app/etc/modules/RWS_CustomOptions.xml
<?xml version="1.0"?>
<config>
<modules>
<RWS_CustomOptions>
<active>true</active>
<codePool>local</codePool>
</RWS_CustomOptions>
</modules>
<config>
To test, add a (simple) product in magento admin with a custom select option, the go product detail/view page.

I want to know why my Magento Module with only controllers is not working

This is my activation file,
in app/etc/modules/My_Test.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_Test>
<active>true</active>
<codePool>local</codePool>
</My_Test>
</modules>
</config>
This is my config file in app/code/local/MY/Test/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<helpers>
<my_test>
<class>My_Test_Helper</class>
</my_test>
</helpers>
<models>
<mytest>
<class>My_Test_Model</class>
</my_test>
</models>
<blocks>
<my_test>
<class>My_Test_Block</class>
</my_test>
</blocks>
</global>
<frontend>
<routers>
<my_test>
<use>standard</use>
<args>
<module>My_Test</module>
<frontName>test</frontName>
</args>
</my_test>
</routers>
</frontend>
</config>
This is my controllers code
in app/core/local/My/Test/controllers/IndexController.php
<?php
class My_Test_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo "This is the Module MyTest";
}
}
Now when i am giving the url
176.58.99.99/herbal/index.php/test/index/index
176.58.99.99/herbal/index.php/test/
176.58.99.99/herbal/test/index/index
176.58.99.99/herbal/test/
Its showing 404 error
You say your controller is located in app/core/local/My/Test/controllers/IndexController.php while it should be placed as followed: app/code/local/My/Test/controllers/IndexController.php
I had the same problem, but in my case it was about file permissions. Make sure that your files and directories have the proper access permissions, if not, magento will never be able to read those files.

Resources