commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[4.x]: validate() function removes tax adjustment from line item

Open jonleverrier opened this issue 2 years ago • 1 comments

What happened?

Description

When creating a manual order, I have a module that runs the validation function on the EVENT_BEFORE_SAVE event. When validation is run, it removes the tax adjustment from the line item.

Steps to reproduce

Event::on(
    Order::class,
    Order::EVENT_BEFORE_SAVE,
    function (ModelEvent $event) {

        if (Craft::$app->request->isCpRequest) {
            $this->updateLineItemQuantity($event);
        }

    }
);

private function updateLineItemQuantity($event): void
{
    $order = $event->sender;

    // this line removes tax adjuster from line item for some reason
    // stop here if the order has errors for the admin to fix
    if (!$order->validate()) return;

}
  1. Create some required "order fields"
  2. Load module code as above
  3. Create new manual order
  4. Add/input customer and add one line item that has a price excluding tax to the order (notice the tax adjustment figure/percentage below the line item is shown when added to the order)
  5. Click "Update order", and notice how the tax line vanishes

Expected behaviour

Tax on line items to be persistent when validating an order for "errors"

Actual behavior

Tax line is not persistent when validating an order for "errors". Tax adjustment is not even in the line item snapshot.

Craft CMS version

4.5.13

Craft Commerce version

4.3.3

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

jonleverrier avatar Dec 19 '23 19:12 jonleverrier

Hi @jonleverrier

Thank you for your message, I was unable to replicate this issue with the following event code:

Event::on(Order::class, Order::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    $event->sender->validate();
});

Here is a video of the result:

https://github.com/craftcms/commerce/assets/266453/640e207c-961c-4cff-acd6-dfe37f07c2f0

Is there any other custom code running in your project that could be making a difference to the process?

Thanks

nfourtythree avatar Jan 04 '24 17:01 nfourtythree

Closing this issue for now due to not being able to replicate and no further information.

Please let us know if you have any more info and we can re-open the issue.

Thanks!

nfourtythree avatar Mar 26 '24 08:03 nfourtythree