-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Commerce 3.x: Purchasing products with stock quantity of 1 causing overpayment and missing items in completed order (after upgrade from v2 > v3) #1431
Comments
What is the recalculation mode of the order when you save it in your event listener? Commerce 3 introduced a recalculation mode for orders, and no longer just relies on the 'isCompleted' flag to determine if the order should recalculate. I am guessing it is set to the 'recalculate all' mode? You can check that that with:
My guess is that since you are saving the order, the order is being recalculated (depending on the recalculation mode set) and the line items that no longer have stock are removed. You need to put the order into "Recalculate None" Mode before saving the order, this will force the order to not change any line items or values other than your custom field.
Let me know if that makes sense. |
I haven’t touched the recalculation mode, so it will be whatever the default is. I’ll try what you suggest and sounds like that should work.
Thanks!
…Sent from my iPhone
On 4 May 2020, at 09:24, Luke Holder ***@***.***> wrote:
What is the recalculation mode of the order when you save it in your event listener? Commerce 3 introduced a recalculation mode for orders, and no longer just relies on the 'isCompleted' flag to determine if the order should recalculate.
I am guessing it is set to the 'recalculate all' mode? You can check that that with:
$order->getRecalculationMode()
My guess is that since you are saving the order, the order is being recalculated (depending on the recalculation mode set) and the line items that no longer have stock are removed.
You need to put the order into "Recalculate None" Mode before saving the order, this will force the order to not change any line items or values other than your custom field.
$originalRecalculationMode = $order->getRecalculationMode();
$this->setRecalculationMode(\craft\commerce\elements\Order::RECALCULATION_MODE_NONE);
Craft::$app->elements->saveElement($order);
$order->setRecalculationMode(originalRecalculationMode)
Let me know if that makes sense.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Just to confirm, this has fixed it.
However looking at the upgrade guide, which I had read before upgrading — I
think this potentially breaking change could be made clearer, as this
default behavior has changed. As it only causes issues when products are on
their final stock, it was also something which was easily missed when we
tested after upgrade.
Appreciate your for your speedy response though, thank you!
…On Mon, May 4, 2020 at 9:31 AM Jack Wild ***@***.***> wrote:
I haven’t touched the recalculation mode, so it will be whatever the
default is. I’ll try what you suggest and sounds like that should work.
Thanks!
Sent from my iPhone
On 4 May 2020, at 09:24, Luke Holder ***@***.***> wrote:
What is the recalculation mode of the order when you save it in your event
listener? Commerce 3 introduced a recalculation mode for orders, and no
longer just relies on the 'isCompleted' flag to determine if the order
should recalculate.
I am guessing it is set to the 'recalculate all' mode? You can check that
that with:
$order->getRecalculationMode()
My guess is that since you are saving the order, the order is being
recalculated (depending on the recalculation mode set) and the line items
that no longer have stock are removed.
You need to put the order into "Recalculate None" Mode before saving the
order, this will force the order to not change any line items or values
other than your custom field.
$originalRecalculationMode = $order->getRecalculationMode();
$this->setRecalculationMode(\craft\commerce\elements\Order::RECALCULATION_MODE_NONE);
Craft::$app->elements->saveElement($order);
$order->setRecalculationMode(originalRecalculationMode)
Let me know if that makes sense.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1431 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABL7YD25ANB4AVQADZLF64DRPZ3URANCNFSM4MYC26QA>
.
|
Thanks, added it to the docs, let me know if something is unclear: https://docs.craftcms.com/commerce/v3/upgrading.html#order-recalculations |
Thanks Luke that's really helpful. One thing — in the example do you have
to saveElement before and after setting the recalculation mode? I think I
had an issue with this and it was fixed if I did the following (although
perhaps I have 1 more saveElement than I need here):
```
$originalRecalculationMode = $order->getRecalculationMode();
$this->setRecalculationMode(\craft\commerce\elements\Order::RECALCULATION_MODE_NONE);
Craft::$app->elements->saveElement($order);
$order->setFieldValues(['foo' => 'bar']);
Craft::$app->elements->saveElement($order);
// restore what it so the current code raising this event works correctly.
$order->setRecalculationMode($originalRecalculationMode);
Craft::$app->elements->saveElement($order);
```
…On Mon, May 4, 2020 at 11:39 AM Luke Holder ***@***.***> wrote:
Closed #1431 <#1431>.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1431 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABL7YD4WJ5GEAWMCPVXCFQTRP2LMRANCNFSM4MYC26QA>
.
|
No you only need to save the order once to save your custom field values. $originalRecalculationMode = $order->getRecalculationMode();
$this->setRecalculationMode(\craft\commerce\elements\Order::RECALCULATION_MODE_NONE);
$order->setFieldValues(['foo' => 'bar']);
Craft::$app->elements->saveElement($order);
$order->setRecalculationMode($originalRecalculationMode); After setting the order back, you are only doing that for the purpose of putting it back to the original mode in memory, incase other parts of the application expect it to be in a certain mode during that request. |
Description
When purchasing final item of stock, items are included in the payment price, but are not added to the order. This has caused quite a few issues with overpayments and missing order items.
It appears that this happens when hooking into the
Payments::EVENT_AFTER_PROCESS_PAYMENT
event.Steps to reproduce
In a custom module I have the following:
I have modified my code to instead use the
Order::EVENT_AFTER_ORDER_PAID
, and this works fine, but keen to get to the bottom of why this was happening.The old code has been working without any issues for a couple of months, and the issue started as soon as we upgraded to Craft Commerce 3.
Additional info
The text was updated successfully, but these errors were encountered: