-
-
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
When user deletes a shipping method existing orders are updated with a random shipping method #6029
Comments
Hi When a shipment method is removed from a distributor, the orders are not updated automatically. The order is updated when the admin access (using the edit link) the order. module Spree
module Admin
class OrdersControlle
...
def edit
@order.shipments.map(&:refresh_rates)
...
end The shipment's refresh_rates uses OrderManagement::Stock::Estimator#shipping_rates to recalculate # class Shipment
def refresh_rates
...
original_shipping_method_id = shipping_method.try(:id)
self.shipping_rates = OrderManagement::Stock::Estimator.new(order).shipping_rates(to_package)
keep_original_shipping_method_selection(original_shipping_method_id)
...
shipping_rates
end
class Shipment
...
def to_package
package = OrderManagement::Stock::Package.new(stock_location, order)
...
package
end
class OrderManagement::Stock::Package
...
def shipping_methods
...
order.distributor.shipping_methods.uniq.to_a # This is maybe the source of the problem
end And finally class OrderManagement::Stock::Estimator
def shipping_rates(package, frontend_only = true)
shipping_rates = []
shipping_methods = shipping_methods(package)
...
end
...
def shipping_methods(package) # This is maybe the source of the problem
shipping_methods = package.shipping_methods
...
shipping_methods
end Questions:
Any ideas? |
I've no idea ; and I agree with you: there is an inconsistency between an order accessed by an admin, and an order which is not.
Currently, i don't know (sorry, i'm useless) I'll deep dive into |
Hi @jibees I checked the problem again. I think you're right, the problem is on The following diagram illustrates the relationship between the models In
def refresh_rates
return shipping_rates if shipped?
# The call to Stock::Estimator below will replace the current shipping_method
original_shipping_method_id = shipping_method.try(:id)
self.shipping_rates = OrderManagement::Stock::Estimator.new(order).shipping_rates(to_package)
keep_original_shipping_method_selection(original_shipping_method_id)
shipping_rates
end The Now, the objective of To do so, it tries to find among the new def keep_original_shipping_method_selection(original_shipping_method_id)
return if shipping_method&.id == original_shipping_method_id
rate_for_original_shipping_method = find_shipping_rate_for(original_shipping_method_id) #<<<<<
if rate_for_original_shipping_method.present?
self.selected_shipping_rate_id = rate_for_original_shipping_method.id
else
# If there's no original ship method to keep, or if it cannot be found on the ship rates
# But there's a new ship method selected (first if clause in this method)
# We need to save the shipment so that callbacks are triggered
save!
end
end The def find_shipping_rate_for(shipping_method_id)
return unless shipping_method_id
shipping_rates.detect { |rate|
rate.shipping_method_id == shipping_method_id
}
end The default behavior will return the first shipping rate as the selected one def shipping_method
method = selected_shipping_rate.try(:shipping_method)
method ||= shipping_rates.first.try(:shipping_method) unless order.manual_shipping_selection #<<<
method
end As you can see, this scenario is already expected in the comment in
We can solve the issue by not erasing the What do you think? |
You really did a great research job, thanks for that 🙏 I don't spot any error or something suspicious, but I'm not totally sure ;) What I'd do:
+ code review with @dacook I guess it's ok. What do you think? |
@jibees I believe that the error is on overwriting the Suppose we start with a distributor that supports the shipping methods m1 and m2
order:
shipment:
shipment rates:
- shipping rate 1 (connected to m1) [Selected]
- shipping rate 2 (connected to m2)
distributor:
shipment methods:
- m1
- m 2 Suppose now that the distributor removed m1 from his list, the order:
shipments:
shipping rates: <- need to refresh
- shipping rate 1 [Selected]
- shipping rate 2
distributor:
shipment methods:
- m 2 when we access the order as admin, order:
shipments:
shipping rates: <- we're refreshing this
- shipping rate 2 (connected to m2) (Not selected, but will be used by default since it is the first shipping rate in the list)
distributor:
shipment methods:
- m 2 In the final step, we lose the Now, if you try to access What do you think? |
I was thinking about the solution of preventing the removal of a shipping method if it's used in an order cycle. Honestly, I was confused about the meaning of 'removing'. Case 1: if Denying this operation will not solve the problem, it'll just postpone it to the end of the order cycle (I explained in my previous comments). I already have a solution for this case (and I'll implement it). Case 2: if This is out of the scope. By default, when the user decides to erase a shipping method, it will be softly deleted from the database ( openfoodnetwork/app/models/spree/shipping_method.rb Lines 3 to 12 in 5ed6e55
|
@abdellani thanks for the update. There are two options I think:
if this takes too much work (our aim here is to fix a bug, not to implement a feature) we need to:
What do you think? |
Thank you @RachL
I agree and I believe that the source of the problem is the method openfoodnetwork/app/models/spree/shipment.rb Lines 114 to 124 in 5ed6e55
I tried to prevent that in my PR, but It seems that my solution is causing another problem with the total amount calculation. More precisely, in the following test, |
Description
When an enterprise removes shipping method from their Shipping Method list existing orders with this shipping method are changed. It seems the first shipping method available to the enterprise is selected.
This results in:
Example:
Orders: R852656180 (paid by PayPal) and R282147658 (paid by Stripe) placed with enterprise Sail Cargo SE (ID 201099).
Customer opted for 'UK delivery outside Brighton and Hove B1, B2, B3 postcodes' and paid 10% fee associated with this method at check out.
Hub removed this shipping method
Order and reports display 'Collection from Fiveways office...' which has £0.00 fee. Orders display as 'credit owing'.
Expected Behavior
Customer places order with a shipping method
order confirmation email shows this is what they have paid for
which is reflected on the Orders page
Hub removes the shipping method from their shipping method list.
List at the time order was placed
List some time after order placed when hub has removed this shipping option
Order (via 'Orders' -> Edit order and via Reports) should the shipping method as originally selected by the shopper.
Actual Behaviour
Steps 1 and 2 as Expected Behaviour
Further, thange is not reflected under order adjustments
Steps to Reproduce
Workaround
Workaround is to recreate the shipping method.
Severity
s3 - Damaging if not noticed.
Your Environment
Possible Fix
The text was updated successfully, but these errors were encountered: