Copied to clipboard

Flag this post as spam?

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


  • Francis Benyah 35 posts 111 karma points
    May 21, 2020 @ 16:59
    Francis Benyah
    0

    Setting Vendr Product Price Programatically

    Hi Matt

    Just a quick one here.

    How would I go about setting the price property programatcally

    enter image description here

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    May 21, 2020 @ 18:23
    Matt Brailsford
    102

    Hey Francis,

    You should be able to set the price property via Umbraco's content API. You'll need to feed it the expected data formatted for the Price property editor, which needs to be like:

    {
        "27f0ce3e-da98-4492-b442-8010cd31053c": 10.00,
        "14b17a8d-b8cc-4617-ab9d-cc6fb60b0535": 12.00
    }
    

    The GUIDs being the ID's of the Currencies and the values being the price for those currencies.

    To get a list of Currencies you can get these from the from the Vendr Currency Service

    _currencyService.GetCurrencies(storeId);
    

    Hope this helps

    /Matt

  • Francis Benyah 35 posts 111 karma points
    May 22, 2020 @ 06:05
    Francis Benyah
    0

    Hi Matt

    Thanks for the guidance much appreciated.

    I took a stab at this with not much luck. I suspect it is in the way I am structuring the value object.

    I tried the following approaches

    // set price
    productNode.SetValue(DocumentTypeProperties.BaseVendrProductPrice, new { RootNodes.eVendrCurrenciesUSDNodeKey, price});
    

    and also this passing as a dictionary then tried to pass it as KeyValuePair by getting FirstOrDefault

    // set price
    Dictionary<string, int> price = new Dictionary<string, int>
    {
       { RootNodes.eVendrCurrenciesUSDNodeKey, price}
    };
    productNode.SetValue(DocumentTypeProperties.BaseVendrProductPrice, price.FirstOrDefault());
    

    RootNodes.eVendrCurrenciesUSDNodeKey holds the GUID of the currency created in the settings section DocumentTypeProperties.BaseVendrProductPrice holds the alias of the price property on the product document type

    I have tried with the price as a decimal as well in these scenarios.

    Much appreciated.

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    May 22, 2020 @ 06:10
    Matt Brailsford
    1

    Hey Francis,

    Have you tried serializing it to a JSON string?

    /Matt

  • Francis Benyah 35 posts 111 karma points
    May 22, 2020 @ 07:02
    Francis Benyah
    0

    Thanks Matt

    That did it. Totally forgot about that.

  • Michele Benolli 31 posts 149 karma points
    Jul 09, 2021 @ 13:13
    Michele Benolli
    0

    I tried to follow this to save a "zero" price for products without a price set. I intercept the content publishing event in order to edit the Price field. The field is filled with the same string that I obtain by manually editing the price via the content editor, however, the Price is not recognized and I got a null reference error when I try to access the property of a product. What I'm missing?

    private void ContentService_Publishing(IContentService sender, ContentPublishingEventArgs e)
    {
        foreach (var node in e.PublishedEntities)
        {
            var price = node.GetValue("price");
            if (price == null)
            {
                // Get the home page of the current content
                var homeId = sender.GetAncestors(node.Id).FirstOrDefault(x => x.ContentType.Alias == Constants.HomePageAlias).Id;
                var home = Umbraco.Web.Composing.Current.UmbracoHelper.Content(homeId) as HomePage;                   
    
                // Get the store currencies
                var currencies = Vendr.Core.Api.VendrApi.Instance.GetCurrencies(home.Store.Id);
    
                // Set price "0" for all the currencies
                var prices = new Dictionary<Guid, decimal>();
                foreach (var currency in currencies)
                    prices.Add(currency.Id, 0m);
    
                node.SetValue("price", JsonConvert.SerializeObject(prices));
            }
        }
    }
    
  • Amir Khan 1282 posts 2739 karma points
    Jun 07, 2022 @ 17:26
    Amir Khan
    0

    Trying something similar with the content service, but with no luck.

    This isn't so much Vendr issue as I'm struggling to output the JSON correctly, what I have below outputs:

    {"currencyId":"771cc1cb-a743-4c5d-8c7b-01812fd545c0","price":0}

    var currencyId = "771cc1cb-a743-4c5d-8c7b-01812fd545c0";
    
    int price = 0;
    
    var json = JsonConvert.SerializeObject(new {currencyId, price});
    
  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Jun 08, 2022 @ 08:05
    Matt Brailsford
    0

    Hi Amir,

    Maybe try something more like

    var currencyId = "771cc1cb-a743-4c5d-8c7b-01812fd545c0";
    var price = 0m;
    var json = JsonConvert.SerializeObject(new Dictionary<string, decimal> {
        { currencyId, price }
    });
    

    Matt

  • Amir Khan 1282 posts 2739 karma points
    Jun 09, 2022 @ 17:55
    Amir Khan
    1

    That worked perfectly, thank you!

Please Sign in or register to post replies

Write your reply to:

Draft