Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

rioug
Copy link
Collaborator

@rioug rioug commented Dec 10, 2024

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! ) :

before_save :update_shipping_fees!, if: :complete?

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 the before_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:

  • Create a shipping method with a fee using the flat percent calculator.

As a customer:

  • start an order with that shipping method and proceed to step three (order summary) of the checkout but don't complete the order.
  • Take note of the order total and the shipping fee.
  • Continue shopping, i.e. click on the shop's name next to the cart icon in the top right corner.
  • Add one or more products to your cart.
  • Click checkout again - you should land on step 1, continue checking out using the shipping method set up earlier
  • Take note of the order total and the shipping fee - both should be updated correctly, including your added products.
  • Complete the order.
  • Compare the numbers of order total and shipping fee again.
    --> 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

  • Pick a completed order in the backoffice
  • Update a line item quantity or add a new variant
  • Check the shipping fees have been updated

As an enterprise manager, in the backoffice

  • Create a new order
  • Choose the shipping method with a flat percent calculator created a the start
    --> 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):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

#12954 needs to be merged first

rioug added 6 commits December 4, 2024 16:30
`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
@rioug rioug marked this pull request as ready for review December 10, 2024 04:27
@rioug rioug added the user facing changes Thes pull requests affect the user experience label Dec 10, 2024
@rioug rioug changed the title 12907 fix checkout shipping fee [Checkout] Shipping fees update, remove order callback Dec 10, 2024
Copy link
Member

@mkllnk mkllnk left a 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.

Comment on lines 455 to 456
order2.contents.update_item(Spree::LineItem.where(order_id: order2.id).first,
{ quantity: 5 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 })

Copy link
Member

@dacook dacook left a 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 👍

Comment on lines +1250 to +1251
item_num = order.line_items.sum(&:quantity)
expect(order.reload.adjustment_total).to eq(item_num * shipping_fee)
Copy link
Member

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user facing changes Thes pull requests affect the user experience
Projects
Status: Test Ready 🧪
Development

Successfully merging this pull request may close these issues.

3 participants