Magento 1.9 – How to Extend an Abstract Block Class

classcoreextendmagento-1.9overrides

I'm modifying getCountryHtmlSelect() through a custom class that extends another core class that extends Mage_Checkout_Block_Onepage_Abstract. Here's the class with the customized code:

local/Mycompany/Checkout/Block/Onepage/Billing.php:

class Mycompany_Checkout_Block_Onepage_Billing extends Mage_Checkout_Block_Onepage_Billing{

    // customizing from Mage_Checkout_Block_Onepage_Abstract
    public function getCountryHtmlSelect($type){
        [INSERT CUSTOM CODE]
    }

}

For your reference, here is class Mage_Checkout_Block_Onepage_Billing:

core/Mage/Checkout/Block/Onepage/Billing.php:

class Mage_Checkout_Block_Onepage_Billing extends Mage_Checkout_Block_Onepage_Abstract{
...
}

For completeness sake, here's Mage_Checkout_Block_Onepage_Abstract:

core/Mage/Checkout/Block/Onepage/Abstract.php:

abstract class Mage_Checkout_Block_Onepage_Abstract extends Mage_Core_Block_Template{

    public function getCountryHtmlSelect($type)
    {
       [CORE CODE]
    }
}

Unfortunately, Magento isn't picking up the customized version of getCountryHtmlSelect(); it's still using getCountryHtmlSelect() in class Mage_Checkout_Block_Onepage_Abstract.

Here's my config.xml:

app/code/local/Mycompany/Checkout/etc/config.xml:

<?xml version="1.0" ?>
<config>
    <modules>
        <Mycompany_Checkout>
            <version>0.0.1</version>
        </Mycompany_Checkout>
    </modules>
    <global>
        <blocks>
            <checkout>
                <rewrite>
                    <onepage>Mycompany_Checkout_Block_Onepage</onepage>
<onepage_shipping>Mycompany_Checkout_Block_Onepage_Shipping</onepage_shipping>
                </rewrite>
            </checkout>
        </blocks>
    </global>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Mycompany_Checkout before="Mage_Checkout">Mycompany_Checkout</Mycompany_Checkout>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

Questions:

  1. What do I need to do to get Magento to use getCountryHtmlSelect() in the custom class?

  2. Rather than approaching the problem by inserting getCountryHtmlSelect() in the custom class, is it better to do this:

local/Mycompany/Checkout/Block/Onepage/Abstract.php:

abstract class Mage_Checkout_Block_Onepage_Abstract extends Mage_Checkout_Block_Onepage_Abstract{

    public function getCountryHtmlSelect($type)
    {
        [INSERT CUSTOM CODE]
    }
}

Best Answer

Through @saravanavelu's comments, I ultimately noticed that I was modifying code related to Checkout Shipping while my config.xml file had renamed Checkout Billing. Simple fix. But, I'm leaving this question since it does show how to override an abstract class in case this helps somebody else.

Here's how the relevant part of config.xml that should look:

<?xml version="1.0" ?>
<config>
   ...
                <rewrite>
                    <onepage>Mycompany_Checkout_Block_Onepage</onepage>
<onepage_billing>Mycompany_Checkout_Block_Onepage_Billing</onepage_billing>
                </rewrite>
    ...
</config>
Related Topic