Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Karl Alesbury 8 posts 73 karma points
    Feb 28, 2024 @ 17:27
    Karl Alesbury
    0

    Umbraco Commerce - Multiple Cultures, multiple stores?

    Hi,

    I couldn't specify an Umbraco version above V9, so apologies for it saying V9!

    I'm building a U13 instance with multiple websites under it, each with its own culture and domain name. These stores will all sell the same product, so the product listing sits OUTSIDE the content trees.

    I want to be able to specify the price in USD and GBP for a given product, and for Umbraco Commerce to display that price in the respective currency and allow the user to check out.

    I was hoping that two website that have separate cultures could share the same Umbraco Commerce if they both had the same Store picked in their homepage (as per the demo site), but if I try this, users are seemingly checked out as UK customers, which is the default country, with GBP as the default base currency.

    If I try and create two Umbraco Commerce stores, one that has a base country of United States & currency of USD, the other UK with GBP, the UmbracoCommerce:Price picker only allows me to pick prices for the FIRST store, without being able to configure the store that the price currencies come from.

    I appreciate this is complicated to read, so what I'm asking in a nutshell is:

    If I have multiple websites in an Umbraco instance that share a product content node, can I use a single Umbraco Commerce store to check the user out with the relevant currency depending on the website's culture, or is there a way to have a 1-to-1 relationship between Commerce store and website?

    Thanks, and happy to elaborate as much as I can to make it clear. I found it tricky to explain :D

  • Joppe Ruessink 18 posts 168 karma points
    Feb 28, 2024 @ 21:51
    Joppe Ruessink
    100

    Hi Karl,

    I think I understand approximately what you are asking:

    • You are using a single Umbraco Commerce store in which two currencies are set.
    • Because UK (with default currency GBP) is set as default within this store, USD/USA orders are also created as UK orders with the currency GBP.
    • You have tried to solve this by creating a new store, separating currency (and thus making it possible to select different default currencies) but because the product is only linked to 1 store it is no longer possible to set different prices per currency.

    If the above is correct I would do the following: Since there is no difference between the product per currency/country (appart from the price) I would just keep using a single store in which two currencies are set (GBP, USD), which I think would still allow setting prices for both currencies.

    To solve the problem with the wrong currencies in orders, when adding the product to a shopping cart I would use the ISessionManager in combination with the IOrderService, ICurrencyService and the IUnitOfWorkProvider offered by Umbraco Commerce to set the currency of the order correctly. The point here is that when the product is added to the cart, additional custom logic is executed that sets the currency correctly.

    The code can then look like this (sorry if i made a mistake):

    //Creating an bundle of transactions (in case of required rollback)
    _unitOfWorkProvider.Execute(uow =>
    {
        //Retrieving or creating writable order
        Order order = _sessionManager.GetOrCreateCurrentOrder(_store.Id)
            .AsWritable(uow);
    
        //Checking if USD must be set
        if(usdIsRequired)
        {
            //Retrieving the USD currency
            var currency = _currencyService.GetCurrencies(storeId).FirstOrDefault(c => c.Code.ToLower() == "usd");
            order.SetCurrency(currency);
        }
    
        //Adding the product to the cart
        order.AddProduct(
            productReference: input.ProductReference,
            qty: input.Quantity,
            properties: null
        );
    
        //Saving the order
        _orderService.Save(order);
    
        uow.Complete();
    }
    

    You must give some input as suggested above. Please let me now if this can help!

    Greetings, Joppe

  • Karl Alesbury 8 posts 73 karma points
    Feb 29, 2024 @ 10:34
    Karl Alesbury
    0

    Hi Joppe,

    Thanks very much for getting in touch!

    Yes you've interpreted my message correctly (phew!) and setting the currency depending on the culture is exactly what I was after, in the sense that I can now apply the correct currency and calculate tax accordingly.

    The shipping country was still perpetually set to GB though, despite the culture of the website being en-US, and the currency being set to USD. That particular website will be serving US customers, and shipping to the US, so presumably it'd make sense to also alter the shipping country to US.

    I've used:

    var usd = _currenciesService.GetCurrencies(store.Id).FirstOrDefault(m => m.Code.ToLower() == "usd");
    var usCountry = _countryService.GetCountries(store.Id).FirstOrDefault(m => m.Code.ToLower() == "us");
    
    order.SetCurrency(usd);
    order.SetShippingCountryRegion(usCountry);
    order.SetPaymentCountryRegion(usCountry);
    

    Is that the recommended course?

    Thanks again for all your help, it's massively appreciated!

    Cheers,

    K

  • Joppe Ruessink 18 posts 168 karma points
    Feb 29, 2024 @ 12:33
    Joppe Ruessink
    0

    Hi Karl,

    Cheers indeed!

    A few important points. Always make sure you use the IUnitOfWorkProvider and that the UnitOfWork is ALWAYS disposed at the end. I didn't see it in your code example but I assume you already do this.

    Furthermore, I also saw that it was possible to retrieve the correct currency and country without LINQ:

    _currencyService.GetCurrency(storeId, "USD");
    _countryService.GetCountry(storeId, "US");
    

    The rest of the code looks good!

    Greetings,

    Joppe

  • Karl Alesbury 8 posts 73 karma points
    Feb 29, 2024 @ 15:22
    Karl Alesbury
    0

    Hi Joppe,

    Yes, my code is encapsulated within the unit of work, thanks for pointing it out.

    Thanks for the tip on not having to use LINQ as well!

    Your time is very much appreciated!

    All the best,

    K

Please Sign in or register to post replies

Write your reply to:

Draft