Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Add a class to an existing module
 
egabbud
Jr. Member
 
Total Posts:  10
Joined:  2008-07-16
 

Hi,

I need to add a new class to the Catalog module.

I know how to rewrite an existing class in config.xml using something like:

<global>
        <
models>
           <
catalog>
                   <
rewrite>
                    <
category>MyCompany_Catalog_Model_Category</category>
                   </
rewrite>
           </
catalog>
        </
models>
    </global>

But what if I want to add a new class? Let’s say MyCompany_Catalog_Model_Category_myNewFolder_myNewClass.

I tried the following to redeclare the module as being in my local pool:

<catalog
   <class>
MyCompany_Catalog_Model</class> 
</
catalog>
But then it expects to find all the files in my local pool, meaning I should overwrite every existing file of that module -> lot of work.

Trying something more subtle like:

<catalog_category_myNewFolder
   <class> 
MyCompany_Catalog_Model_Category_myNewFolder_myNewClass </class> 
</
catalog_category_myNewFolder>
doesn’t work neither.

What is the general approach to just add a file/class to an existing module in Magento?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Arhwen
Jr. Member
 
Total Posts:  9
Joined:  2008-11-23
 

for example,you add a new class MyCompany_Catalog_Nav, to enable this class,you should edit the app/etc/local.xml:

put this in the Global tag: 

<blocks>
            <
catalog>
               <
rewrite>
                  <
nav>MyCompany_Catalog_Block_Nav</nav>
                </
rewrite>
             </
catalog>
  </
blocks>
 
Magento Community Magento Community
Magento Community
Magento Community
 
gMike
Jr. Member
 
Total Posts:  4
Joined:  2009-03-05
 

It’s not working if you edit the app/etc/local.xml. And egabbud want to add a new class to an existing module, not to rewrite an existing one.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MA_19
Member
 
Total Posts:  41
Joined:  2009-01-21
 

Of course if you don’t mind modifying the core, you can just create your new class file in the catalog module folder and define it in the module’s config.xml. you would also have to modify the existing catalog classes to use your new class.

I’m pretty sure this isn’t what you want to do.

Are you trying to “extend” the catalog module to add your own functionality? If this is the case, then you need to create a new module for your changes with a rewrite rule to catch the catalog module class where you want to insert your own functionality. Then in your own class start using the new class that you have in your module folder. They will have to be defined in your modules locale.xml:

<global>
                <
models>
                                <
yourmodulename_classname>
                                <class>
your CompanyName_yourModuleName_Model_className</class>
                                </
yourmodulename_classname>
                </
models>
</global>

Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
gMike
Jr. Member
 
Total Posts:  4
Joined:  2009-03-05
 

I will explain the problem:
I’ve created a new API for categories (not extending existing one). I’ve added posibility to link categories like associated/grouped products. Everithing works great inside the Mage core, but if i want to move this part to app/code/local/company_name it’s not working anymore.

Refferences for this API in api.xml inside the core are:

<config>
    <
api>
        <
resources>
 .............................................................................................................
            <
catalog_category_link translate="title" module="catalog">
                <
title>Category links API (related)</title>
                <
model>catalog/category_link_api</model>
                <
acl>catalog/category/link</acl>
                <
methods>
                    <list 
translate="title" module="catalog">
                        <
title>Retrieve linked category</title>
                        <
method>items</method>
                    </list>
                    <
assign translate="title" module="catalog">
                        <
title>Assign category link</title>
                        <
acl>catalog/category/link/assign</acl>
                    </
assign>
                    <
remove translate="title" module="catalog">
                        <
title>Remove category link</title>
                        <
acl>catalog/category/link/remove</acl>
                    </
remove>
                </
methods>
                <
faults module="catalog">
                    <
type_not_exists>
                        <
code>100</code>
                        <
message>Given invalid link type.</message>
                    </
type_not_exists>
                    <
not_exists>
                        <
code>101</code>
                        <
message>Category not exists.</message>
                    </
not_exists>
                    <
data_invalid>
                        <
code>102</code>
                        <
message>Invalid data givenDetails in error message.</message>
                    </
data_invalid>
                    <
not_removed>
                        <
code>104</code>
                        <
message>Category link not removed.</message>
                    </
not_removed>
                </
faults>
            </
catalog_category_link>
        </
resources>
        <
resources_alias>
 .............................................................................................................
            <
category_link>catalog_category_link</category_link>
 .............................................................................................................
        </
resources_alias>
        <
acl>
            <
resources>
                <
catalog translate="title" module="catalog">
                    <
title>Catalog</title>
                    <
sort_order>1</sort_order>
                    <
category translate="title" module="catalog">
                        <
title>Category</title>
 .............................................................................................................
                        <
link translate="title" module="catalog">
                            <
title>Link (Related)</title>
                            <
sort_order>101</sort_order>
                            <
assign translate="title" module="catalog">
                                <
title>Assign</title>
                            </
assign>
                            <
remove translate="title" module="catalog">
                                <
title>Remove</title>
                            </
remove>
                        </
link>
 .............................................................................................................
                    </
category>

                </
catalog>
            </
resources>
        </
acl>
    </
api>
</
config>

and in config.xml:

<config>
 .............................................................................................................
    <global>
        <
models>
 .............................................................................................................
           <
catalog_resource_eav_mysql4>
                <
entities>
 .............................................................................................................
                    <
category_link>
                        <
table>catalog_category_link</table>
                    </
category_link>
                    <
category_link_type>
                        <
table>catalog_category_link_type</table>
                    </
category_link_type>
                    <
category_link_attribute>
                        <
table>catalog_category_link_attribute</table>
                    </
category_link_attribute>
                    <
category_link_attribute_decimal>
                        <
table>catalog_category_link_attribute_decimal</table>
                    </
category_link_attribute_decimal>
                    <
category_link_attribute_int>
                        <
table>catalog_category_link_attribute_int</table>
                    </
category_link_attribute_int>
                    <
category_link_attribute_varchar>
                        <
table>catalog_category_link_attribute_varchar</table>
                    </
category_link_attribute_varchar>
 ............................................................................................
                </
entities>  
            </
catalog_resource_eav_mysql4>
        </
models>
    </global>
</
config>
Already i’ve created a module in app/code/local/company_name for catalog witch overide Magento’s original API for category (and this works).
If i move everithing from code/core to code/local (also the references from XML files - config.xml and api.xml), magento is able to find the new API but searching for classes inside core.
 
Magento Community Magento Community
Magento Community
Magento Community
 
gMike
Jr. Member
 
Total Posts:  4
Joined:  2009-03-05
 

I will put config.xml and api.xml used in /app/local/company_name.

api.xml

<config>
    <
api>
        <
resources>

            <
catalog_category_link translate="title" module="catalog">
                <
title>Category links API (related)</title>
                <
model>catalog/category_link_api</model>
                <
acl>catalog/category/link</acl>
                <
methods>
                    <list 
translate="title" module="catalog">
                        <
title>Retrieve linked category</title>
                        <
method>items</method>
                    </list>
                    <
assign translate="title" module="catalog">
                        <
title>Assign category link</title>
                        <
acl>catalog/category/link/assign</acl>
                    </
assign>
                    <
remove translate="title" module="catalog">
                        <
title>Remove category link</title>
                        <
acl>catalog/category/link/remove</acl>
                    </
remove>
                </
methods>
                <
faults module="catalog">
                    <
type_not_exists>
                        <
code>100</code>
                        <
message>Given invalid link type.</message>
                    </
type_not_exists>
                    <
not_exists>
                        <
code>101</code>
                        <
message>Category not exists.</message>
                    </
not_exists>
                    <
data_invalid>
                        <
code>102</code>
                        <
message>Invalid data givenDetails in error message.</message>
                    </
data_invalid>
                    <
not_removed>
                        <
code>104</code>
                        <
message>Category link not removed.</message>
                    </
not_removed>
                </
faults>
            </
catalog_category_link>
        </
resources>
        <
resources_alias>
            
            <
category_link>catalog_category_link</category_link>
            
        </
resources_alias>
        <
acl>
            <
resources>
                <
catalog translate="title" module="catalog">
                    <
title>Catalog</title>
                    <
sort_order>1</sort_order>
                    <
category translate="title" module="catalog">
                        <
title>Category</title>
                        
                        <
link translate="title" module="catalog">
                            <
title>Link (Related)</title>
                            <
sort_order>101</sort_order>
                            <
assign translate="title" module="catalog">
                                <
title>Assign</title>
                            </
assign>
                            <
remove translate="title" module="catalog">
                                <
title>Remove</title>
                            </
remove>
                        </
link>
                        
                    </
category>

                </
catalog>
            </
resources>
        </
acl>
    </
api>
</
config>

and config.xml:

<config>
    <
modules>
        <
COMPNAME_Catalog>
            <
version>0.7.57</version
        </
COMPNAME_Catalog>
    </
modules>
    
    <global>
        <
models>
        
           <
catalog>
                   <
rewrite>
            <
category>COMPNAME_Catalog_Model_Category</category>
            <
category_api>COMPNAME_Catalog_Model_Category_Api</category_api>
                   </
rewrite>
                
                
           </
catalog>
           <
catalog_resource_eav_mysql4>
                <
rewrite>
                    <
category>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category</category>
                    <
product_collection>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Product_Collection</product_collection>
                    <
category_collection>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category_Collection</category_collection>
                </
rewrite>
                <
entities>
                    
                    <
category_link>
                        <
table>catalog_category_link</table>
                    </
category_link>
                    <
category_link_type>
                        <
table>catalog_category_link_type</table>
                    </
category_link_type>
                    <
category_link_attribute>
                        <
table>catalog_category_link_attribute</table>
                    </
category_link_attribute>
                    <
category_link_attribute_decimal>
                        <
table>catalog_category_link_attribute_decimal</table>
                    </
category_link_attribute_decimal>
                    <
category_link_attribute_int>
                        <
table>catalog_category_link_attribute_int</table>
                    </
category_link_attribute_int>
                    <
category_link_attribute_varchar>
                        <
table>catalog_category_link_attribute_varchar</table>
                    </
category_link_attribute_varchar>
                    
                    <
attribute_translation>
                        <
table>catalog_attribute_translation</table>
                    </
attribute_translation>
                    
                </
entities>  
            </
catalog_resource_eav_mysql4>
        </
models>
    </global>
</
config>

config.xml contains also rules for other features already implemented.

 
Magento Community Magento Community
Magento Community
Magento Community
 
gMike
Jr. Member
 
Total Posts:  4
Joined:  2009-03-05
 

I found the solutions. When add functionality to an existing module, you should moddify only config.xml from your /app/code/local/company_name/module_name/etc/. I will put here this config in case someone in the future will need..

<?xml version="1.0"?>
<config>
    <
modules>
        <
COMPNAME_Catalog>
            <
version>0.7.57</version
        </
COMPNAME_Catalog>
    </
modules>
    <global>
        <
models>
           <
catalog>
                   <
rewrite>
                    <
category>COMPNAME_Catalog_Model_Category</category>
                    <
category_api>COMPNAME_Catalog_Model_Category_Api</category_api>
                    <
product_api>COMPNAME_Catalog_Model_Product_Api</product_api>
                    <
category_link_api>COMPNAME_Catalog_Model_Category_Link_Api</category_link_api>
                    <
category_link>COMPNAME_Catalog_Model_Category_Link</category_link>
                   </
rewrite>
           </
catalog>
           <
catalog_resource_eav_mysql4>
                <
rewrite>
                    <
category>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category</category>
                    <
product_collection>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Product_Collection</product_collection>
                    <
category_collection>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category_Collection</category_collection>
                    <
category_link>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category_Link</category_link>
                    <
category_link_collection>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category_Link_Collection</category_link_collection>
                    <
category_link_category_collection>COMPNAME_Catalog_Model_Resource_Eav_Mysql4_Category_Link_Category_Collection</category_link_category_collection>
                </
rewrite>
                <
entities>
                    <
category_link>
                        <
table>catalog_category_link</table>
                    </
category_link>
                    <
category_link_type>
                        <
table>catalog_category_link_type</table>
                    </
category_link_type>
                    <
category_link_attribute>
                        <
table>catalog_category_link_attribute</table>
                    </
category_link_attribute>
                    <
category_link_attribute_decimal>
                        <
table>catalog_category_link_attribute_decimal</table>
                    </
category_link_attribute_decimal>
                    <
category_link_attribute_int>
                        <
table>catalog_category_link_attribute_int</table>
                    </
category_link_attribute_int>
                    <
category_link_attribute_varchar>
                        <
table>catalog_category_link_attribute_varchar</table>
                    </
category_link_attribute_varchar>
                    <
attribute_translation>
                        <
table>catalog_attribute_translation</table>
                    </
attribute_translation>
                </
entities>  
            </
catalog_resource_eav_mysql4>
        </
models>
    </global>
</
config>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top