Adding a Static Block Above Shipping Methods in Magento Checkout
As a senior developer specializing in e-commerce platforms like Magento, I often encounter scenarios where clients need to insert custom content into specific checkout pages without altering core templates directly. In this article, we’ll explore how to add a CMS static block above the available shipping methods in the one-page checkout using only your local.xml file.
Understanding the Current Approach
The code snippet you provided is on the right track. It attempts to reference the ‘shipping_method’ block and append our custom ‘basket_shipping_message’ block before it. However, there are a few issues with this approach that prevent it from working correctly:
- The ‘content’ block doesn’t hold the shipping methods in Magento’s one-page checkout layout.
- The ‘before’ attribute isn’t recognized in this context.
The Correct Approach: Using Layout Handles
To achieve your goal without modifying core files, we’ll leverage Magento’s layout handles and reference the correct block where we want to insert our static block. Let me guide you through the process:
- In your local.xml file located under app/design/frontend/YOUR_PACKAGE/YOUR_THEME/layout, add or update the following code within the
<layout>section:
<checkout_onepage_index>
<reference name="root">
<block type="cms/block" name="basket_shipping_message" template="cms/block.phtml">
<action method="setBlockId"><block_id>basket_shipping_message</block_id></action>
<action method="setTemplate"><template>YOUR_CUSTOM_TEMPLATE.phtml</template></action>
</block>
</reference>
</checkout_onepage_index>
Let’s break down this code:
- We’re using the
<checkout_onepage_index>layout handle, which corresponds to the one-page checkout page. - The
<reference name="root">line tells Magento to look for the root container on the page. - Inside the ‘root’ reference, we create a new block of type “cms/block” with the name “basket_shipping_message”.
- We use the
setBlockIdaction to set the ID of our static block. In your case, it’sbasket_shipping_message. - The
setTemplateaction allows us to specify a custom template for this block. You can create a new PHTML file under app/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/cms with your desired HTML content.
- After adding the code, clear your cache using the Magento 2 command-line interface:
php bin/magento cache:flush
- Now, if you have already created a static block with the ID
basket_shipping_messageand saved it in the CMS, it should appear above the available shipping methods in the one-page checkout.
Conclusion
By following this approach, we successfully added a CMS static block to the Magento one-page checkout without modifying any core files. This technique allows for greater flexibility and easier maintenance when integrating custom content into your store’s checkout process.
Remember, using local.xml for layout modifications keeps your changes isolated from the core codebase, making it easier to upgrade or switch themes in the future. If you ever need further assistance with Magento development or have questions about other e-commerce platforms, feel free to explore our resources at LaravelCompany.com.