Get Free Audit

How to Use Total Models in Magento 2

Oct 30, 2018

20990 Andrey Litvin

How to Use Total Models in Magento 2

Custom totals help you modify the calculation process in the shopping cart. They can be used to add an extra tax or discount to Magento Checkout or modify the existing ones and they are later applied to the products in the shopping cart. BTW, this topic is included into the Magento 2 professional developer certification exam.

Adding Custom Total

The first thing we need to do is create an app/code/Vendor/Module/etc/sales.xml file in our module. This file is used to register all the available totals in Magento 2.

app/code/Vendor/Module/etc/sales.xml:


Here we specified the \Vendor\Module\Model\Totals\Custom class as a model of our custom total. This class must inherit \Magento\Quote\Model\Quote\Address\Total\AbstractTotal and also implement the collect and fetch methods. The collect method is used to calculate the value of our total, and the fetch method returns this value, as well as the total’s code and its name.

The \Magento\Quote\Model\Quote\Address\Total $total parameter also allows you to affect the result of the other total classes. But depending on the task, it may make more sense to use plugins to modify their values.

app/code/Vendor/Module/Model/Total/Custom.php:

Displaying Custom Total

Cart and checkout pages

Knockout.js is used for displaying totals. In order for our total to appear on the cart and checkout pages, we need to add a new js component to checkout_cart_index.xml and checkout_index_index.xml layouts.

app/code/Vendor/Module/view/frontend/layout/checkout_cart_index.xml:


app/code/Vendor/Module/view/frontend/layout/checkout_index_index.xml

Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


And also write the components and HTML templates:

app/code/Vendor/Module/view/frontend/web/js/view/checkout/cart/totals/custom_total.js


app/code/Vendor/Module/view/frontend/web/template/checkout/cart/totals/custom_total.html


app/code/Vendor/Module/view/frontend/web/js/view/checkout/summary/custom_total.js


The getValue and getPureValue methods return the value of our custom total, but the getValue method formats this value by adding two decimal digits and a symbol of the current currency.

app/code/Vendor_Module/view/frontend/web/template/checkout/summary/custom_total.html

Order Emails

To make a new total display in the order email you need to add a new block in the sales_email_order_items layout.

app/code/Vendor/Module/view/frontend/layout/sales_email_order_items.xml


app/code/Vendor/Module/Block/Order/CustomTotal.php


That is it, thank you for reading. Leave your comments and questions below and good luck at the exam!

one_page_checkout

One Page Checkout with Cart

Take your online store to the next level with BelVG module

Download here

Check out our great Ajax Cart extension to improve the quality of customer experience.

Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

6 Comments

  1. Hi, Nicolas. Thanks for the comment.
    There are a lot of things that can be the reason it doesn’t work. The easiest way to diagnose them is by taking the standard Total field. For example Discount. Then, look at how it was added in the current version of Magento and compare with what you have.

  2. So I’ve tried out your example in here, but I figured out two things; The first one is the typo with Total and Totals in the Custom.php. And second, it compiles through and stuff, but it doesn’t do anything sadly. And I don’t really get why :( No exceptions, etc. Just nothing displayed.

  3. Hi Parth
    Thanks for your question!
    Usually the custom total is copied from quote to order page.

  4. Hi, how can we display the custom total on view order page ? Will this total be copied from quote to order or do we have to write fieldset for copying it to the order ? Thanks in advance.

  5. Hi, can I ask a sample codes on how to create a table/page in frontend custom module using Model, Resource Model in Magento 2 with Insert, Delete, Edit functions. Thank you very much Sir!

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *