Understanding Magento Layout System: Adding Custom Block to Shipping Methods Page via Module
Hello fellow developers! Today, I’m going to guide you through adding a custom block of HTML to the shipping methods section of the checkout page in Magento - all while keeping your hands clean from core file modifications.
Understanding the Layout System
Before we dive into code, let’s quickly recap how the layout system works. Magento uses XML files (called layout updates) to define the structure and content of pages. These layouts are applied based on the current page being viewed.
Your Current Setup: What’s Missing?
You’re almost there with your setup! Let me point out a couple of things that might be causing the issue:
-
Reference Name: The reference name you’re using is correct -
checkout.onepage.shipping_method.available. This points to the block where shipping methods are displayed. -
Block Type: You’ve defined the block type as
mymodule/mymodule, but this won’t work because you haven’t created a block class in your module.
Creating the Block Class
To create a block, you need to define a PHP class that extends the Mage_Core_Block_Template or Magento\Framework\View\Element\Template class. Here’s a basic example:
<?php
namespace Brisqq\Cart\Block;
class MyModule extends \Magento\Framework\View\Element\Template
{
public function __construct(\Magento\Framework\View\Element\Context $context, array $data = [])
{
parent::__construct($context, $data);
}
}
Including CSS and JS
To include your custom stylesheet or script in this specific page:
- CSS: Create a file named
requirejs-config.jsin the following location:<your_module_dir>/view/frontend/requirejs-config.js. Add the following code to include your CSS:
var config = {
map: {
'*': {
mymodule_css: 'Brisqq_Cart/css/mymodule.css'
}
}
};
- JS: Follow a similar approach for JS files, but place them in the
web/jsfolder of your module.
Firing jQuery Function After Shipping Methods Load
To fire a jQuery function after the shipping methods step is loaded from the custom module:
- Include your script file using the
requirejs-config.jsmethod mentioned above. - In your JavaScript file, use an event listener to capture the
contentUpdatedevent fired by Magento’s checkout component.
require(['jquery', 'mage/url'], function($) {
$(document).on('contentUpdated', function() {
// Your jQuery function here
});
});
Conclusion
In summary, to add a custom block to the shipping methods page in Magento:
- Create a valid block class that extends the template block.
- Use the correct reference name and block type in your layout XML file.
- Include your CSS using
requirejs-config.jsin the frontend view folder. - Fire jQuery functions after content updates using event listeners.
Remember, keeping everything in your custom module ensures that changes won’t be overwritten during Magento upgrades. This approach follows best practices for extending Magento functionality without touching core files.
For more detailed guides and real-world examples of Magento development, I highly recommend checking out the resources available at Laravel Company.
Happy coding! Let me know if you have any other questions or need further clarification.