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

Close adjustments so that updates are effective #3666

Merged
merged 1 commit into from
Apr 10, 2019
Merged

Close adjustments so that updates are effective #3666

merged 1 commit into from
Apr 10, 2019

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Mar 29, 2019

What? Why?

Closes #1830

Spree 2.0 keeps shipping fee adjustments open unless they are manually closed. But open adjustments cannot be edited. To preserve updates, like changing the amount of the shipping fee we close the adjustment first.

What should we test?

  1. Place an order.
  2. Log in as admin and go to the order.
  3. Observe the shipping fee and total amount.
  4. Click on Adjustments.
  5. Click on the edit icon of the shipping fee.
  6. Enter a different amount and click Continue.
  7. Check the amount was updated.
  8. Click on Order Details and check the shipping fee and totals have updated.

How is this related to the Spree upgrade?

Part of the Spree Upgrade.

Documentation updates

Deleting shipping fees

If you delete a shipping fee and look at the order details again, the shipping fee will re-appear. You can use this behaviour if you want to re-calculate the shipping fee. If you don't want to charge a shipping fee you can set the amount of the shipping fee to 0.

Spree 2.0 keeps shipping fee adjustments open unless they are manually
closed. But open adjustments cannot be edited.
To preserve updates, like changing the amount of the shipping fee
we close the adjustment first.
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

omg, that looks easy! Amazing.

@@ -38,6 +38,17 @@ def set_included_tax
params[:adjustment][:included_tax] = 0
end
end

# Spree 2.0 keeps shipping fee adjustments open unless they are manually
# closed. But open adjustments cannot be edited.
Copy link
Contributor

Choose a reason for hiding this comment

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

How did you get to this conclusion? our #update_adjustment! still opens the adjustment to update it so it feels contradictory.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I simply observed that adjustment.update_attributes didn't have any effect on an open shipping fee adjustment. But after I closed it, it did update the attributes. I guess that you need to open adjustments to reset them to their calculated value and you have to close them to set a custom value. Do you think we should dig deeper in the code to verify that theory?

Copy link
Member Author

Choose a reason for hiding this comment

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

I checked vanilla Spree and an adjustment can be updated either open or closed. But it triggers the after_save callback update_adjustable which calls update! on the order and that eventually triggers a reset of the adjustment. I guessed that before but here is the proper trace:

  1. adjustment.update_attributes triggers adjustable.update!.
  2. Order#update! triggers the Spree::OrderUpdater.
  3. The updater updates all shipments and adjustments.
  4. Adjustment#update! doesn't do anything if the adjustment is closed.
  5. When the adjustment is open, originator.update_adjustment is called.
  6. ShippingMethod#Adjustment recalculates the amount and luckily avoids callbacks: adjustment.update_attribute_without_callbacks(:amount, compute_amount(calculable)). Otherwise we would have an infinite loop.

All this seems Spree code. It's not something we accidentally caused with OFN overrides. So Spree defines this way:

  • Open shipping adjustments are always recalculated -> can't be changed to a custom value.
  • Closed shipping adjustments are not recalculated -> can be changed to a custom value.

This research presents another option though. We could try to call adjustment.update_attribute_without_callbacks to update the amount. I guess though that it wouldn't last long. The adjustment would probably be recalculated when anything in the callback jungle is triggered, e.g. order.next. I conclude that closing the adjustment is the "Spree thing" to do if we want to set a custom amount.

Do you think that's reasonable?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, yes, I followed this track in the past. Makes sense.
When you edit an adjustment, you need to close it, otherwise it gets recalculated. That's the spree logic.
finalized means you can't, you shouldnt change the value in any way.

Copy link
Contributor

@sauloperez sauloperez Apr 5, 2019

Choose a reason for hiding this comment

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

Could you take the time to paste that amazing investigation in the Spree upgrade wiki section? It will save us time in the future. I specially like your final open/closed summary 👏

Copy link
Member Author

Choose a reason for hiding this comment

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

@RachL RachL self-assigned this Apr 10, 2019
@RachL RachL added the pr-staged-fr staging.coopcircuits.fr label Apr 10, 2019
@RachL
Copy link
Contributor

RachL commented Apr 10, 2019

This is good to go https://docs.google.com/document/d/1Ks1lcvGxUaAiDHINnNcGnBaBfsQr-AfoUhbQkVrKKDA/edit#

I've noticed something weird on the order total, but I will test this further and see if a new issue needs to be open.

@RachL RachL removed the pr-staged-fr staging.coopcircuits.fr label Apr 10, 2019
@luisramos0
Copy link
Contributor

luisramos0 commented Apr 10, 2019

nice nice nice!!! 👏

@luisramos0 luisramos0 merged commit 4bf9320 into openfoodfoundation:2-0-stable Apr 10, 2019
@mkllnk mkllnk deleted the 1830-v2-edit-shipping-fees branch June 25, 2019 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants