craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
215 stars 170 forks source link

[5.x]: OrderAdjustment not included in tax calculation #3587

Closed HigumaSan4050 closed 1 month ago

HigumaSan4050 commented 1 month ago

What happened?

Description

When i add an OrderAdjuster, the amount ist not included in the tax calculation.

Added the OrderAdjustment with following event

Event::on(
    OrderAdjustments::class,
    OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
    function(RegisterComponentTypesEvent $event) {
    }
);

If it is not possible, could we get the private functions from the tax adjuster "craftcms\commerce\adjusters\Tax" as protected functions, so we can reuse them in child classes?

or an event before the tax adjustment is created

Craft CMS version

5.2.6

Craft Commerce version

5.0.12.1

PHP version

8.3

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

linear[bot] commented 1 month ago

PT-1952 [5.x]: OrderAdjustment not included in tax calculation

nfourtythree commented 1 month ago

Hi @HigumaSan4050

Unfortunately, we are unable to replicate this issue. I have tried with the following code and the adjustment is showing as tax correctly.

// In Module
Event::on(OrderAdjustments::class, OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS, function(RegisterComponentTypesEvent $event) {
    $event->types[] = MyOrderAdjuster::class;
});
// Custom adjuster class
namespace modules\adjusters;

use craft\commerce\adjusters\Tax;
use craft\commerce\elements\Order;
use craft\commerce\models\OrderAdjustment;

class MyOrderAdjuster extends Tax
{
    public function adjust(Order $order): array
    {
        $return = parent::adjust($order);

        $adjustment = new OrderAdjustment();
        $adjustment->type = self::ADJUSTMENT_TYPE;
        $adjustment->name = 'Foo';
        $adjustment->description = 'Bar';
        $adjustment->setOrder($order);
        $adjustment->isEstimated = false;
        $adjustment->sourceSnapshot = [];
        $adjustment->amount = 10;

        $return[] = $adjustment;

        return $return;
    }
}

Please let us know if you have any further information that will help diagnose the issue you are seeing.

Thanks.

HigumaSan4050 commented 1 month ago

Sorry for the misunderstanding.

I create a discount adjustment but this is not taken into account in the tax calculation

nfourtythree commented 1 month ago

Hi @HigumaSan4050

Thank you for the extra info. If you are registering a discount adjuster you should be using the OrderAdjustments::EVENT_REGISTER_DISCOUNT_ADJUSTERS event.

This will register your class alongside the core discount adjuster so it is run at the same time.

Hopefully this helps, thanks!

HigumaSan4050 commented 1 month ago

Hi @nfourtythree

After changing the event my adjuster is no longer available in the cart.

\craft\commerce\services\OrderAdjustments->getAdjusters() returns

Array (
  [0] => craft\commerce\adjusters\Shipping
  // my Discount Adjuster added with the event OrderAdjustments::EVENT_REGISTER_DISCOUNT_ADJUSTERS
  [1] => modules\testmodule\adjusters\FooBarAdjuster 
  [2] => craft\commerce\adjusters\Discount
  [3] => craft\commerce\adjusters\Tax
) 

but when i use cart.orderAdjustments i got only the default shipping and tax adjustments.

my adjuster registration

Event::on(
    OrderAdjustments::class,
    OrderAdjustments::EVENT_REGISTER_DISCOUNT_ADJUSTERS,
    function(RegisterComponentTypesEvent $event) {
        $event->types[] = FooBarAdjuster::class;
    }
);

my adjustmentClass

class FooBarAdjuster extends \craft\commerce\adjusters\Discount
{
    public function adjust(Order $order): array
    {
        $adjustments = [];

        $adjustment = new \craft\commerce\models\OrderAdjustment();
        $adjustment->type = self::ADJUSTMENT_TYPE;
        $adjustment->name = 'Foo';
        $adjustment->description = 'discount';
        $adjustment->setOrder($order);
        $adjustment->amount = 100;

        $adjustments[] = $adjustment;

        return $adjustments;
    }
}

cant find any error. the function adjust of my adjuster is called.

using the event EVENT_REGISTER_ORDER_ADJUSTERS and the adjustment is added but not included in the tax calculation

nfourtythree commented 1 month ago

Hi @HigumaSan4050

Discount order level adjustments are spread across the line items on the cart. This is done to make sure all calculations add up correctly and that the cart is in an understandable format for all gateways.

So you should see your discount be applied to one or more of the line items (depending on how much discount is applied).

Also to note, you discount amount would need to be a minus number.

Hope this helps, thanks!

HigumaSan4050 commented 1 month ago

Hi @nfourtythree

thanks for the hint.

it's actually a surcharge. adding the discount adjustment to an lineitem the calculation works.

nfourtythree commented 1 month ago

Thanks for letting me know, glad things are working.