Developer forum

Forum » Development » Product Variant prices and discounts

Product Variant prices and discounts

Andrew Rushworth
Reply

When I use the template view module in rapido and get back the product I use the Ecom:Product.Discount.Price and Ecom:Product.Price for the Price and the Discounted price.

I created an APi and use ProductService().GetProductById() method to get the product.
I then loop thorugh the combination of variants using product.VariantCombinations.
On each of the variants I can once again use variantCombination.GetProduct() to get the product for that variant ( variantProduct).
However, if I use variantProduct.Price or variantProduct.Discount.Price I'm not getting the same results as the template view model.

I've tried GetDiscountMatrix too.

There is something I'm getting wrong here and not winning.
How can I use the ProductService to get the correct pricing information for each variant combination?

 


Replies

 
Andrew Rushworth
Reply

I've tried just about all the various properties and methods on the Product object and can't seem to find a way to get teh Discount calculations for a product that will return what the Ecom:Product.Discount.Price and Ecom:Product.Price return on the ViewModel

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Andrew

There are no discounts on the product objects - so using product and productservice will not help you.

What version are you on? You mention Rapido which could indicate an older version.

You can use this tag @GetTemplateTags("Price") and @GetTemplateTags("Discount") to search for tags containing those 2 strings. If you are in a loop of products you can do @productLoopItem.GetTemplateTags("Price")

That would give you a list of all available tags including the one the price you are looking for.

Some later versions of DW9 contains more tags.

 
Andrew Rushworth
Reply

Hi Nicolai,

Apologies, I'm using 9.17.7.
I'm implementing a custom API, so not really using rapido.
Is there a way give an product number to get the discounted value? On the product srevice there is a Product.Discount.Price and a Product.GetDiscountMatrix().

I just need to find out what the product Price after being run through the Discount calcualtions.

Here is what I am doing to loop through the product's variants and each variants prices:
 

                var productService = new ProductService();
                PriceContext priceContext = new PriceContext(Ecommerce.Common.Context.Currency, Ecommerce.Common.Context.Country);

                var product = productService.GetProductById(productId, string.Empty, request.LanguageId);

                if (product == null) return NotFound();

                foreach (var vc in product.VariantCombinations)
                {
                    var vcProduct = vc.GetProduct(Ecommerce.Common.Context.LanguageID);
                    var discounts = vcProduct.GetDiscountMatrix(Ecommerce.Common.Context.Currency.Code);

                }

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Andrew

It is not quite that simple. A product has a price and then when you buy it you can get one or more discounts.

I think the easiest way for you to get the products, you can create a list of product viewmodels using the ViewModelFactory - that will take care of calculating the discounts and give you something useful:

IEnumerable<ProductViewModel> products = Dynamicweb.Ecommerce.ProductCatalog.ViewModelFactory.CreateView(ProductViewModelSettings settings, IEnumerable<ProductInfoViewModel> productInfo)

So in your code before creating this list, find the variants you in the end result, and pass them to the above code.

A couple of comments of your code:

>>var productService = new ProductService();
You should not create new instances of services as that can cause some bad behavior - instead use Dynamicweb.Ecommerce.Services.Products which will give you an instance

>>PriceContext priceContext = new PriceContext(Ecommerce.Common.Context.Currency, Ecommerce.Common.Context.Country);
You state custom API - if you are creating a REST api this is not possible as the Common.Context is based on context and REST is state-less. Also if you are not running in the context of a pageview, using Common.Context is also an issue. 

 

 
Andrew Rushworth
Reply

Thanks for the feedback Nicolai.

I was using Dynamicweb.Ecommerce.ProductCatalog.ViewModelFactory.CreateView to create the view model, but the prices that come back are the same for all variants.
I have to get the price collection on the main product which will then give me a list of the variants and the actual prices (before discount).
Regarding getting the discounted price, I'm still at a loss in getting these back.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Andrew

I have asked the partner center to reach out to you.

BR Nicolai

 
Andrew Rushworth
Reply

Thanks, appreciated.

 

You must be logged in to post in the forum