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...
Related
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.
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.
Edit 4/4/12
I STILL HAVE ONE QUESTION:
I solved my issue but it adds my option at the top of the list. How can I sort it to my my option to the bottom of the list?
Please see answer for contents of my working config.xml file...
I'm looking for in depth help here because I've been at this for quite a while now and seem to be getting nowhere.
Background
I was looking to make a printable RMA form for our customers to make it easier on them to Return/Exchange items.
I did this with the help of this extension: http://www.magentocommerce.com/magento-connect/admin-order-printing-extension.html
(Please download this to see the directory structure if needed)
It adds a button to the order and then I went in and edited the form/form layout so that it was an RMA (or has RMA content).
Everything is working great, however, we have to go into each order and press the button in order for it to print.
We really need a Mass Action in order to make it more efficient and useful.
Problem
I've tried to different ways to get this to work but I really need help on this. Most recently I've tried to follow this tutorial (http://www.blog.magepsycho.com/adding-new-mass-action-to-admin-grid-in-magento/) using the second method of events but I can't seem to get this to work.
Can someone please explain in more depth (this last method I've tried) so that I can get it working with this module???
I've added this in the config.xml(according to the tutorial)
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<orderprint_core_block_abstract_prepare_layout_before>
<class>orderprint/observer</class>
<method>addRmaAction</method>
</orderprint_core_block_abstract_prepare_layout_before>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
And in Nastnet/OrderPrint/Model/Observer.php I've added this (according to the tutorial)
<?php class Nastnet_OrderPrint_Model_Observer {
public function addRmaAction($observer) {
$block = $observer->getEvent()->getBlock();
if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addItem('pdfrma_order', array(
'label' => 'Print RMA',
'url' => Mage::app()->getStore()->getUrl('nastnet/controller/action'),
));
}
} }
(Sorry for the messy code above - the only way I could get it highlighted)
EDIT 1 (4/2/12)
This is my module's config.xml file. I've had to go with the rewrite/override method as of right now (deleting my attempts at using events). If you see anything, including the exact structure of what it would need to be please let me know...
Minus the <config> tags:
<modules>
<Nastnet_OrderPrint>
<version>0.1.3</version>
</Nastnet_OrderPrint>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>Nastnet_OrderPrint_Block_Sales_Order_Grid</sales_order_grid> <!-- WORKIING METHOD -->
<sales_order_view>Nastnet_OrderPrint_Block_Sales_Order_View</sales_order_view>
</rewrite>
</adminhtml>
</blocks>
<rewrite>
<Nastnet_OrderPrint_OrderController>
<from><![CDATA[#/\w+/sales_order/print/#]]></from>
<to>/orderprint/order/print/</to>
</Nastnet_OrderPrint_OrderController>
</rewrite>
<models>
<Nastnet_OrderPrint>
<class>Nastnet_OrderPrint_Model</class>
</Nastnet_OrderPrint>
</models>
<pdf>
<order>
<default>Nastnet_OrderPrint/order_pdf_items_order_default</default>
<grouped>Nastnet_OrderPrint/order_pdf_items_order_grouped</grouped>
</order>
</pdf>
</global>
<admin>
<routers>
<Nastnet_OrderPrint>
<use>admin</use>
<args>
<module>Nastnet_OrderPrint</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>orderprint</frontName>
</args>
</Nastnet_OrderPrint>
</routers>
I've figured out the correct handles and everything in order for this to work (with the help of this article: http://mydons.com/simple-example-using-magento-event-observer/)
In the config.xml file, I had to make new <adminhtml> handles after the <config> handles. This got me to the right "depth". Here is the entire config.xml file minus the <config> handles (cause it won't paste correctly):
<modules>
<Nastnet_OrderPrint>
<version>0.1.3</version>
</Nastnet_OrderPrint>
</modules>
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<Nastnet_OrderPrint_Model_Observer>
<type>singleton</type>
<class>Nastnet_OrderPrint_Model_Observer</class>
<method>addRmaAction</method>
</Nastnet_OrderPrint_Model_Observer>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_view>Nastnet_OrderPrint_Block_Sales_Order_View</sales_order_view>
</rewrite>
</adminhtml>
</blocks>
<rewrite>
<Nastnet_OrderPrint_OrderController>
<from><![CDATA[#/\w+/sales_order/print/#]]></from>
<to>/orderprint/order/print/</to>
</Nastnet_OrderPrint_OrderController>
</rewrite>
<models>
<Nastnet_OrderPrint>
<class>Nastnet_OrderPrint_Model</class>
</Nastnet_OrderPrint>
</models>
<pdf>
<order>
<default>Nastnet_OrderPrint/order_pdf_items_order_default</default>
<grouped>Nastnet_OrderPrint/order_pdf_items_order_grouped</grouped>
</order>
</pdf>
</global>
<admin>
<routers>
<Nastnet_OrderPrint>
<use>admin</use>
<args>
<module>Nastnet_OrderPrint</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>orderprint</frontName>
</args>
</Nastnet_OrderPrint>
</routers>
</admin>
This is the part that I needed and it is finally correct here:
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<Nastnet_OrderPrint_Model_Observer>
<type>singleton</type>
<class>Nastnet_OrderPrint_Model_Observer</class>
<method>addRmaAction</method>
</Nastnet_OrderPrint_Model_Observer>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
It points to my observer file (based on the correct event) here: app/code/local/Nastnet/OrderPrint/Model/Observer.php. This obviously then holds my addRmaAction.
I STILL HAVE ONE QUESTION:
This puts my option at the top of the list. How can I sort it to my my option to the bottom of the list?
Contents of Observer.php
<?php
class Nastnet_OrderPrint_Model_Observer
{
public function addRmaAction($observer)
{
$block = $observer->getEvent()->getBlock();
if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addItem('rmamassprint', array(
'label' => 'Print Return/Exchange',
'url' => Mage::app()->getStore()->getUrl('orderprint/order/pdfRma'),
));
}
}
}
For starters did you confirm whether or not your observer is being called? Just by adding a Mage::log("Observer called");
and watching the system.log file..
Here is the working tutorial for adding mass action to Sales > Order Grid:
http://www.blog.magepsycho.com/adding-new-mass-action-to-admin-grid-in-magento/
Try the 2nd approach(using event-observer method) which seems to be more upgrade proof way.
Thanks
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));
I would like to set the default number of displayed rows in the admin to higher than 20.
I was following along at http://inchoo.net/ecommerce/magento/magento-admin-grid-how-to-change-number-of-rows/, but I'm trying to make a module to do the task. Magento version is 1.4.2.0.
The error I am getting is Mage registry key "_singleton/grid/observer" already exists.
I have in app/code/local/Company/Custom/etc/config.xml:
<config>
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<grid_observer>
<class>grid/observer</class>
<method>applyLimitToGrid</method>
</grid_observer>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
</config>
and in app/code/local/Company/Custom/Model/Observer.php:
class Company_Custom_Grid_Model_Observer
{
public function applyLimitToGrid(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if (($block instanceof Mage_Adminhtml_Block_Widget_Grid) && !($block instanceof Mage_Adminhtml_Block_Dashboard_Grid)) {
$block->setDefaultLimit(200);
}
}
}
app/etc/modules/Company_Custom.xml:
<config>
<modules>
<Company_Custom>
<codePool>local</codePool>
<active>true</active>
</Company_Custom>
</modules>
</config>
class Company_Custom_Grid_Model_Observer
should be
class Company_Custom_Model_Observer
You don't have module models class declaration:
<global>
<models>
<modulename>
<class>Namespace_ModuleName_Model</class>
</modulename>
</models>
</global>
The biggest change was in Company/Custom/etc/config.xml, where I put the <events> section inside a <global> block instead of <adminhtml>.
Also inside that file, and fixing the error message was <class>Company_Custom_Model_Observer</class> (where I had grid/observer before...)