How to Add Additional Text based on Selected Shipping Methods in the Checkout in Magento 2- Magento source24


Hello magento friends todays in this blog post im share with you to How to Add Additional Text based on Selected Shipping Methods in the Checkout in Magento 2.


How to Add Additional Text based on Selected Shipping Methods in the Checkout in Magento 2- Magento source24


To add additional text based on selected shipping methods in the checkout of Magento 2, you can follow these steps:


➤ Create a custom module:- 

First, create a custom module if you don't have one already. You can create it in the app/code directory of your Magento installation.


➤ Create a new block: - 

In your custom module, create a new block class that will be responsible for generating the additional text. This block will be used to fetch the text to be displayed on the checkout page.


In this example, let's assume the block class is named Custom_Module\Block\ShippingInfo.




➤ Create a template file:- 

Create a template file that will be used to render the additional text. For this example, let's call it shipping_info.phtml and place it in the app/code/Custom/Module/view/frontend/templates directory.


➤ Configure the block to be rendered:- 

Now, configure the block to be rendered on the checkout page when specific shipping methods are selected. You can achieve this by overriding the checkout_index_index.xml layout file in your custom module.


Create or edit the checkout_index_index.xml file in the app/code/Custom/Module/view/frontend/layout directory, and add the following code:


<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <block class="Custom\Module\Block\ShippingInfo" name="custom.shipping.info" template="Custom_Module::shipping_info.phtml" before="-" />
        </referenceBlock>
    </body>
</page>

This will add the Custom_Module\Block\ShippingInfo block before the checkout page content (before="-") so that it is visible on the checkout page.

➤ Implement the logic in the block:

Now, in your Custom\Module\Block\ShippingInfo block class, implement the logic to get the additional text based on the selected shipping method. For example:



<?php
namespace Custom\Module\Block;

use Magento\Framework\View\Element\Template;

class ShippingInfo extends Template
{
    /**
     * Get the additional text based on the selected shipping method.
     *
     * @return string|null
     */
    public function getShippingInfo()
    {
        // Implement your logic here to fetch the additional text based on the selected shipping method.
        // You can access the selected shipping method code using $this->_checkoutSession->getQuote()->getShippingAddress()->getShippingMethod()

        // For example, return some static text for demonstration purposes:
        $selectedShippingMethod = $this->_checkoutSession->getQuote()->getShippingAddress()->getShippingMethod();
        if ($selectedShippingMethod == 'carrier_method_code') {
            return 'Additional text for the selected shipping method.';
        }

        return null; // Return null if there is no additional text to display.
    }
}

➤ Update the template file:

In your shipping_info.phtml template file, display the additional text if it exists:


<?php
$shippingInfo = $block->getShippingInfo();
if ($shippingInfo): ?>
    <div class="shipping-info">
        <?= $shippingInfo ?>
    </div>
<?php endif; ?>

➤ Clear the cache:

After making these changes, clear the Magento cache by running the following command:


php bin/magento cache:clean


Now, when a customer goes to the checkout page and selects a shipping method (assuming you've implemented the logic in the block correctly), the additional text should appear based on the selected shipping method.

Remember to adjust the logic in the block (getShippingInfo method) according to your specific shipping method codes and the text you want to display.




Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post