Magento - rewrite hierarchy - magento

What is the config required to rewrite a class that is already rewriting another class in magento?
For example, There is a community module doing the following:
<models>
<modulea>
<adminhtml>
<rewrite>
<session>CompanyA_ModuleA_Model_Adminhtml_Session</session>
</rewrite>
</adminhtml>
</modulea>
</models>
I want to rewrite the community module and I am trying the following code which is not working:
<models>
<moduleb>
<class>CompanyB_ModuleB_Model</class>
</moduleb>
<modulea>
<rewrite>
<adminhtml_session>CompanyB_ModuleB_Model_Adminhtml_Session</adminhtml_session>
</rewrite>
</modulea>
</models>
What is wrong with this

Be carefull at your rewrite XML syntax, you have an error. It should like :
<modulea>
<adminhtml>
<rewrite>
<adminhtml_session>CompanyA_ModuleA_Model_Adminhtml_Session</session>
</rewrite>
</adminhtml>
</modulea>
and
<moduleb>
<adminhtml>
<rewrite>
<adminhtml_session>CompanyB_ModuleB_Model_Adminhtml_Session</session>
</rewrite>
</adminhtml>
</moduleb>
Next you'll need to extends the Good PHP class in the good order. FOr example if you want CompanyA extending/rewriting CompanyB :
CompanyA file :
class CompanyA_ModuleA_Model_Adminhtml_Session extends Mage_Adminhtml_Model_Session {
CompanyB Model php file :
class CompanyB_ModuleB_Model_Adminhtml_Session extends CompanyA_ModuleA_Model_Adminhtml_Session {
Last thing, to be sure that the rewrite is done in the good order (when doing a Mage::getModel('adminhtml/session') you want to retrieve the CompanyB class and not the COmpanyA one), you'll need to define which module depends on which. YOu do that in the app/etc/modules/Company_Module.xml file.
add for Company A :
<depends>
<Mage_Adminhtml />
</depends>
add for Company B :
<depends>
<CompanyA_ModuleA />
</depends>

Related

override class in core magento

I need to override class Mage_Paypal_Model_Ipn.
class Budsies_Budsie_Paypal_Model_Ipn extends Mage_Paypal_Model_Ipn
{
protected function _processOrder()
{
Mage::log('Budsies_Budsie_Paypal_Model_Ipn', null, 'processOrder.log');
$orderState = $this->_order->getState();
if ($orderState != Mage_Sales_Model_Order::STATE_COMPLETE &&
$orderState != Mage_Sales_Model_Order::STATE_HOLDED) {
parent::_processOrder();
}
}
}
I added config.xml to my module в global section:
<global>
<models>
<paypal>
<rewrite>
<ipn>Budsies_Budsie_Paypal_Model_Ipn</ipn>
</rewrite>
</paypal>
</models>
</global>
But it doesn't work. What should be config file to make my class to rewrite core class. Could you please tell me what's the problem?
I corrected it, simply it's this section is larger and I made copy paste. But it doesn't work.
Thank you for your help. My rewrite works. The problem was that ipn for that account were not configured, there were not ipn, so this method wasn't called.
If this is really what your XML looks like then it's this:
<global>
<models>
<paypal>
<rewrite>
<ipn>Budsies_Budsie_Paypal_Model_Ipn</ipn>
</rewrite>
</paypal>
</models>
</global>
You had </models> in there twice and didn't have your </global> tag closed.

Can not override Sales Resouce Model Collection class

I am trying to override Mage_Sales_Model_Resource_Order_Collection
My custom module's config:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Abc_Salesextend>
<version>0.1.0</version>
</Abc_Salesextend>
</modules>
<global>
<blocks>
<salesextend>
<class>Abc_Salesextend_Block</class>
</salesextend>
<adminhtml>
<rewrite>
<sales_order_grid>Abc_Salesextend_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
<models>
<salesextend>
<class>Abc_Salesextend_Model</class>
<resourceModel>salesextend_resource</resourceModel>
</salesextend>
<salesextend_resource>
<class>Abc_Salesextend_Model_Resource</class>
</salesextend_resource>
<!-- HERE is i am trying to override-->
<sales_resource>
<rewrite>
<order_collection>Abc_Salesextend_Model_Resource_Order_Collection</order_collection>
</rewrite>
</sales_resource>
</models>
</global>
</config>
It's not giving me any kind of error even if I place the wrong custom class name. So it's not finding my custom class.
Please help
I am not quite sure why the rewrite fails. The rewrite definition looks good. The error can only be related to the wrong class used for rewriting, a cache problem (config.xml cache) or to your module not being active.
I guess you want to just manipulate the collection so instead of a rewrite you should use some event/observer technique.
/**
*
* Sales order grid collection
* #param unknown_type $observer
*/
public function salesOrderGridCollectionLoadBefore($observer){
$collection = $observer->getOrderGridCollection();
$select = $collection->getSelect();
$select->DO_WHATEVER_YOU_WANT...
}
and this method is triggered by:
<events>
<sales_order_grid_collection_load_before>
<observers>
<cartware_modify_grid_adminhtml_collection>
<model>your_module/observer_block</model>
<method>salesOrderGridCollectionLoadBefore</method>
</cartware_modify_grid_adminhtml_collection>
</observers>
</sales_order_grid_collection_load_before>
</events>
</adminhtml>
Good luck!
<sales>
<rewrite>
<resource_order_collection>Abc_Salesextend_Model_YOURFILENAME</resource_order_collection>
</rewrite>
</sales>
just replace with you file name in YOURFILENAME,,,it works...

Magento override Customer Resource model

i try to overridde
app\code\core\Mage\Customer\Model\Resource\Customer.php
with this xml:
<models>
<customer_resource>
<rewrite>
<customer>My_Company_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
but i get
Mage registry key "_resource_singleton/customer/customer" already exists
and with
<customer>
<rewrite>
<customer_resource>My_Company_Model_Resource_Customer</customer_resource>
</rewrite>
</customer>
or
<models>
<customer>
<rewrite>
<resource_customer>My_Company_Model_Resource_Customer</resource_customer>
</rewrite>
</customer>
</models>
nothing happens
You should be able to accomplish it with the code you had. If it is not being overwritten try some of the basics like making sure you've cleared your cache. If that still doesn't work, you might be fighting with another module that is already overriding it. In that case you'd need to figure out the next steps in figuring out which one wins: merge logic, rewrite/extend the other one, etc.
<models>
<customer_resource>
<rewrite>
<customer>My_Company_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
I did it (in 1.7.0.2) with the following xml:
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<modules>
<Final_Dummy>
<version>0.0.1</version>
</Final_Dummy>
</modules>
<frontend>
<routers>
<dummy>
<use>standard</use>
<args>
<module>Final_Dummy</module>
<frontName>dummy</frontName>
</args>
</dummy>
</routers>
</frontend>
<global>
<helpers>
<dummy>
<class>Final_Dummy_Helper</class>
</dummy>
</helpers>
<models>
<dummy>
<class>Final_Dummy_Model</class>
<resourceModel>dummy_resource</resourceModel>
</dummy>
<dummy_rsource>
<class>Final_Dummy_Model_Resource</class>
<deprecatedNode>dummy_mysql4</deprecatedNode>
</dummy_rsource>
<customer_resource>
<rewrite>
<customer>Final_Dummy_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
</global>
</config>
and in order to make sure that it really works I created the following class:
<?php
class Final_Dummy_Model_Resource_Customer extends Mage_Customer_Model_Resource_Customer{
public function amIHere(){
return 'Yes sir';
}
}
?>
and called the resource in my module's controller like this
<?php
class Final_Dummy_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$res=Mage::getResourceModel('customer/customer');
die($res->amIHere());
}
}
?>
and the result was:
Yes sir
I hope these code help you in some way. remember to clear the cache and I do recommend that in development phase, disable Magento compiler.

Magento: Rewrite Block is not working

I try to rewrite core file from magento.
Somehow it does not overwrite the code. I try to overwrite the function getProduct().
Tipfix/Block/Product/View.php
<?php
class WP_Tipfix_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId()) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
//return Mage::registry('product');
}
}
Tipfix/etc/config.xml
<blocks>
<WP_Tipfix>
<class>WP_Tipfix_Block</class>
</WP_Tipfix>
<catalog>
<rewrite>
<product_view>WP_Tipfix_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
I have know idea what i'm doing wrong.
Gr.
Lex
Your class is WP_Tipfix_Block_Catalog_Product_View which means it must be in the folder WP/Tipfix/Block/Catalog/Product/View.php. You must either move your Product directory into a new directory called Catalog in that place or rename your class (both the class and in the XML) to WP_Tipfix_Block_Product_View. I recommend moving the file.
Please change the config.xml content of your module to this, and I'm sure that it should work:-
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<WP_Tipfix>
<version>1.0.0</version>
</WP_Tipfix>
</modules>
<global>
<blocks>
<wptipfix>
<class>WP_Tipfix_Block</class>
</wptipfix>
<catalog>
<rewrite>
<product_view>WP_Tipfix_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
</global>
</config>
Hope it helps.
UPDATE:-
After Ben's comment, I feel that I should have mentioned that the OP must also use the solution as mentioned by Max in his answer. So the OP will need a combined effort to fix his problem.

Rewriting Magento Gift Card observer function

When I need to rewrite a function in an observer located in the Enterprise section, how will that rewrite bit look like on config.xml.
is it something like this?
<global>
<models>
<enterprise>
<rewrite>
<giftcard>Custom_GiftCard_Model_Observer</giftcard>
</rewrite>
</enterprise>
</models>
</global>
My class is declared as follow:
class Custom_GiftCard_Model_Observer extends Enterprise_GiftCard_Model_Observer {
.....
}
I don't have an enterprise development environment setup at the moment, so this is untested, but it should work as described.
If you look at the Gift Card configuration in
app/code/core/Enterprise/GiftCard/etc/config.xml
You can grep about and discover the class alias for the gift card observer
<class>enterprise_giftcard/observer</class>
So, with a class alias of enterprise_giftcard/observer you have a model group name of enterprise_giftcard, and a model class name of observer.
In your module's configuration file, first you'll create an area for model configuration
<global>
<models>
</models>
</global>
Then, you'll add the group name of the class you want to rewrite, enterprise_giftcard
<global>
<models>
<enterprise_giftcard>
</enterprise_giftcard>
</models>
</global>
Then, you'll add a node saying you want to rewrite the a single class in this group
<global>
<models>
<enterprise_giftcard>
<rewrite>
</rewrite>
</enterprise_giftcard>
</models>
</global>
The, you'll add a node indicating WHICH class in the group you wish to rewrite, using the name portion of the class alias (observer)
<global>
<models>
<enterprise_giftcard>
<rewrite>
<observer></observer>
</rewrite>
</enterprise_giftcard>
</models>
</global>
And finally, within this node, you'll add a text node that's the name of your new class.
<global>
<models>
<enterprise_giftcard>
<rewrite>
<observer>Custom_GiftCard_Model_Observer</observer>
</rewrite>
</enterprise_giftcard>
</models>
</global>
You can test your rewrite by instantiating the observer directly, and checking its class name
$model = Mage::getModel('enterprise_giftcard/observer');
var_dump(get_class($model));

Resources