-
-
Notifications
You must be signed in to change notification settings - Fork 725
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
[Checkout] Shipping fees update, remove order callback #13023
base: master
Are you sure you want to change the base?
[Checkout] Shipping fees update, remove order callback #13023
Conversation
`Spree::OrderContents#update_or_create` is used to update the cart when on the /shop page. If you start an order and proceed to the "Order summary" step, and then decide to update your order by using the shop link next to the cart, such update wouldn't update the shipment. This result in the order page in the backoffice displaying the wrong data, and more importantly, in the stock not being updated. So now we ensure shipment will be updated, which result in the checkout flow being restarted, thus making sure the shipment is updated.
It should be handled in the controller, it's currently handled in `Spree::OrderContents#remove`. As long as we don't manually remove line item from an order we should be good. Currently it gets trigerred each time the order is saved, which seems to happen mutiple time when we finalize an order. It's a bit useless to recalculated the fees over and over Context, it was added here : openfoodfoundation@217eda8
And update related specs
User Order::Contents#update_item to update line item on an order, it ensures the order is properly updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. That should speed up the app.
spec/system/admin/orders_spec.rb
Outdated
order2.contents.update_item(Spree::LineItem.where(order_id: order2.id).first, | ||
{ quantity: 5 }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
order2.contents.update_item(Spree::LineItem.where(order_id: order2.id).first, | |
{ quantity: 5 }) | |
order2.contents.update_item(Spree::LineItem.find_by(order: order2), { quantity: 5 }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
It seems more reasonable to be enforcing this at the controller level 👍
item_num = order.line_items.sum(&:quantity) | ||
expect(order.reload.adjustment_total).to eq(item_num * shipping_fee) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we're using test data, we should be able to specify the actual amounts here, which would be less error-prone than using a calculation. But let's not bother updating now..
What? Why?
Now that checkout workflow is restarted when an order is updated, it fixes the issue with shipping fee calculation. That said, while investigating the issue, I realised shipment fees were recalculated multiple times when completing an order , which seemed unnecessary. I tracked down the issue to a callback ( 😮 surprise! 😮 surprise! ) :
openfoodnetwork/app/models/spree/order.rb
Line 109 in ee2a6bf
Given that we have https://github.com/openfoodfoundation/openfoodnetwork/blob/master/app/models/spree/order_contents.rb that should be used to managed adding/updating/removing an order's line item.
Spree::OrderContents
already handle updating shipment and shipment fees, so now thebefore_save
callback becomes redundant.Drawback : shipment fees won't be updated is someone directly update a line item on an order. There isn't anyway to prevent that (as far as I know), we (as the dev team) need to enforce the use
Spree::OrderContents
for any line item update.What should we test?
As an enterprise manager:
As a customer:
--> they should be matching.
Same scenario as above but this time once on step 3 of checkout, use "Edit" link next to "Order Details" to update your order
As an enterprise manager
As an enterprise manager, in the backoffice
--> check the shipping fee is calculated correctly
-- Add or update a line item in the order
--> check the shipping fee is updated correctly
Release notes
Changelog Category (reviewers may add a label for the release notes):
The title of the pull request will be included in the release notes.
Dependencies
#12954 needs to be merged first