Magento - Override/extend Ebizmarts_SagePaySuite_Model_Api_Payment model class - magento

I am trying to override/extend the Ebizmarts SagePay Suite Ebizmarts_SagePaySuite_Model_Api_Payment model class
My module looks like this:
etc/modules/Sulman_ModifySagePay.xml
<?xml version="1.0"?>
<config>
<modules>
<Sulman_ModifySagePay>
<active>true</active>
<codePool>local</codePool>
<version>1.0</version>
<depends>
<Ebizmarts_SagePaySuite />
</depends>
</Sulman_ModifySagePay>
</modules>
</config>
app/code/local/Sulman/ModifySagePay/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Sulman_ModifySagePay>
<version>0.0.1</version>
</Sulman_ModifySagePay>
</modules>
<global>
<models>
<sagepaysuite>
<rewrite>
<api_payment>Sulman_ModifySagePay_Model_Api_Payment</api_payment>
</rewrite>
</sagepaysuite>
</models>
</global>
</config>
app/code/local/Sulman/ModifySagePay/Model/Api/Payment.php
<?php
class Sulman_ModifySagePay_Model_Api_Payment extends Ebizmarts_SagePaySuite_Model_Api_Payment {
public function _construct(){
Mage::log('in my construct()', null, 'Sulman.log');
}
public function requestPost($url, $data, $returnRaw = false) {
Mage::log('in my requestPost()', null, 'Sulman.log');
}
}
Nothing gets logged and this gives me errors on the checkout page because:
When I check to see if it is instantiated it returns false (if I disable my module it returns the Ebizmarts_SagePaySuite_Model_Api_Payment object):
var_dump(Mage::getModel('sagepaysuite/api_payment'));
Other calls to this model throughout the site also fail.
I'm a little confused as it seems my rewrite is working correctly but it can't seem to find my new model class.
Anyone have any ideas where I'm going wrong?
Thanks

In your app/code/local/Sulman/ModifySagePay/etc/config.xml in the <models></models> section you need to define where your models are located:
<models>
<modifysagepay>
<class>Sulman_ModifySagePay_Model</class>
<modifysagepay>
</models>

Related

Magento - How override admin Sales Totals block (Mage_Adminhtml_Block_Sales_Totals)

I am trying to override the admin Sales Totals block (Mage_Adminhtml_Block_Sales_Totals) situated under "/app/code/core/Mage/Adminhtml/Block/Sales/Totals.php"
I declare my module like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CompanyName_Adminhtml>
<version>0.1.0</version>
</CompanyName_Adminhtml>
</modules>
<global>
<blocks>
<companyname_adminhtml>
<class>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</class>
</companyname_adminhtml>
<adminhtml>
<rewrite>
<sales_totals>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</sales_totals>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
My module class path is like this:
local/CompanyName_Adminhtml/Block/Adminhtml/Sales/Totals.php
and My class code start like this:
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals extends Mage_Adminhtml_Block_Sales_Totals {
// block methods goes here..
}
Unfortunately the class override don't works and I don't get any errors in the log files.
I am using Magento Version 1.7.0.2
In the magento backoffice I can see my module as enabled.
Any help?
Try Overriding the individual Totals.php block which is at the location Mage/AdminHtml/Block/Sales/Order/ file rather than the main Totals.php under the Mage/AdminHtml/Block/Sales/ folder.
It worked for me. As i wanted to show one more row in the order-totals block.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CompanyName_Adminhtml>
<version>0.1.0</version>
</CompanyName_Adminhtml>
</modules>
<global>
<blocks>
<companyName_adminhtml>
<class>CompanyName_Adminhtml_Block</class>
</companyName_adminhtml>
<companyname_adminhtml>
<class>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</class>
</companyname_adminhtml>
<adminhtml>
<rewrite>
<sales_order_totals>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</sales_totals>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
then class
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Totalsextends Mage_Adminhtml_Block_Sales_Order_Totals {
protected function _initTotals() {
parent::_initTotals();
$this->_totals['foo_total'] = new Varien_Object(array(
'code' => 'purchasewrapping_total',
'value' => $this->getSource()->getFooTotal(),
'base_value'=> $this->getSource()->getBaseFooTotal(),
'label' => $this->helper('sales')->__('Foo'),
));
return $this;
}
}`enter code here`
Mage_Adminhtml_Block_Sales_Order_Totals is extends at Mage_Adminhtml_Block_Sales_Totals
and here you can extend and write your changes

magento can not load my custom model

Hi I have a issue which I don't understand, please someone could help me.
I'm on the 1.8.1
I have a Module in app->code->local->Mycompany
Mycampany
TestModel
etc
config.xml
Model
FirstModel.php
code in config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<TestModel>
<class>Mycompany_TestModel_Model</class>
</TestModel>
</models>
</global>
</config>
code in FirstModel.php
class Mycompany_TestModel_Model_FirstModel extends Mage_Core_Model_Abstract {
public function output()
{
echo "get";
}
}
When I use
Mage::getModel('TestModel/FirstModel')
magento can not load the class.
I tested and it is working on my local machine.
Please help.
Plus. I also tried:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<testmodel> /* instead of <TestModel> */
<class>Mycompany_TestModel_Model</class>
</testmodel>
</models>
</global>
</config>
I still can not get anything in
Mage::getModel('testmodel/FirstModel')
Thanks very much.
As you use magento fatory method [Mage::getModel('testmodel/FirstModel') ]
that means you need to use it procedure to define an model.
As per as magento, when autoloader execute testmodel/FirstModel run like:
testmodel=Mycompany_TestModel_Model
and
FirsModel is wrong ,File and name should be Firstmodel as after model model folder( Mycompany/TestModel/Model) all folders and files name should first letter with uppercase case and after that all should be lower a
That means file should be
Firstmodel.php instead of FirstModel
and testmodel/FirstModel should be testmodel/firstmodel
NO underscore should be use in folders and file name

How to rewrite a Magento Block with an extension?

I want to rewrite the Mage_Catalog_Block_Product_View block.
I created this file: config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<JB_CustomPrice>
<version>0.1.0</version>
</JB_CustomPrice>
</modules>
<global>
<blocks>
<customprice>
<class>JB_CustomPrice_Block</class>
</customprice>
<catalog>
<rewrite>
<product_view>JB_CustomPrice_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
</config>
Then I created the block in
/app/code/local/JB/CustomPrice/Block/Catalog/Product/View.php
<?php
class JB_CustomPrice_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
public function getJsonConfig()
{
return 'test';
}
}
?>
Though, the product view doesn't change, even not when I call the getProduct() method. What's going wrong?
The global error was a typo, solved by using Mage::log(get_class($this)), that fixed it. The problem was that another plugin extended Mage_Catalog_Block_Product_View already. It was an old Yoast_MetaRobots plugin, which is deprecated. I disabled it and everything was fixed. Thanks for the answer!

Magento override controller

I would like to do the above.
Ive overridden many files in the past...block, model, helper....but this one eludes me.
Can anyone see what im doing wrong here:
(ive edited this code...to include some of the recomendations now...)
Heres my folder structure (2 controller locations as a test):
/Idigital/Idgeneral/etc/config.xml
/Idigital/Idgeneral/controllers/Checkout/CartController.php
/Idigital/Idgeneral/controllers/CartController.php
Heres my config.xml:
<?xml version="1.0"?>
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
<blocks>
<idgeneral><class>Idigital_Idgeneral_Block</class></idgeneral>
</blocks>
</global>
<frontend>
<routers>
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>
<layout>
<updates>
<idgeneral>
<file>idigital.xml</file>
</idgeneral>
</updates>
</layout>
</frontend>
</config>
Ihave placed my controller file in 2 locations to test.
And heres the top of my FIRST controller file:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside checkout/cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
Ans my second controller:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
When i visit: /checkout/cart/add
Im directed to the mage controller...not my local. (i have var_dump stmts in each..so i can see which is ran).
When i visit /checkout/cart/test - i get a 404
When i visit /cart/add or cart/test - i get a 404
when i visit idgeneral/cart/test or idgeneral/cart/add - i get a 404
Create your module folders and files
app/code/local/MyNameSpace/MyModule/etc/config.xml
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
app/etc/modules/MyNameSpace_All.xml
Edit /etc/config.xml and Create app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:
<?xml version="1.0"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<global>
<!-- This rewrite rule could be added to the database instead -->
<rewrite>
<!-- This is an identifier for your rewrite that should be unique -->
<!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<!--
- mymodule matches the router frontname below
- checkout_cart matches the path to your controller
Considering the router below, "/mymodule/checkout_cart/" will be
"translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
-->
<to>/mymodule/checkout_cart/</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
<!--
If you want to overload an admin controller this tag should be <admin> instead,
or <adminhtml> if youre overloading such stuff (?)
-->
<frontend>
<routers>
<mynamespace_mymodule>
<!-- should be set to "admin" when overloading admin stuff (?) -->
<use>standard</use>
<args>
<module>MyNameSpace_MyModule</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>mymodule</frontName>
</args>
</mynamespace_mymodule>
</routers>
</frontend>
Note: The above didn’t work for me when I override catalog/product controller. I had to use:
<from><![CDATA[#^catalog/product/#]]></from>
<to>mymodule/mycontroller</to>
(notice the missing leading slash)
Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Please note that before=”Mage_Checkout” will load your controller first if available and fall back to Magento’s if not.
If the controller is in another folder, you’ll have to modify the code:
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.
Replace
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
with
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
Edit 'controllers/Checkout/CartController.php'
Create app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php with the following content: (the only change to indexAction() is adding an error_log() call):
<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Checkout/controllers/CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends
Mage_Checkout_CartController
{
# Overloaded indexAction
public function indexAction() {
# Just to make sure
error_log('Yes, I did it!');
parent::indexAction();
}
}
Edit 'app/etc/modules/MyNameSpace_All.xml'
(This is to activate your module)
true
local
Edit 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' and add the following to use the same update handle as before:
<mynamespace_mymodule_checkout_cart_index>
<update handle="checkout_cart_index"/>
</mynamespace_mymodule_checkout_cart_index>
(Note that these tags seem to be case sensitive. Try using all lowercase if this isn’t working for you)
[by Hendy: When I override catalog/product/view using the method described in this Wiki or here, I didn’t have to do the above. However, when using the 'cms way', I had to update the handle manually.]
The above item did not work for me (2009-02-19 by Jonathan M Carvalho)
I discovered that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml
Add the following lines:
Point your browser to /checkout/cart/
In your PHP error log, you should find ‘Yes, I did it!’.
You need to be extra precise with the rewrite regular expression because errors are very hard to find.
<Idigital_Idgeneral before="Mage_Checkout">Idgeneral_Checkout</Idigital_Idgeneral>
Should be
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
or try moving your custom controller up to
../Idigital/Idgeneral/controllers/CartController.php
and use
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral</Idigital_Idgeneral>
There is also an error in your <modules> tag location. It should be:
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
...
</global>
<frontend>
....
</frontend>
...
</config>
i.e <modules> shouldn't be inside <global>
Here's a good tutorial on how to dump the config tree that Magento sees as XML: http://alanstorm.com/magento_config
I'm leaving this here for the next poor developer forced to work with this jalopy. Much of the instructions here are pasted from the magento docs which like its source, is a twisted labyrinth of misdirection. Okay enough complaints...
This worked for me in version 1.8
Create your namespace and module:
/app/code/local/MyNameSpace/MyModule
Create your module config:
/app/code/local/MyNameSpace/MyModule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Create your controller:
/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction() {
// /var/log/debug.log should log the output
Mage::log('cart index override', null, 'debug.log');
// Call the parent class
parent::indexAction();
}
}
Enable your new module:
/app/etc/modules/MyNameSpace_All.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyNameSpace_MyModule>
</modules>
</config>
That's all thats needed. Now go celebrate, you just polished a turd! ;)
This is a little notification on the include path of the controller.
This include path can cause errors if the Magento Compiler mode is turned on.
require_once 'Mage/Checkout/controllers/CartController.php';
Instead of that it is good to use
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
It will be safer.
Well...it WONT override the checkout cart controller.
So i used the URL REWRITE in the PRODUCT VIEW...from this link...near the bottom of the article. They say its a proper method...
http://www.excellencemagentoblog.com/magento-add-product-to-cart-ajax
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","idgeneral/cart");
That worked for me. Allows me to get cracking. Basically calls MY controller..instead of checkout controller.
Thanks for the help ROSCIUS...appreciated.
I also had to change my config....my routers section now looks like this:
<routers>
<!-- THIS PART REGISTERS OUR CONTROLLERS FOLDER FOR USE -->
<idgeneral>
<use>standard</use>
<args>
<module>Idigital_Idgeneral</module>
<frontName>idgeneral</frontName>
</args>
</idgeneral>
<!-- THIS PART WONT WORK TO OVERWRITE OUR MAGE CONTROLLER -->
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>

where can i declare my custom functions in magento

I want to declare some php functions and i would like to call those functions in various places in magento.Usually in my core php projects i'm declaring php functions in functions.php and i include that file in all pages.I'm not familiar with MVC structure.So where can i declare these kind of functions.
Thanks
Edit :-
Mango_Myfunc.xml (app/etc/modules)
<?xml version="1.0"?>
<config>
<modules>
<Mango_Myfunc>
<active>true</active>
<codePool>local</codePool>
</Mango_Myfunc>
</modules>
</config>
Config.xml(app/code/local/Mango/Myfunc/etc/configure.xml)
<?xml version="1.0"?>
<config>
<modules>
<Mango_Myfunc>
<version>0.1.0</version>
</Mango_Myfunc>
</modules>
<global>
<helpers>
<Myfunc>
<class>Mango_Myfunc_Helper</class>
</Myfunc>
</helpers>
</global>
</config>
Data.php(app/code/local/Mango/Myfunc/helper/Data.php)
class Mango_Myfunc_Helper_Data extends Mage_Core_Helper_Abstract
{
public function short_str ($str, $len, $suf = '...') {
if (strlen($str) > $len)
return substr($str, 0, $len - strlen($suf) ) . $suf;
return $str;
}
}
This is what i added
i used bellow one to call the function in list.phtml
echo $this->helper('Myfunc/Data')->short_str("test","3"); got the error
Fatal error: Class 'Mage_Myfunc_Helper_Data' not found
Magento has helper classes for those kind of methods. So make your extensions and add your methods and you can then later call them like follows
Mage::helper('yourextension/yourhelper')->yourMethod();
Or you can make a library class out of your common methods.

Resources