Open matthieupinte opened 9 years ago
Hi Matthieu,
I was facing similar problems, namely while changing currency during delivery. Before transitioning to delivery the cost per shipment is already calculated, so when we change currency, shipping cost has to be recalculated.
I added a small snippet in set method of currency controller namely,
update_totals if current_order.state.eql? 'delivery'
and I defined a private method
def update_totals
current_order.create_proposed_shipments
current_order.set_shipments_cost
end
this solved my shipping cost problem, I guess this method only works for flat rate. For shipping calculator, i defined a new type of calculator called CustomFlatRate, where in i did the conversion based on what exchange rates did the admin set. eg: 1 EUR = 1.5 USD then during shipping cost calculation i basically did n(USD) * exchange_rate .
hope this helps and you come up with better approach.
Hi, Thanks for your answer, in fact... your snippet doesn't change anything...
After few researches on why and how... I see that: It seems that some calculators use Spree::Config[:currency] by default...
class Calculator::FlatRate < Calculator
preference :amount, :decimal, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }
I thought that when the calculator is used, it will use the "order/shipment currency"... but seems not.
Cause if I change the Spree::Config[:currency]
into the CurrencyController::set
method, it's working well.
Now I need to know how to change it to uses the session[:currency]
, into the calculator? how?...
Thanks again, investigations continue...
Glad you are able to figure it yourself. :)
If you are planning to create a new Calculator, i suggest you edit
compute_package
for the calculator
in this definition, you will be able to get the currency of the order through
package.order.currency
Assuming your default currency is USD, now you need to basically get the shipping cost in USD and apply relevant exchange rates, which you can define in your general settings through preferences or install a gem that would give you the current exchange rates..
Hi, in fact I'm not planning to create new calculators, I will just "decorate" existing ones and set the default currency to session[:currency]
(I thing) later.
For now I don't have time to create a PR about it, but I keep an eye on this issue and for any solution from someone.
Thanks for your help. Regards.
For shipment methods with calculators such as flat rate that take amount, we should have prices for each currency.
@priyank-gupta @damianlegawiec @Mafi88 What are your thoughts? Thank you.
@aishmita-aggarwal i agree with you. I am facing same problem. We can't define right now different value for each currency on calculator. We need it same as we have with products etc. so this feature is missing in this extension.
we can override like this perhaps.
`module Spree module Calculator::Shipping class FlatRate < ShippingCalculator
preference :currency_USD_amount, :decimal, default: 0
preference :currency_AED_amount, :decimal, default: 0
def self.description
Spree.t(:shipping_flat_rate_per_order)
end
def compute_package(_package)
self.send("preferred_currency_#{Spree::Config[:currency]}_amount")
end
end
end end`
Hi, Thanks for your work, it's very helpful.
I have some issues with the Shipments cost... because it's "calculated directly" into a variable
shipment_total
...shipment_total
is calculated intoSpree::OrderUpdated::update_shipment_total
method:The problem is that the
cost
is calculated into the current order currency... so if I want to change theshipment_total
I have to add define thecost
into "all supported currency"?In fact I don't know how to "solve" this problem... How
shipments
are created? When? just after the user enter isshipping address
? What'sshipping_rate
? How all this stuff work together?Really, this time, I'm lost. Thanks for your help.