Extending Mage Core Files Without Loosing Namespace - magento

I would like to introduce my own entities into particular Magento module namespaces for example I might want to be able to call
Mage::getModel('catalog/brand')->load(1);
Brand is not currently a model included in the catalog module. I don't want to modify core files nor do I want to hack the core by just adding a Mage folder to the local directory.
I was thinking perhaps syntax inside of my namespaces config file similar to this:
<models>
<catalog>
<args>
<modules>
<AJW_Catalog before="Mage_Catalog">AJW_Catalog</AJW_Catalog>
</modules>
</args>
</catalog>
<ajw_catalog>
<class>AJW_Catalog_Model</class>
</ajw_catalog>
</models>
but it does not seem to work.
Does anyone know how this can be accomplished?

Maybe possible with some trickery, but not officially supported, and generally a bad idea. The before= syntax you've used only works for the routers node. There's no framework code to let you do what you're trying to do. Also, there's a strong bias in the Magento framework code towards individual modules "owning" their namespace/package name. Defining new models in an existing namespace (catalog) introduces the theoretical possibility that your code may conflicts with a future version of Magento's code.

This may be a possible fix (brain fart)
Create a module named Customnamespace_Catalog and then just rewrite the catalog module with a node that doesnt exist in the default mage module:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Catalog>
<version>0.1.0</version>
</Namespace_Catalog>
</modules>
<global>
<models>
<catalog>
<rewrite>
<brand>Namespace_Brand_Model_Brand</brand>
</rewrite>
</catalog>
</models>
</global>
</config>
Followed by an additional module:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Brand>
<version>0.1.0</version>
</Namespace_Brand>
</modules>
<global>
<models>
<brand>
<class>Namespace_Brand_Model</class>
</brand>
</models>
</global>
</config>
This will allow you to call Mage::getModel('catalog/brand')
echo get_class(Mage::getModel('catalog/brand'); // Namespace_Brand_Model_Brand

Related

Magento custom language file not being used

I have created a custom language file for a feature that I have built into our Magento website. The language variables work fine on my local machine (of course), however on our staging environment it doesn't. My local machine is Windows and staging server is Linux, so obvious answer would be an issue with filename casing, but imho these are right.
I have my own block that overwrites the Mage_Catalog, called Feno_Catalog which works fine. To that config.xml file I've appended some code to load the Feno_Catalog.csv;
/local/Feno/Catalog/etc/config.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<config>
<modules>
<Feno_Catalog>
<version>0.1.0</version>
</Feno_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<class>Feno_Catalog_Block</class>
</rewrite>
</catalog>
</blocks>
<helpers>
<catalog>
<rewrite>
<class>Feno_Catalog_Helper</class>
</rewrite>
</catalog>
</helpers>
</global>
<frontend>
<translate>
<modules>
<Feno_Catalog>
<files>
<default>Feno_Catalog.csv</default>
</files>
</Feno_Catalog>
</modules>
</translate>
</frontend>
<adminhtml>
<translate>
<modules>
<Feno_Catalog>
<files>
<default>Feno_Catalog.csv</default>
</files>
</Feno_Catalog>
</modules>
</translate>
</adminhtml>
</config>
The CSV file has been put into 2 folders: /app/locale/[de_DE|en_US]/ with matching casing.
As I mentioned it works fine on my local machine, but not on the staging server. What could cause this? I've searched for quite a bit and cleared cache (although cache is turned off), switched languages (both languages don't work - The language keys are like "poll_question_a1").
When I move the translations to Mage_Catalog.csv everything also works fine (but of course that is not what I want).
So how to fix? Is there any way to find the cause of this?
Perhaps since you're rewriting the catalog module, you need your translates to look like this:
<translate>
<modules>
<Mage_Catalog>
<files>
<feno>Feno_Catalog.csv</feno> <!-- name it something other than default, to avoid conflict with Mage_Catalog -->
</files>
</Mage_Catalog>
</modules>
</translate>
Also, you can try looking in app/code/core/Mage/Core/Model/Translate.php around line 131-134. That is where it is loading your module translations. Try doing some Mage::log() calls in and around there to see if your CSV files are actually getting loaded.

Magento extend cms block class

I'm trying to extend the magento class Mage_Cms_Block_Block. I've got my module active. I think the reason why its failing is to do with the config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<VisibleBlocks_ShowBlocks>
<!-- The version of our module, starting at 0.0.1 -->
<version>0.0.1</version>
</VisibleBlocks_ShowBlocks>
</modules>
<global>
<blocks>
<mage_cms>
<rewrite>
<cms_blocks>VisibleBlocks_ShowBlocks_Block_Border</cms_blocks>
</rewrite>
</mage_cms>
</blocks>
</global>
</config>
Can someone explain to me what the tags inside the global tags mean? Can the tags inside rewrite be called anything?
OK seems that asking the questions made it clearer to me. Hope this hasn't a waste of time for anyone. 'mage_cms' should be 'cms' as that is the module i'm extending and 'cms_blocks' should be 'block'.
i have explain the details,please check
<global>
<blocks>
<cms><!--module name of rewrite class mage_cms -->
<rewrite>
<!-- file path of Block of app/code/core/mage/cms/block.php -->
<blocks>VisibleBlocks_ShowBlocks_Block_Border</blocks>
</rewrite>
</cms>
</blocks>
</global>
<config>
<global>
<blocks>
<cms>
<rewrite>
<block>VisibleBlocks_ShowBlocks_Block_Cms_Block</block>
</rewrite>
</cms>
</blocks>
</global>
</config>
With these tags, we say we will configure a block of Magento’s core called cms and we will rewrite () the « block » block of this module
Also make sure your module is active & being displayed in system/config/Advanced

Magento helper methods do not work

So this one is a bit odd. I'm sure it's old hat to some now, and I've just done a bad job searching it out. If that's the case I certainly apologize and ask for your indulgence and a link to the already provided answer.
So I have followed the core guides for creating a magento module, but for some reason it does work right. When I var_dump the helper class it has the correct name, but when I try to use one of the methods I've put in the class definition it throws an exception.
Furthermore, when I run get_class_methods() on the class given to me by Mage::helper('mymodule/myhelper') none of my methods are there.
So in summary:
The class is in the right place, app/code/local/MyModule/Helper/Myhelper.php
The class shows as initialized after I call Mage::helper('mymodule/myhelper')
The class methods are not listed, and as a result, break things when called.
Here's my config for reference. The rest of the module seems to be fine.
<?xml version="1.0"?>
<config>
<modules>
<APCShared_Shipping>
<version>0.1.0</version>
</APCShared_Shipping>
</modules>
<frontend>
<routers>
<apcshipping>
<use>standard</use>
<args>
<module>APCShared_Shipping</module>
<frontName>apcshipping</frontName>
</args>
</apcshipping>
</routers>
</frontend>
<global>
<blocks>
<apcshipping>
<class>APCShared_Shipping_Block</class>
</apcshipping>
</blocks>
<helpers>
<apcshipping>
<class>APCShared_Shipping_Helper</class>
</apcshipping>
</helpers>
<models>
<apcshipping>
<class>APCShared_Shipping_Model</class>
<resourceModel>shipping_resource</resourceModel>
</apcshipping>
<apcshipping_resource>
<class>APCShared_Shipping_Model_Resource</class>
<entities>
<zipcode>
<table>apc_shipping_zipcode</table>
</zipcode>
</entities>
</apcshipping_resource>
</models>
</global>
</config>
In the admin somewhere under System > Configuration > Advanced ensure your module has been registered.``
Make sure you have the class APCShared_Shipping_Helper_Data in the Helpers folder in a file called Data.php. Your class should extend Mage_Core_Helper_Abstract
Access your helper with Mage::helper('apcshipping')
Your directory structure should be something like:
- Module Folder
- etc
- config.xml
- Helper
- Data.php
In your file structure, point 1 of your summary, you are only using a modulename, not a packagename.
So it should be: app/code/local/MyCompany/MyModule/Helper/Myhelper.php instead of app/code/local/MyModule/Helper/Myhelper.php.
I'm not sure if this will make a difference, but it's always good to follow the coding convention.

Overriding Core Blocks

I'm try to overriding Mage_Core_Block_Messages
I'm create module
Mycompany_Core.xml and save in path app/etc/modules/Mycompany_Core.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Core>
<active>true</active>
<codepool>local</codepool>
</Mycompany_Core>
</modules>
</config>
next I was create in app/code/local/Mycompany/Core/Block/Messages.php
class Mycompany_Core_Block_Messages extends Mage_Core_Block_Messages
{
//update method
}
and add config.xml in app/code/local/Mycompany/Core/etc/config.xml
<config>
<modules>
<Mycompany_Core>
<version>0.0.1</version>
</Mycompany_Core>
</modules>
<global>
<blocks>
<core>
<rewrite>
<messages>Softdk_Core_Block_Messages</messages>
</rewrite>
</core>
</blocks>
</global>
</config>
But i don't see any result on frontend, I'm wonder where I'm make mistake.
Thx for help.
There are two things that caught my eye.
1.) In your module's registration file, it should be codePool and not codepool (as also said by David in comments)
2.) What is Softdk? If that is the name of your new module, then replace Mycompany with Softdk everywhere in your module.
Clear cache and voila!

Overriding Magento Block

I'm stuck. Been messing with this all day. To me this looks like it should work but It's not, and its not outputting any errors to magento error log.
What I tried to do was simply override the getPriceHtml() function in Catalog/Block/Product.php. The module is active from the 'Advanced' tab via the system configuration.
My config.xml in app/code/local/Brian/Pricefix/etc/config.xml:
<config>
<modules>
<Brian_Pricefix>
<version>1.0</version>
</Brian_Pricefix>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product>Brian_Pricefix_Catalog_Block_Product</product>
</rewrite>
</catalog>
</blocks>
</global>
</config>
My Brian_Pricefix.xml in app/etc/modules:
<config>
<modules>
<Brian_Pricefix>
<active>true</active>
<codePool>local</codePool>
</Brian_Pricefix>
</modules>
</config>
My Product.php in app/code/local/Brian/Pricefix/Catalog/Block/Product.php
class Brian_Pricefix_Catalog_Block_Product extends Mage_Catalog_Block_Product
{
public function getPriceHtml($product)
{
Mage::log("IM IN YOUR MODULEZ");
$this->setTemplate('catalog/product/price_fix.phtml');
$this->setProduct($product);
return $this->toHtml();
}
}
The new module isn't taking, its not logging anything or outputting price_fix.phtml
Any suggestions? I've done a few hours or research and this appears to be the right way to extend a block, so i'm not sure whats going on. The lack of error output is frustrating.
Thanks.
Looks like Mage_Catalog_Block_Product is not used anywhere.
catalog.xml contains mainly Mage_Catalog_Block_Product_View or Mage_Catalog_Block_Product_List calls.
You config looks fine. Try overriding another block.
Can you precise the page you are testing on?

Resources