Magento - How override admin Sales Totals block (Mage_Adminhtml_Block_Sales_Totals) - magento

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

Related

Magento - Override/extend Ebizmarts_SagePaySuite_Model_Api_Payment model class

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>

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 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.

Helper class not found

I'm trying to develop an extension that would add a form on the Admin side of Magento, but, for some reason, I can't seem to be able to even get Magento Administration to load when my module is installed. I'm at the very beginning of the development, and I'm stuck on an error that has been reported several times on StackOverflow. Unfortunately, none of the answers seem to help in my case.
The error I get is *Fatal error: Class 'Mage_Mycompany_Logviewer_Helper_Data' not found in C:\XAMPP\htdocs\magento\app\Mage.php on line 546*. That should mean that Magento can't find the helper class, but it's there and its name matches the one it's looking for (except for the "Mage_" at the beginning, that I never used in any other extension anyway).
Update 2012/07/29
The error occurs as soon as I log in into Magento Admin. When I click "Login", all I get is an error page, nothing is rendered.
Here's the content of all the files I have so far.
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Logviewer>
<version>0.1.0</version>
</Mycompany_Logviewer>
</modules>
<global>
<models>
<logviewer>
<class>Mycompany_Logviewer_Model</class>
</logviewer>
</models>
<blocks>
<logviewer>
<class>Mycompany_Logviewer_Block</class>
</logviewer>
</blocks>
<helpers>
<logviewer>
<class>Mycompany_Logviewer_Helper</class>
</logviewer>
</helpers>
</global>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<mycompany_logviewer after="Mage_Adminhtml">Mycompany_Logviewer_Adminhtml</mycompany_logviewer>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<adminhtml>
<menu>
<mycompany translate="title" module="mycompany_logviewer">
<title>Mycompany</title>
<sort_order>90</sort_order>
<children>
<form translate="title" module="mycompany_logviewer">
<title>Form</title>
<sort_order>10</sort_order>
<action>adminhtml/logviewer</action>
</form>
</children>
</mycompany>
</menu>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<mycompany>
<title>Mycompany</title>
<sort_order>90</sort_order>
<children>
<form>
<title>Form</title>
<sort_order>10</sort_order>
</form>
</children>
</mycompany>
</children>
</admin>
</resources>
</acl>
</adminhtml>
</config>
Data.php (Helper)
class Mycompany_Logviewer_Helper_Data extends Mage_Core_Helper_Abstract
{
}
LogviewerController.php (Controller)
class Mycompany_Logviewer_Adminhtml_LogviewerController extends Mage_Adminhtml_Controller_Action
{
/**
* View form action
*/
public function indexAction() {
$this->loadLayout();
$this->_setActiveMenu('Mycompany/form');
$this->_addBreadcrumb(Mage::helper('Mycompany_Logviewer')->__('Form'), Mage::helper('Mycompany_Logviewer')->__('Form'));
$this->renderLayout();
}
/**
* Check allow or not access to ths page
*
* #return bool - is allowed to access this menu
*/
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('Mycompany/form');
}
}
Mycompany_Logviewer.xml (configuration file)
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Logviewer>
<active>true</active>
<codePool>local</codePool>
</Mycompany_Logviewer>
</modules>
</config>
All the above have been copied from an example I found online, I just replaced the Namespace and the Module name with Mycompany and Logviewer, respectively. I'm pretty sure it's something obvious I forgot, but I can't figure out what it could be. Thanks for the help.
The first thing that jumps out is that you are registering a module with the name of Procedo_Logviewer - when in fact it should be Mycompany_Logviewer
So, Mycompany_Logviewer.xml should be as follows:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Logviewer>
<active>true</active>
<codePool>local</codePool>
</Mycompany_Logviewer>
</modules>
</config>
Clear cache after changing
EDIT
Looking more closely at your controller, I can also see that you are calling your helper like so:
Mage::helper('Mycompany_Logviewer')
You should be calling your helper like this instead:
Mage::helper('logviewer')
EDIT 2
Another issue is present in your config.xml
module="mycompany_logviewer"
Should be
module="logviewer"
This correlates to the helper node you declared in your xml i.e.
<helpers>
<logviewer>
<class>Mycompany_Logviewer_Helper</class>
</logviewer>
</helpers>
I had similar problem. Solution was putting company name in like this:
<helpers>
<Mycompany_Logviewer>
<class>Mycompany_Logviewer_Helper</class>
</Mycompany_Logviewer>
</helpers>
$myHelper= Mage::helper('Mycompany_Logviewer');

How to disable frontend registration in Magento

As titled.
But still, users can login to the frontend, and administrator could create user account only in backend.
Another possibility would be overloading the customer/account/create action and just redirect the user to the home page when this action is invoked.
In a first time, just do what was proposed by Ben V. It will remove the possibility to view the registration page.
Then, create a new module in which you will overload the AccountController.php.
1- Create a new folder in app/code/local/ named Mycompany
2- Create a new folder in app/code/local/Mycompany/ named Registrationremove
3- Create app/code/local/Mycompany/Registrationremove/etc/
4- Create app/code/local/Mycompany/Registrationremove/etc/config.xml
Copy and Paste in config.xml :
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Registrationremove>
<version>0.1.0</version>
</Mycompany_Registrationremove>
</modules>
<global>
<rewrite>
<mycompany_registrationremove_customer_account_create>
<from><![CDATA[#^/customer/account/create/$#]]></from>
<to>/registrationremove/customer_account/create</to>
</mycompany_registrationremove_customer_account_create>
<mycompany_registrationremove_customer_account_createPost>
<from><![CDATA[#^/customer/account/createPost/$#]]></from>
<to>/registrationremove/customer_account/createPost</to>
</mycompany_registrationremove_customer_account_createPost>
</rewrite>
</global>
<frontend>
<routers>
<registrationremove>
<use>standard</use>
<args>
<module>Mycompany_Registrationremove</module>
<frontName>registrationremove</frontName>
</args>
</registrationremove>
</routers>
</frontend>
</config>
5- Create app/code/local/Mycompany/Registrationremove/controllers
6- Create app/etc/modules/Mycompany_Registrationremove.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Registrationremove>
<active>true</active>
<codePool>local</codePool>
</Mycompany_Registrationremove>
</modules>
</config>
7- Create app/code/local/Mycompany/Registrationremove/controllers/Customer/AccountController.php
Copy and Paste in AccountController.php:
require_once 'Mage/Customer/controllers/AccountController.php';
class Mycompany_Registrationremove_Customer_AccountController extends Mage_Customer_AccountController
{
public function createAction()
{
$this->_redirect('*/*');
}
public function createPostAction()
{
$this->_redirect('*/*');
}
}
8- Create app/code/local/Mycompany/Registrationremove/Helper/Data.php
Copy and paste in Data.php:
class Mycompany_Registrationremove_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Now, when someone tries to access to customer/account/create/ it should be redirected to the home page.
Hope that helped :)
Hugues.
You could modify the login screen to remove the "Create New Account" button. This way existing users can still log in but they have no way to create new accounts.
The file to modify is /app/design/frontend/default/default/template/customer/form/login.phtml. Around line 41 you'll see <div class="col-1 new-users">. Comment out that entire div to hide the New User section of the login page.
Edit:
There is no way to just disable new user registration like you are asking. I did a little more searching, and all I found were several people with the same idea as mine. In addition to my original suggestion, I would
a) remove the <customer_account_create> section of /app/design/frontend/default/default/layout/custom.xml, and
b) remove the registration-related lines from /app/design/frontend/default/default/template/checkout/onepage/login.phtml.
Ok. I got it worked. Refer to Hugues Solution, there are two amendments:
add app\etc\modules\Mycompany_All.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Registrationremove>
<active>true</active>
<codePool>local</codePool>
</Mycompany_Registrationremove>
</modules>
</config>
modify the file: app/code/local/Mycompany/Registrationremove/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Registrationremove>
<version>0.1.0</version>
</Mycompany_Registrationremove>
</modules>
<global>
<rewrite>
<mycompany_registrationremove_customer_account_create>
<from><![CDATA[#^/customer/account/create/$#]]></from>
<to>/registrationremove/customer_account/create</to>
</mycompany_registrationremove_customer_account_create>
<mycompany_registrationremove_customer_account_createPost>
<from><![CDATA[#^/customer/account/createPost/$#]]></from>
<to>/registrationremove/customer_account/createPost</to>
</mycompany_registrationremove_customer_account_createPost>
</rewrite>
</global>
<frontend>
<routers>
<mycompany_registrationremove>
<use>standard</use>
<args>
<module>Mycompany_Registrationremove</module>
<frontName>registrationremove</frontName>
</args>
</mycompany_registrationremove>
</routers>
</frontend>
</config>
In the above example structure, the controller class name should be changed
from
class Mycompany_Registrationremove_AccountController extends Mage_Customer_AccountController
to
class Mycompany_Registrationremove_Customer_AccountController extends Mage_Customer_AccountController
Does this method works for ver. 1.4.1.1?
Finally I decided to skip all the complicated steps and also modify the register.phtml in \app\design\frontend\base\default\template\customer\form, apart from the files that BenV said.
I deleted the form and changed the title to "User registration disabled" :)
I couldn't get any of the solutions posted here to work, and found I had to use the following syntax in the config.xml (specifically frontend routers syntax):
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Registrationremove>
<version>0.1.0</version>
</Mycompany_Registrationremove>
</modules>
<global>
<rewrite>
<mycompany_registrationremove_customer_account_create>
<from><![CDATA[#^/customer/account/create/$#]]></from>
<to>/registrationremove/customer_account/create</to>
</mycompany_registrationremove_customer_account_create>
<mycompany_registrationremove_customer_account_createPost>
<from><![CDATA[#^/customer/account/createPost/$#]]></from>
<to>/registrationremove/customer_account/createPost</to>
</mycompany_registrationremove_customer_account_createPost>
</rewrite>
</global>
<frontend>
<routers>
<customer>
<args>
<modules>
<Mycompany_Registrationremove before="Mage_Customer">Mycompany_Registrationremove_Customer</Mycompany_Registrationremove>
</modules>
</args>
</customer>
</routers>
</frontend>
</config>
There's more info on this method here - http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller
One could always change the customer router to point to a module without controllers, e.g.
<frontend>
<routers>
<customer>
<args>
<module>MyModule_NullRouting</module>
</args>
</customer>
</routers>
</frontend>
As an extension to liquidity's answer, a couple more changes I'd make to improve this. Firstly the regex doesn't match the url if it does not have a trailing slash eg. customer/account/create. To correct, the "from" nodes should read <![CDATA[#^/customer/account/create(/.*)?$#]]> and <![CDATA[#^/customer/account/createPost(/.*)?$#]]>.
Secondly, I've included an observer that listens for the customer_registration_is_allowed event (this gets called in persistent/customer/form/login.phtml when using the RWD theme from the $this->helper('customer')->isRegistrationAllowed() function):
<events>
<customer_registration_is_allowed>
<observers>
<your_module_set_is_active>
<class>Your_Module_Model_Observers_Customer</class>
<method>disableCustomerRegistration</method>
</your_module_set_is_active>
</observers>
</customer_registration_is_allowed>
</events>
Then in the observer:
class Your_Module_Model_Observers_Customer
{
/**
* Force disable customer registration
*
* #param Varien_Event_Observer $observer Observer
* #return void
*/
public function disableCustomerRegistration($observer)
{
$result = $observer->getResult();
if ($result->getIsAllowed() === true) {
$result->setIsAllowed(false);
}
}
}
This sets allowing registration to false and prevents the registration form showing without any template modifications.
Hope this is helpful!

Resources