[4.x]: validate() function removes tax adjustment from line item
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;
}
- Create some required "order fields"
- Load module code as above
- Create new manual order
- 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)
- 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
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
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!