Ive been trying to figure out a way to do a dynamic price update bases on a variant. For instance if i have a variant of a shirt in sizes s, m, l , xl.
so i have a variant with an attribute size and a attribute price
I've been trying with javascript but i cant seem to get it working.
The easy way would be to just output the price as value in your dropdown of shirt sizes and set the price after selected item. Another way is to fetch the specific values of your variant using service stack. You can specify your own callbacks and whatnot. It is pretty straight forward once you just take a look at how it's done in the razor store.
I have looked at the razor store but i cant see its updating the price on the product page when i select a diffrent size. Just to clear it up, its not when adding it to basket that i got covered. its just that when i switch the parameters in the dropdowns i want it to update the price displayed on the product instant.
You could output price information on initial page load and hide it using jQuery. Based on the selection in the drop down lists you could find the appropriate price already present in the DOM and display that.
Alternatively you can hook into the webservice, which checks for valid variant combos and have that return the appropriate price as part of the JSON it produces.
If you download the source for Avenue Clothing you'll find a method called GetProductVariationsResponse. It outputs information for each variant. It will be pretty easy to add price info to that output.
I have had a similar issue and have tried your suggestion above, but trying to get the variant price using the "varient.GetPrice" function seems to crash my development server (before I can even see an error).
Do you have a suggested method for doing this? Below is the code I've tried on GetVarientSkuFromSelection.cs in the source project:
Variant = new ProductVariation { Sku = variant.Sku, VariantSku = variant.VariantSku, ProductName = variant.Name, Price = variant.GetPrice(priceGroup), Properties = variant.ProductProperties.Select(prop => new ProductProperty { Id = prop.Id, Name = prop.ProductDefinitionField.Name, Value = prop.Value }) };
Here is the line I've added to the ProductVariation.cs model: public Money Price { get; set; }
I have verified that my "priceGroup" variable is being loaded and has the correct information in it. However, when I go to run GetPrice, it crashes out.
Thank you for the update. Where can I download this update? I can't see any commits regarding this in the Razor Store repo, or a new package release for uCommerce?
Dynamic Price update
Ive been trying to figure out a way to do a dynamic price update bases on a variant. For instance if i have a variant of a shirt in sizes s, m, l , xl.
so i have a variant with an attribute size and a attribute price
I've been trying with javascript but i cant seem to get it working.
Anyone out there that has solved this before?
Hi Klaus.
Do you use the razor store?
The easy way would be to just output the price as value in your dropdown of shirt sizes and set the price after selected item. Another way is to fetch the specific values of your variant using service stack. You can specify your own callbacks and whatnot. It is pretty straight forward once you just take a look at how it's done in the razor store.
Hey Morten
Thanks for your reply
I have looked at the razor store but i cant see its updating the price on the product page when i select a diffrent size. Just to clear it up, its not when adding it to basket that i got covered. its just that when i switch the parameters in the dropdowns i want it to update the price displayed on the product instant.
Hi Klaus,
You could output price information on initial page load and hide it using jQuery. Based on the selection in the drop down lists you could find the appropriate price already present in the DOM and display that.
Alternatively you can hook into the webservice, which checks for valid variant combos and have that return the appropriate price as part of the JSON it produces.
If you download the source for Avenue Clothing you'll find a method called GetProductVariationsResponse. It outputs information for each variant. It will be pretty easy to add price info to that output.
Hope this helps.
Hey Søren,
I have had a similar issue and have tried your suggestion above, but trying to get the variant price using the "varient.GetPrice" function seems to crash my development server (before I can even see an error).
Do you have a suggested method for doing this? Below is the code I've tried on GetVarientSkuFromSelection.cs in the source project:
PriceGroup priceGroup = ProductCatalog.All().Single(x => x.Name == SiteContext.Current.CatalogContext.CurrentCatalog.Name).PriceGroup;
Variant = new ProductVariation
{
Sku = variant.Sku,
VariantSku = variant.VariantSku,
ProductName = variant.Name,
Price = variant.GetPrice(priceGroup),
Properties = variant.ProductProperties.Select(prop => new ProductProperty
{
Id = prop.Id,
Name = prop.ProductDefinitionField.Name,
Value = prop.Value
})
};
Here is the line I've added to the ProductVariation.cs model:
public Money Price { get; set; }
I have verified that my "priceGroup" variable is being loaded and has the correct information in it. However, when I go to run GetPrice, it crashes out.
Any ideas?
Cheers,
Danny
Hi Danny,
Do you see anything in the UmbracoLog ?
Also, you could just use this single line of code to get the current catalog:
SiteContext.Current.CatalogContext.CurrentCatalog
What you do is load every catalog from the database and the using single :)
Hi Morten,
Thank you for your help. I have checked the UmbracoLog and the server seems to crash before anything can get written.
I will have a play with that single line of code and see how I go.
Thanks again,
Danny
Hi Danny,
Your variant probably doesn't have a price set up in the price group you're asking for. It will cause a NullReferenceException at the moment.
I have updated the code base to return a null value instead to make it easier to work around.
Sorry about the inconvenience.
Hi Søren,
Thank you for the update. Where can I download this update? I can't see any commits regarding this in the Razor Store repo, or a new package release for uCommerce?
Cheers,
Danny
is working on a reply...