My products have a base price, but each product can have additional fees, like costs for the packaging or for special delivery conditions (Dangerous Goods Fee). How can I calculate the final product price dynamically by using all my costs?


  1. Create a sales.xml file under <NAMESPACE>/<MODULE_NAME>/etc folder and add the below code over there.

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <section name="quote">
        <group name="totals">
            <item name="customer_discount" instance="<NAMESPACE>\<MODULE_NAME>\Model\Total\Quote\<CLASS_NAME>" sort_order="420"/>

  2. Now create the class on the above mention path.

    namespace Vendor\Module\Model\Total\Quote;
    class CLASS_NAME extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
    protected $priceCurrency;
    public function __construct(
            \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
        ) {
            $this->priceCurrency = $priceCurrency;
    public function collect(
            \Magento\Quote\Model\Quote $quote,
            \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
            \Magento\Quote\Model\Quote\Address\Total $total
        ) {
        $address = $shippingAssignment->getShipping()->getAddress();
        if ($address->getAddressType() != 'shipping') {
            return $this;
        parent::collect($quote, $shippingAssignment, $total);
        $baseDiscount = 10;
        $discount = 10;
        $total->addTotalAmount('customer_discount', -$discount);
        $total->addBaseTotalAmount('customer_discount', -$baseDiscount);
        return $this;
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total) {
        if ($quote->getActualMycardDiscount()) {
            $data = [
            'code' => 'customer_discount',
            'title' => 'customer discount',
            'value' => 10
            return $data;
  3. You need to generate dynamic value for $baseDiscount and $discount.

  • I want to explain more what I need. I have added two price attributes: cost_packaging and cost_transport_dangerous_good. No I want to us as product price: price + cost_packaging + cost_transport_dangerous_good. This calculated price should be used for the whole order process and should replace the default product price May 30 '20 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.