Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
It would be nice if we could change the default currency depending on the site, so if we have a US store it could be USD and if we have a Canadian store it would be CAD.
Right now it fallsback too quickly to the site default currency, if all the order line items are in one currency (or first one IIRC) then it will use that currency for the order.
Proposed resolution
Add an alter hook to commerce_default_currency
so that we can hijack that value through a rule or alter hook.
Remaining tasks
User interface changes
N/A
API changes
New alter hook.
Comment | File | Size | Author |
---|---|---|---|
#11 | change_site_s_default-2415237-11.patch | 1.94 KB | japerry |
| |||
#10 | interdiff.txt | 654 bytes | joelpittet |
#10 | change_site_s_default-2415237-10.patch | 1.15 KB | joelpittet |
| |||
#3 | 2415237-3-default-currency.patch | 526 bytes | joelpittet |
#1 | 2415237-1.patch | 687 bytes | joelpittet |
Comments
Comment #1
joelpittetI think this is all that would be needed for this?
Then someone could do this:
Comment #3
joelpittetWhoops missing --relative there.
Comment #4
joelpittetComment #5
joelpittetComment #6
mglamanRandom Q: would be want to static cache this so we didn't need to run through all of the alters each time this is invoked?
Comment #7
joelpittet@mglaman That may be a good addition. If you feel it's needed we can add that here. There is at least a few function calls in drupal_alter() that could add to the total number of calls.
Comment #8
mglamanNever mind my comment. Does exactly as it should. I think any implications (CAD might have a higher price than USD on some stores) would be up to those who implement this hook.
Comment #9
mglamanForgot I ended up using this patch as a quick way to switch currencies based on some global variables (such as language.)
Comment #10
joelpittetAdding api docs.
Comment #11
japerryheh interesting that we're working on the same issue at the same time! :-D
My patch is slightly different, in that it allows functions calling the default to provide a context, since the currency might get defined by a group. It also sets up a static var so it doesn't get called over and over again during a call.
Comment #12
joelpittetIt's commerce-sprint time:) interdiff?
Comment #13
joelpittet@japerry What kinds of context would you expect to be passed, use-case?
Comment #14
japerryMainly we're using the commerce_discount module with conferences to establish different currencies per discount. We pass the discount (Which contains a conference reference) into the default currency check, which is passed into an alter, which grabs the conference node id and a currency sitting in the conference.
Comment #15
joelpittet@japerry how do you ensure that context get's passed in on other calls within the system?
Comment #16
joelpittet@japerry I just noticed the static cache. That could actual counter what you are trying to do with the context. Because the context could change the results of the function... maybe you could put this idea into a follow-up so we can get at least this in to commerce?
Comment #17
japerryFor our specific case, we need a static cache because the context should only get called once. When doing a discount calculation, when this is first called, it has a context -- but later on in the same call it does not have it. In general, we don't want the context changing during the page request. There should only be one context being passed in.
og_context went through a similar problem, and was resolved with a static cache, figured a similar strategy could be used here.
Comment #18
japerryMade #2657718: Add a static cache and context to determine default currency in lieu of continuing to stall this issue. #10 is good by me.
Comment #19
rszrama CreditAttribution: rszrama at Centarro commentedThanks guys, committed! (Changed the variable name from $default to $currency_code.)