Magento Ajax Static Block Call: Displaying CMS Content Dynamically
Hello Magento developers! I understand your challenge in dynamically loading CMS static blocks using Ajax when clicking on footer links. Let’s dive into a step-by-step solution that maintains the clean separation of concerns between PHP and JavaScript.
The Current Challenge
You’re currently facing two main obstacles:
-
Mixing PHP and JavaScript: Embedding PHP code inside your JavaScript function is not recommended, as it breaks the script execution flow in modern browsers.
-
Capturing the Block ID: You need to capture the static block ID from the clicked footer link and pass it to your JavaScript function without using
GETparameters or server-side processing.
The Proposed Solution
Let’s create a clean, efficient solution using Magento’s built-in capabilities and some clever use of data attributes:
- Define Your Static Blocks
First, ensure you have created the necessary CMS static blocks in the admin panel to hold your content. Let’s say you have three blocks: about, payment_methods, and delivery_info.
- Set Up Your Footer Links
In your footer template file (usually located at app/design/frontend/<Vendor>/<Theme>/Magento_Theme/templates/html/footer.phtml), create the required links with unique data attributes:
<!-- TRIGGER -->
<li>
<a href="javascript:void(0)" data-block-id="about">About</a>
</li>
<li>
<a href="javascript:void(0)" data-block-id="payment_methods">Payment Methods</a>
</li>
<li>
<a href="javascript:void(0)" data-block-id="delivery_info">Delivery Info</a>
</li>
<!-- CONTAINER -->
<div id="infoShow">
<section></section>
</div>
- Create the JavaScript Function
Now, let’s create a JavaScript function that can access the data attribute of the clicked link without requiring a server-side request:
<script type="text/javascript">
function abreInfo(event) {
event.preventDefault(); // Prevent default link behavior
var blockId = $(this).data('block-id'); // Capture the static block ID from the data attribute
var container = $('#infoShow');
// Slide down the container and animate scrolling to it
container.slideDown('fast', function () {
$('html,body').animate({ scrollTop: container.offset().top - 54 }, 200);
// Load the static block content into the container section
container.find('section').load(blockId);
});
}
</script>
- Attach the Function to the Links
Finally, attach the abreInfo function to all footer links with a data attribute using jQuery:
<script type="text/javascript">
// Attach the abreInfo function to each footer link
$(document).on('click', 'a[data-block-id]', abreInfo);
</script>
Conclusion
By following this approach, you maintain a clean separation between PHP and JavaScript while efficiently loading Magento CMS static blocks using Ajax. This method allows for better performance, as the content is loaded dynamically without refreshing the entire page.
Remember to test your implementation thoroughly in various browser environments to ensure consistency across different devices and platforms.
Further Reading
For more information on working with CMS static blocks in Magento 2, I recommend checking out this comprehensive guide: Managing CMS Pages in Magento 2 by Laravel Company. It covers best practices for managing content and integrating it into your store.
Happy coding! Let me know if you have any questions or need further assistance.