Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Override core block
 
anton_SH
Jr. Member
 
Total Posts:  5
Joined:  2011-08-02
 

Hello, I tried to override a block, but the module doesn`t work. The block which I need to override is /app/code/core/Mage/Checkout/Block/Cart.php
I created 3 files:
/app/code/local/MyNameSpace/SkipCart/Block/Cart.php

include_once(’Mage/Checkout/Block/Cart.php’);
class SkipCart_Block_Cart extends Mage_Checkout_Block_Cart {

public function __construct(){
parent::addAction();
$home_url = Mage::helper(’core/url’)->getHomeUrl();
Mage::app()->getFrontController()->getResponse()->setRedirect($home_url.’checkout/onepage’);
}
}

/app/code/local/MyNameSpace/SkipCart/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
<global>
<blocks>
<checkout>
<rewrite>
<cart>SkipCart_Block_Cart</cart>
</rewrite>
</checkout>
</blocks>
</global>
</config>

/app/etc/modules/My_SkipCart.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyNameSpace_SkipCart>
<active>true</active>
<codePool>community</codePool>
</MyNameSpace_SkipCart>
</modules>
</config>

I tested the module with test.php file in main directory. The code is:
include ‘app/Mage.php’;
Mage::app();
$object = Mage::app()
->getLayout()
->createBlock(’checkout/cart’);

echo get_class($object);

Can anybody help me?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

There are 2 problems in your code (that I see)
In config.xml this:

<cart>SkipCart_Block_Cart</cart>
should be this:
<cart>MyNameSpace_SkipCart_Block_Cart</cart>
and your block class should be
class MyNameSpace_SkipCart_Block_Cart extends Mage_Checkout_Block_Cart { 
...
}
and you dont need this line
include_once(’Mage/Checkout/Block/Cart.php’);

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
anton_SH
Jr. Member
 
Total Posts:  5
Joined:  2011-08-02
 

I did this changes and also change <codePool>community</codePool> to <codePool>local</codePool> but no effect :( Can you find another reason?

 
Magento Community Magento Community
Magento Community
Magento Community
 
anton_SH
Jr. Member
 
Total Posts:  5
Joined:  2011-08-02
 

Two days I am trying to override some controller in Magento. I undenstood that I cannot override a Abstract class, so I cannot override Mage/Checkout/Block/Cart.php. Then I tried to override Mage/Checkout/controllers/CartController.php but I couldn`t do it. Can anybody help me?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

The Cart block is not abstract. You should be able to override it.
Try to change the <codePool> tag to <codepool>. I mean lowercase.
Also clear the cache after each modification.

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
vishal_2811
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2011-04-17
 

In order to override core page block (in my case to override app\code\core\Mage\Page\Block\Html\Welcome.php) follow the url below

http://www.dmertl.com/blog/?p=54

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Actually Monty that is not the best way to do it.
Here is why. Let’s say that you do that for one class and you upgrade the Magento version.
And in the upgraded version the file you copied in ‘local’ has 2 additional methods and 3 changes to other methods.
There is a big chance that your website will stop working.
For one file is manageable but if you do this for 20-30 file you’ve got chaos.

If you override them the proper way the the chance of colliding with upgraded code drops significantly.

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kaushik Patel
Jr. Member
 
Total Posts:  28
Joined:  2011-12-24
 

try this solution.
http://hkpatel201.blogspot.in/2013/03/overriderewrite-magento-core-blocks-and.html

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top