Magento extend getCssJsHtml()

Magento extend getCssJsHtml()

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento extend getCssJsHtml()

Debugging Magento’s getCssJsHtml() Method: A Comprehensive Guide

Are you struggling to extend or override the getCssJsHtml() method in your Magento 1.9 CE installation? You’re not alone! This fundamental method is responsible for generating CSS, JavaScript, and RSS definitions within the HTML <head> section of your storefront pages.

Let’s dive into your specific issue and explore how to correctly extend this method.

Understanding Your XML Configuration File

Your confix.xml file seems properly configured under the app/code/local/Company/etc/ directory. It defines a new module called Company_Core with version 0.1.0, and it specifies a rewrite for the Mage_Page_Block_Html_Head model.

However, there are a few key points to note:

  • In Magento 1.x, the correct namespace for the head block is Mage_Page_Block_Html_Head, not Company_Page_block_Html_Head. Always use the official Magento namespaces.
  • The rewrite configuration should target the correct model or block. Here, it’s targeting a non-existent Mage_Catalog_Model_Product instead of the actual head block.

Overriding the getCssJsHtml() Method

Now let’s examine your PHP override file located at app/code/local/Company/Page/Block/Html/Head.php. You’ve correctly included the parent class, which is a best practice for extending Magento methods. However, there are a few critical errors that prevent your override from working:

  1. Incorrect Inheritance: Your Company_Page_block_Html_Head class inherits from Mage_Catalog_Model_Product, not from the correct parent Mage_Page_Block_Html_Head. This inheritance error prevents your custom logic from being executed.

  2. Method Name Mismatch: The original method in Magento is named getCssJsHtml(), but you’re trying to override a non-existent method called getJsHtml(). Always ensure the method names match exactly when extending or overriding.

  3. Echo Statement: You’ve used an echo statement inside your overridden method, which will output the string ‘asd’ directly to the browser. However, this approach won’t work because Magento uses a return value for this method, not an echo statement. Always return values from methods in Magento.

The Correct Approach

To correctly extend or override the getCssJsHtml() method, follow these best practices:

  1. Correct Inheritance: Ensure you inherit from the correct parent class. In your case, that would be Mage_Page_Block_Html_Head.

  2. Method Name Matching: Always ensure the method names in your override match exactly with those in the original Magento file.

  3. Return Values: Use return statements to pass back values from your overridden methods, not echo or other output functions.

  4. Recompile Your Configuration: After making changes to your XML configuration files, always recompile your Magento configuration using the command line:

    php shell/compiler.php
    

A Working Example

Let’s create a working example based on your current structure:

// app/code/local/Company/Page/Block/Html/Head.php
class Company_Page_Block_Html_Head extends Mage_Page_Block_Html_Head {
    /**
     * Get HEAD HTML with CSS/JS/RSS definitions
     *
     * @return string
     */
    public function getCssJsHtml()
    {
        $html = parent::getCssJsHtml();
        // Add your custom logic here, for example:
        $html .= '<link rel="stylesheet" href="' . $this->getSkinUrl('css/custom.css') . '" type="text/css" media="all" />';
        return $html;
    }
}

In this example:

  • We inherit correctly from Mage_Page_Block_Html_Head.
  • The method name matches the original Magento method exactly.
  • The parent’s method is called using parent::getCssJsHtml() to ensure the core functionality remains intact.
  • Your custom CSS file is added to the HTML returned by the method.

Conclusion

Debugging and extending Magento methods like getCssJsHtml() can be challenging, but with a deep understanding of Magento’s architecture and the correct approach, you can successfully override behavior and add your own logic.

Remember:

  1. Always use the correct namespaces.
  2. Inherit from the right parent classes.
  3. Ensure method names match exactly.
  4. Return values instead of echoing them directly.
  5. Recompile your Magento configuration after making XML changes.

By following these guidelines, you’ll be able to extend and override Magento methods effectively, enhancing your store’s functionality without compromising its stability or performance.

For more advanced topics on Magento development and customization, I recommend exploring the extensive resources available at LaravelCompany.

Magento 1 Catalog Extensions Themes

Our network