0

I need to count on decimal places (tax, promotion, fee ...), but the total price must be mathematically rounded without decimals.

Example:
2 x product1 CZK 424.00
1 x Product2 CZK 408.00

Subtotal CZK 832.00
Shipping CZK 99.00
Discount CZK -41.60
10% VAT CZK 71.86
21% VAT CZK 17.18
-Total CZK 889.40- remove
Rounded CZK - 0.60 it is not necessary to display
Total CZK 889.00

1

this solved my problem: https://www.drupal.org/project/commerce/issues/3076196

my module:

roundingmodule.info

name: Commerce rounding module.
type: module
description: Rounding.
core: 8.x
package: Custom

roundingmodule.services.yml

services:
  roundingmodule.rounding_order_processor:
    class: Drupal\roundingmodule\RoundingOrderProcessor
    tags:
      - { name: commerce_order.order_processor, priority: -10 }

roundingmodule.commerce_adjustment_types.yml

rounding:
  label: 'Rounding'
  singular_label: 'rounding adjustment'
  plural_label: 'rounding adjustments'
  has_ui: true
  weight: 20

src/RoundingOrderProcessor.php

    <?php

namespace Drupal\roundingmodule;

use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\OrderProcessorInterface;

/**
 * Processes the order's shipments.
 */
class RoundingOrderProcessor implements OrderProcessorInterface {

  /**
   * {@inheritdoc}
   */
  public function process(OrderInterface $order) {
   
      $rounder = \Drupal::service('commerce_price.rounder');
      $order_total = $order->getTotalPrice();
      $rounded_total = $rounder->round($order_total->divide(100))->multiply(100);
      $difference = $rounded_total->subtract($order_total);

      $order->addAdjustment(new Adjustment([
        'type' => 'rounding',
        'label' => 'Rounding',
        'amount' => $difference,
      ]));
  }

}
| improve this answer | |

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