Display Cart Total To MiniCart

Do you need to display additional data to Mini Cart like the cart total? Then this post will definitely help you with that!

As is well known Magento 2 uses sections which are initialised with AJAX requests to customer/section/load, taking as a parameter the section name (key) we want to load. In our case will be the cart customer/section/load?sections=cart. The cart section on server side is initialised via the class Magento\Checkout\CustomerData\Cart. In that class we can find the function getSectionData which is setting the data to the section cart. So we will need to extend that class adding our cart total value. We do that by defining creating a plugin like below adding the cart total in the key grand_total.

Defining the plugin

Creating the plugin

Then we will need to display our car total value in the frontend. So we will need to extend the checkout_cart_sidebar_total_renderers.xml adding our total component. In our example we will not add a new container only for the total, but we will extend the current subtotal container like below.

After defining our component and it's template we will have to create them.

In frontend the result after adding a product to cart will be the following.

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge