Tea Commerce - Create Dynamic Product with Dynamic Price
Hi
So I'm building a picture frame e-commerce site and one of the bits of functionality of this site is being able to build your own picture frame.
The customer can set a width and height of the picture frame.
The idea is to set a price per mm of the frame, and then calculate the final price with regards tot he size.
Is there a way to add this as a dynamic product to the basket?
As all I can see is that a product has to have a set product price with a unique id to add to the basket?
The only other way I can think of doing it is multiplying the quantity by 2000 in this instance to the get £32.00 price to add to the basket?
Id then have to change the code in the basket to check what product type has been added to hide the mammoth quantity added?
Just wondered if I was missing an easier way of doing this?
I think the main option I would look at would be a custom OrderLineCalculator (https://docs.teacommerce.net/3.3.0/api/extending-tea-commerce/#order-line-calculator) that could be used to look for a custom property on the OrderLine to identify it as a dynamic product, and then also uses order line properties to capture the width / height of the frame. The product node could then hold the base price and then the calculator could use these to work out what the actual price of the order line is.
If it's not a dynamic product, then it could fall back to the base functionality for standard products.
Yep had a look through the Docs and came to the same conclusion. Seems simple enough, I want to check the document type of the product and then i can do the calculation dependant on the width and height.
However running into a problem trying to access IPublishedContent to check the DocumentType of the Product, Im passing in the orderline.ProductIdentifier but my code crashes.
Here's what I have so far
[SuppressDependency("TeaCommerce.Api.PriceCalculators.IOrderLineCalculator", "TeaCommerce.Api")]
public class CustomOrderLineCalculator : OrderLineCalculator
{
public CustomOrderLineCalculator(IVatGroupService vatGroupService, IProductInformationExtractor productInformationExtractor) : base(vatGroupService, productInformationExtractor)
{
}
public override Price CalculatePrice(OrderLine orderLine, Currency currency, Order order)
{
if (!string.IsNullOrEmpty(orderLine.ProductIdentifier))
{
var frameType = new Umbraco.Web.UmbracoHelper();
var frame = frameType.TypedContent(orderLine.ProductIdentifier.ToString());
}
return new Price(100M, orderLine.VatRate, currency);
}
}
Sorry, think it's my setup, keep loosing the Autofac.dll from the build for some reason.
Then when adding the product to the basket I get the blank screen with the formPost.aspx. I've usually had that when there have been more than 20 items in the orders, clearing it out fixes it but it showed up again and it was down to trying to access IPublishedContent
Managed to sort it now, and yep will use the UmbracoContext to get the node details.
I've got a little further today on this, just wondering from a point of returning a new price I need to do anything else?
I'm hitting an error - System.ArgumentException: The product doesn't have a store id associated with it - remember to add the Tea Commerce store picker to your Umbraco content tree
My code so far is: (needs refactoring yet but you get the idea)
public override Price CalculatePrice(OrderLine orderLine, Currency currency, Order order)
{
decimal finalPrice = 0;
if (!string.IsNullOrEmpty(orderLine.ProductIdentifier))
{
var frame = UmbracoContext.Current.ContentCache.GetById(Convert.ToInt32(orderLine.ProductIdentifier));
if (frame != null && frame.DocumentTypeAlias == "attributeFrame")
{
var frameWidth = Convert.ToInt32(orderLine.Properties["productWidth"]);
var frameHeight = Convert.ToInt32(orderLine.Properties["productHeight"]);
var framePrice = Convert.ToDecimal(frame.GetPropertyValue("priceGBP"));
var frameWidthDouble = frameWidth * 2;
var frameHeightDouble = frameHeight * 2;
var heightPlusWidth = frameWidthDouble + frameHeightDouble;
finalPrice = Convert.ToDecimal(heightPlusWidth * framePrice);
}
else
{
//normal product
var collectionPrice = orderLine.OriginalUnitPrices.FirstOrDefault();
finalPrice = collectionPrice.Value;
}
}
return new Price(finalPrice, orderLine.VatRate, currency);
}
I'm just getting the blank page again and the URL is - /base/TC/FormPost.aspx
Tea Commerce - Create Dynamic Product with Dynamic Price
Hi
So I'm building a picture frame e-commerce site and one of the bits of functionality of this site is being able to build your own picture frame.
The customer can set a width and height of the picture frame. The idea is to set a price per mm of the frame, and then calculate the final price with regards tot he size.
So example:
Is there a way to add this as a dynamic product to the basket? As all I can see is that a product has to have a set product price with a unique id to add to the basket?
The only other way I can think of doing it is multiplying the quantity by 2000 in this instance to the get £32.00 price to add to the basket?
Id then have to change the code in the basket to check what product type has been added to hide the mammoth quantity added?
Just wondered if I was missing an easier way of doing this?
Hey Neil,
I think the main option I would look at would be a custom
OrderLineCalculator
(https://docs.teacommerce.net/3.3.0/api/extending-tea-commerce/#order-line-calculator) that could be used to look for a custom property on the OrderLine to identify it as a dynamic product, and then also uses order line properties to capture the width / height of the frame. The product node could then hold the base price and then the calculator could use these to work out what the actual price of the order line is.If it's not a dynamic product, then it could fall back to the base functionality for standard products.
Hope that helps
Matt
Hi Matt
Yep had a look through the Docs and came to the same conclusion. Seems simple enough, I want to check the document type of the product and then i can do the calculation dependant on the width and height.
However running into a problem trying to access IPublishedContent to check the DocumentType of the Product, Im passing in the orderline.ProductIdentifier but my code crashes.
Here's what I have so far
When you say your code crashes, what exactly do you mean? Do you get an error? If so, what's the stack trace?
Also, you shouldn't neet to go via the Umbraco helper to get the frame node, you could go via the UmbracoContext content cache instead
Sorry, think it's my setup, keep loosing the Autofac.dll from the build for some reason.
Then when adding the product to the basket I get the blank screen with the formPost.aspx. I've usually had that when there have been more than 20 items in the orders, clearing it out fixes it but it showed up again and it was down to trying to access IPublishedContent
Managed to sort it now, and yep will use the UmbracoContext to get the node details.
Thanks for your help Matt, much appreciated!
Aaaah, awesome.
Glad you got it all working 👍
Hi
I've got a little further today on this, just wondering from a point of returning a new price I need to do anything else?
I'm hitting an error -
System.ArgumentException: The product doesn't have a store id associated with it - remember to add the Tea Commerce store picker to your Umbraco content tree
My code so far is: (needs refactoring yet but you get the idea)
I'm just getting the blank page again and the URL is - /base/TC/FormPost.aspx
Hi Niel,
Does your code actually get hit? Or is it erroring before it even gets there?
Got this working well now, thank's for your help on it Matt. I can now calc the price dynamically
That's awesome. Glad you got it working 👍
is working on a reply...