How to add title to total_segments section of javascript totals object In Magento 2

How to add title to total_segments section of javascript totals object In Magento 2

September 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add title to total_segments section of javascript totals object In Magento 2

Setting the Title for Custom Totals in Magento 2

In Magento 2, adding a title to the total_segments section of your JavaScript totals object can be achieved by following these steps.

Understanding the Issue with Your Current Setup

The issue lies in how you are setting and retrieving the title of your custom total. In your current implementation:

public function fetch(Quote $quote, Total $total)
{
    //...
    return  [
        'code' => $this->getCode(),
        'value' => $fee,
        'title' => 'test'
    ];
}

You are correctly setting the title key in your array, but for some reason, it’s not being reflected in the JavaScript object.

The Key to Solving This: Magento’s Total Management

Magento 2 has a specific way of managing totals. When you create a new total, it expects certain methods and properties to be implemented or set.

In your case, you need to ensure that the getLabel method is properly implemented in your custom total class.

Implementing the getLabel Method

The getLabel method should return the title for your total as a string. Here’s how you can implement it:

public function getLabel()
{
    return $this->title;
}

You also need to set the $title property in your constructor or any other relevant place where you initialize your custom total object.

Setting the Title

To set the title, add this line in your constructor:

public function __construct(Client $client)
{
    //...
    $this->title = 'Oversized Shipping Fee';
}

Replace 'Oversized Shipping Fee' with whatever title you want to display for your custom total.

After making these changes, the getLabel method should return the correct title, and it will be reflected in the JavaScript totals object when the page is rendered.

Adding Additional Keys

To add additional keys like a “sku” value:

return  [
    'code' => $this->getCode(),
    'value' => $fee,
    'title' => $this->getLabel(),
    'sku' => 'SKU-VALUE'
];

Ensure that the key names match exactly what Magento expects for proper rendering in the frontend.

Conclusion

Setting a title (or label) for your custom totals in Magento 2 requires implementing the getLabel method and setting the corresponding property. This is a common requirement when developing custom extensions or adding new totals.

Remember to test your implementation thoroughly after making these changes, especially if you have other parts of your code that depend on this total being set correctly.

For more detailed guides on Magento 2 development, I recommend checking out our comprehensive guides at Laravel Company.

Magento 2 Configuration Errors JavaScript

Our network