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:
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.
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));
}
}
}
Setting Vendr Product Price Programatically
Hi Matt
Just a quick one here.
How would I go about setting the price property programatcally
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:
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
Hope this helps
/Matt
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
and also this passing as a dictionary then tried to pass it as KeyValuePair by getting 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.
Hey Francis,
Have you tried serializing it to a JSON string?
/Matt
Thanks Matt
That did it. Totally forgot about that.
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?
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}
Hi Amir,
Maybe try something more like
Matt
That worked perfectly, thank you!
is working on a reply...