Copied to clipboard

Flag this post as spam?

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


  • Neil Hodges 193 posts 519 karma points
    Apr 14, 2019 @ 11:43
    Neil Hodges
    0

    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:

    > Frame Price = 0.016p 
    > Frame Size = 500 x 500 
    > Calc = 500 + 500 + 500 + 500 = £32.00
    

    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?

  • Matt Brailsford 2149 posts 10592 karma points MVP 6x c-trib
    Apr 14, 2019 @ 14:39
    Matt Brailsford
    0

    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

  • Neil Hodges 193 posts 519 karma points
    Apr 14, 2019 @ 15:52
    Neil Hodges
    0

    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

    [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);
                    }
    
                }
    
  • Matt Brailsford 2149 posts 10592 karma points MVP 6x c-trib
    Apr 15, 2019 @ 08:05
    Matt Brailsford
    0

    When you say your code crashes, what exactly do you mean? Do you get an error? If so, what's the stack trace?

  • Matt Brailsford 2149 posts 10592 karma points MVP 6x c-trib
    Apr 15, 2019 @ 08:08
    Matt Brailsford
    0

    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

    UmbracoContext.Current.ContentCache.GetById(orderLine.ProductIdentifier.ToString());
    
  • Neil Hodges 193 posts 519 karma points
    Apr 15, 2019 @ 08:17
    Neil Hodges
    0

    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!

  • Matt Brailsford 2149 posts 10592 karma points MVP 6x c-trib
    Apr 15, 2019 @ 08:21
    Matt Brailsford
    0

    Aaaah, awesome.

    Glad you got it all working 👍

  • Neil Hodges 193 posts 519 karma points
    Apr 16, 2019 @ 15:00
    Neil Hodges
    0

    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)

            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

  • Matt Brailsford 2149 posts 10592 karma points MVP 6x c-trib
    Apr 16, 2019 @ 16:22
    Matt Brailsford
    100

    Hi Niel,

    Does your code actually get hit? Or is it erroring before it even gets there?

  • Neil Hodges 193 posts 519 karma points
    Apr 30, 2019 @ 15:44
    Neil Hodges
    0

    Got this working well now, thank's for your help on it Matt. I can now calc the price dynamically

  • Matt Brailsford 2149 posts 10592 karma points MVP 6x c-trib
    Apr 30, 2019 @ 16:15
    Matt Brailsford
    0

    That's awesome. Glad you got it working 👍

Please Sign in or register to post replies

Write your reply to:

Draft