Magento 2 - How to Display Minimum Order Amount Message on Minicart

While Magento typically shows the Minimum Order Amount Message exclusively on the shopping cart page by default, some clients express the desire to include this message in the Minicart as well. In this article, we'll guide you on adding the Minimum Order Amount Message to the Minicart.

Before we begin, I assume that you have already created a custom module. If you haven't done so or are unfamiliar with the process, please refer to our separate article on How To Create a Magento 2 Module.

Step 1: Create di.xml file at app/code/Devhooks/MinOrderAmount/etc/frontend/ and paste the below code.

saveCopyzoom_out_map
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Checkout\CustomerData\Cart"> <plugin name="custom_minicart_content" type="Devhooks\MinOrderAmount\Plugin\CustomerData\Cart"/> </type> </config>

As you can see in the above code, we've created the plugin for getSectionData method of Magento\Checkout\CustomerData\Cart class. So, now we're creating a Cart.php class file.

Step 2: Create Cart.php file at app/code/Devhooks/MinOrderAmount/Plugin/CustomerData/Cart.php and paste the below code. You can change the code as per your requirement on afterGetSectionData method.

saveCopyzoom_out_map
<?php namespace Devhooks\MinOrderAmount\Plugin\CustomerData; use Magento\Checkout\Helper\Cart as cartHelper; use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage; class Cart { /** * @var cartHelper * */ protected cartHelper $cartHelper; /** * @var ValidationMessage */ protected ValidationMessage $minOrderValidationMessage; /** * Constructor method * * @param ValidationMessage $minOrderValidationMessage * @param cartHelper $cartHelper */ public function __construct( ValidationMessage $minOrderValidationMessage, cartHelper $cartHelper ) { $this->minOrderValidationMessage = $minOrderValidationMessage; $this->cartHelper = $cartHelper; } /** * Constructor method * * @param \Magento\Checkout\CustomerData\Cart $subject * @param array $result * @return array */ public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, array $result) { $result['min_order_message'] = ''; if (!$this->cartHelper->getQuote()->validateMinimumAmount()) { $result['min_order_message'] = $this->minOrderValidationMessage->getMessage(); } return $result; } }

You can see we've injected the Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage class to validate the amount and get the messages based on the configuration of the Minimum order amount on afterGetSectionData method.

Now we need to override the Magento_Checkout/template/minicart/content.html in our module and add the message.

Step 3: To override content.html file we need to create requirejs-config.js file at app/code/Devhooks/MinOrderAmount/view/frontend/ and paste the below code.

saveCopyzoom_out_map
var config = { map: { '*': { 'Magento_Checkout/template/minicart/content.html': 'Devhooks_MinOrderAmount/template/minicart/content.html' } } }

Step 4: Copy vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html file and paste it at app/code/Devhooks/MinOrderAmount/view/frontend/web/template/minicart/

Now, add the below code in content.html file whereever you want to display the minimum order amount message.

saveCopyzoom_out_map
.... <!-- ko if: getCartParam('summary_count') > 0 && getCartParam('min_order_message') != '' --> <div class="message message-notice notice"> <span class="min_order_config" text="getCartParam('min_order_message')"></span> </div> <!--/ko--> ....

In above code, we've use the min_order_message which we've added on our plugin also check the condition before showing this message. You can modify this code as per your requirement.

That's it!!

Make sure you've enabled the below configuration and set the minimum order amount as per your requirement. Please review the below screenshot for your reference.

Stores > Configuration > Sales > Sales > Minimum Order Amount

Magento 2 Minimum Order Amount Configuration

After making these changes, run the below commands and review your changes.

saveCopyzoom_out_map
php bin/magento c:c; php bin/magento s:up; php bin/magento s:s:d -f;

Now, add products to cart and you'll see the message like the below screenshot.

Magento 2 Add Minimum Order Amount Message on Minicart

We hope this Magento post helped you to know How to Display Minimum Order Amount Message on Minicart in Magento 2.

Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off