Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Shipments cost #34

Open
matthieupinte opened this issue Jan 7, 2015 · 8 comments
Open

Shipments cost #34

matthieupinte opened this issue Jan 7, 2015 · 8 comments

Comments

@matthieupinte
Copy link

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 into Spree::OrderUpdated::update_shipment_total method:

order.shipment_total = shipments.sum(:cost)

The problem is that the cost is calculated into the current order currency...
so if I want to change the shipment_total I have to add define the cost 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 is shipping address?
What's shipping_rate?
How all this stuff work together?

Really, this time, I'm lost. Thanks for your help.

@arpanlepcha
Copy link
Contributor

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.

@matthieupinte
Copy link
Author

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

@arpanlepcha
Copy link
Contributor

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

@matthieupinte
Copy link
Author

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.

@aishmita-aggarwal
Copy link
Contributor

For shipment methods with calculators such as flat rate that take amount, we should have prices for each currency.

@aishmita-aggarwal
Copy link
Contributor

@priyank-gupta @damianlegawiec @Mafi88 What are your thoughts? Thank you.

@omairrazam
Copy link

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

@omairrazam
Copy link

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`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants