Copied to clipboard

Flag this post as spam?

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


  • Luke Alderton 192 posts 509 karma points
    Dec 13, 2012 @ 17:12
    Luke Alderton
    0

    Shipping by weight plus original shipping fee

    Ive added shipping by weight to a website however it seems that the original price is overriding it?

    Im using the code below to work out shipping using "price per kg * weight in kg" I then add the cost of the selected shipping method to that and set order.shippingFeeWithoutVAT to the new value.

    When I go through the checkout with a shipping method cost of £0, the shipping by weight kicks in and shows £106 in the log which is what it should be, but in the checkout it still shows £0, however if I do the same but with a shipping method cost of £5, the shipping by weight kicks in and shows £111 which is also correct. Why would it be setting my shipping cost to £0 even after I have set it to £111?

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Web;
    using System.Xml.XPath;
    using TeaCommerce.Data;
    using TeaCommerce.Data.Extensibility;
    using TeaCommerce.Data.Tools;
    
    /// <summary>
    /// Summary description for TeaCommerceExtension
    /// </summary>
    namespace TeaCommerce.WebShop.Integration
    {
        public class TeaCommerceExtension : ITeaCommerceExtension
        {
            #region ITeaCommerceExtension Members
    
            public void Initialize()
            {
                WebshopEvents.OrderLineChanged += WebshopEvents_OrderLineChanged;
                WebshopEvents.ShippingMethodChanged += WebshopEvents_ShippingMethodChanged;
                WebshopEvents.CurrencyChanged += WebshopEvents_CurrencyChanged;
                WebshopEvents.OrderLineRemoved += WebshopEvents_OrderLineRemoved;
            }
    
            void WebshopEvents_OrderLineRemoved(Order order, OrderLine orderLine)
            {
                UpdateOrderShippingCost(order);
            }
    
            /// <summary>
            /// On currency change
            /// </summary>
            /// <param name="order"></param>
            /// <param name="currency"></param>
            void WebshopEvents_CurrencyChanged(Order order, Currency currency)
            {
                UpdateOrderShippingCost(order);
            }
    
            /// <summary>
            /// On shipping method change
            /// </summary>
            /// <param name="order"></param>
            /// <param name="shippingMethod"></param>
            void WebshopEvents_ShippingMethodChanged(Order order, ShippingMethod shippingMethod)
            {
                UpdateOrderShippingCost(order);
            }
    
            void WebshopEvents_OrderLineChanged(Order order, OrderLine orderLine)
            {
                UpdateOrderShippingCost(order);
            }
    
            #endregion
    
            Boolean debug = true;
    
            private void UpdateOrderShippingCost(Order order)
            {
                XPathNodeIterator allXml = umbraco.library.GetXmlAll();
    
                XPathNavigator shippingRules = allXml.Current.SelectSingleNode("//ShippingRules");
                decimal defaultWeightInKg = (decimal)PriceHelper.ParsePrice(shippingRules.SelectSingleNode("defaultWeight").Value);
                decimal defaultPricePerKg = (decimal)PriceHelper.ParsePrice(shippingRules.SelectSingleNode("defaultPricePerKG").Value);
    
                // Log a, error in the database
                if (debug)
                {
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                        umbraco.BusinessLogic.User.GetUser(0), -1,
                        "default weight: " + defaultWeightInKg + " and default price per kg: " + defaultPricePerKg);
                }
    
                decimal totalWeightInKg = 0;
                foreach (OrderLine ol in order.OrderLines)
                {
                    OrderLineProperty weightProp = ol.Properties.FirstOrDefault(p => p.Alias == "productWeight");
    
                    if (debug)
                    {
                        foreach (OrderLineProperty op in ol.Properties)
                        {
                            // Log a, error in the database
                            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                                umbraco.BusinessLogic.User.GetUser(0), -1,
                                "order property " + op.Alias + " is " + op.Value.ToString());
                        }
                    }
    
                    if (weightProp != null && !string.IsNullOrEmpty(weightProp.Value))
                    {
                        totalWeightInKg += (decimal)PriceHelper.ParsePrice(weightProp.Value.ToString()) * ol.Quantity;
                        if (debug)
                        {
                            // Log a, error in the database
                            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                                umbraco.BusinessLogic.User.GetUser(0), -1,
                                "reached product weight");
                        }
                    }
                    else
                    {
                        totalWeightInKg += defaultWeightInKg * ol.Quantity;
                    }
                }
    
                if (debug)
                {
                    // Log a, error in the database
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                        umbraco.BusinessLogic.User.GetUser(0), -1,
                        "total weight in kg: " + totalWeightInKg);
                }
    
                XPathExpression weightPathXPath = XPathExpression.Compile("//ShippingRule");
                weightPathXPath.AddSort("upToWeight", XmlSortOrder.Ascending, XmlCaseOrder.None, string.Empty, XmlDataType.Number);
                XPathNodeIterator weightRules = shippingRules.Select(weightPathXPath);
                decimal shippingPricePerKgWithoutVAT = defaultPricePerKg;
                while (weightRules.MoveNext())
                {
                    if (debug)
                    {
                        // Log a, error in the database
                        umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                            umbraco.BusinessLogic.User.GetUser(0), -1,
                            "found rule: " + weightRules.Current.SelectSingleNode("upToWeight").Value);
                    }
    
                    if (int.Parse(weightRules.Current.SelectSingleNode("upToWeight").Value) >= totalWeightInKg)
                    {
                        shippingPricePerKgWithoutVAT = (decimal)PriceHelper.ParsePrice(weightRules.Current.SelectSingleNode("shippingPrice").Value);
                        if (debug)
                        {
                            // Log a, error in the database
                            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                                umbraco.BusinessLogic.User.GetUser(0), -1,
                                "match: " + weightRules.Current.SelectSingleNode("upToWeight").Value);
                        }
                        break;
                    }
                };
    
                if (debug)
                {
                    // Log a, error in the database
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                        umbraco.BusinessLogic.User.GetUser(0), -1,
                        "shipping price per kg: " + shippingPricePerKgWithoutVAT);
                }
    
                decimal totalShippingPriceWithoutVAT = shippingPricePerKgWithoutVAT * totalWeightInKg;
    
                if (debug)
                {
                    // Log a, error in the database
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                        umbraco.BusinessLogic.User.GetUser(0), -1,
                        "total Shipping Price Without VAT: " + totalShippingPriceWithoutVAT);
                }
    
                OrderProperty shippingPriceOP = order.Properties.FirstOrDefault(p => p.Alias == "shippingPriceWithoutVAT" + order.Currency.ISOCode);
                OrderProperty totalWeightInKgProp = order.Properties.FirstOrDefault(p => p.Alias == "totalWeightInKg");
                if (shippingPriceOP == null)
                {
                    shippingPriceOP = new OrderProperty("shippingPriceWithoutVAT" + order.Currency.ISOCode, totalShippingPriceWithoutVAT.ToString());
                    order.AddProperty(shippingPriceOP);
                    totalWeightInKgProp = new OrderProperty("totalWeightInKg", totalWeightInKg.ToString());
                    order.AddProperty(totalWeightInKgProp);
                }
                else
                {
                    if (Decimal.Parse(shippingPriceOP.Value) != totalShippingPriceWithoutVAT)
                        shippingPriceOP.Value = totalShippingPriceWithoutVAT.ToString();
    
                    if (Decimal.Parse(totalWeightInKgProp.Value) != totalWeightInKg)
                        totalWeightInKgProp.Value = totalWeightInKg.ToString();
                }
    
                if (debug)
                {
                    // Log a, error in the database
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                        umbraco.BusinessLogic.User.GetUser(0), -1,
                        "shipping method cost is: " + order.ShippingMethod.GetFeeWithoutVAT(order));
                }
    
                order.ShippingFeeWithoutVAT = order.ShippingMethod.GetFeeWithoutVAT(order) + totalShippingPriceWithoutVAT;
    
                if (debug)
                {
                    // Log a, error in the database
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,
                        umbraco.BusinessLogic.User.GetUser(0), -1,
                        "final value added to order: " + order.ShippingFeeWithoutVAT);
                }
    
                //OrderProperty shippingPriceOP = order.Properties.FirstOrDefault(p => p.Alias == "shippingPriceWithoutVAT" + order.Currency.ISOCode);
                //OrderProperty totalWeightInKgProp = order.Properties.FirstOrDefault(p => p.Alias == "totalWeightInKg");
                //if (shippingPriceOP == null)
                //{
                //    shippingPriceOP = new OrderProperty("shippingPriceWithoutVAT" + order.Currency.ISOCode, totalShippingPriceWithoutVAT.ToString());
                //    order.AddProperty(shippingPriceOP);
                //    totalWeightInKgProp = new OrderProperty("totalWeightInKg", totalWeightInKg.ToString());
                //    order.AddProperty(totalWeightInKgProp);
                //}
                //else
                //{
                //    if (Decimal.Parse(shippingPriceOP.Value) != totalShippingPriceWithoutVAT)
                //        shippingPriceOP.Value = totalShippingPriceWithoutVAT.ToString();
    
                //    if (Decimal.Parse(totalWeightInKgProp.Value) != totalWeightInKg)
                //        totalWeightInKgProp.Value = totalWeightInKg.ToString();
                //}
                //if (order.ShippingMethod.Id == 2 && order.ShippingFeeWithoutVAT != totalShippingPriceWithoutVAT)
                //    order.ShippingFeeWithoutVAT = totalShippingPriceWithoutVAT;
    
                order.Save();
            }
        }
    }
    

    Thanks in advance,
    Luke

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 13, 2012 @ 19:51
    Rune Grønkjær
    0

    Hi Luke,

    Thanks for all the code :)

    Actually the way you add your fee to the order looks right after the book and you're saving the order afterwards:

    order.ShippingFeeWithoutVAT= order.ShippingMethod.GetFeeWithoutVAT(order)+ totalShippingPriceWithoutVAT;

    What happens when you set the ShippingFeeWithoutVAT is that the ManipulatedShippingFeeWithoutVAT is set behind the scenes. Next time you Get the ShippingFeeWithoutVAT Tea Commerce will hand you the ManipulatedShippingFeeWithoutVAT if its not null instead of OriginalShippingFeeWithoutVAT, which is just the backup.

    What does your last debug write?

    final value added to order

    /Rune

  • Luke Alderton 192 posts 509 karma points
    Dec 14, 2012 @ 10:05
    Luke Alderton
    0

    When the shipping method with a price is selected, it shows 'final value added to order: 106.0000' but when the shipping method with a price of £0 is set, it shows 'final value added to order: 0' any ideas why it's doing it then? It seems to work in one shop and not another however it's the exact same code since im compiling as a dll and then putting it into both sites; I'm not doing any other maniplulation to the prices...

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 14, 2012 @ 10:11
    Rune Grønkjær
    0

    It's really hard to say :(

    Sounds like it's not doing the calculations correctly. Probably some small bug in your code. I'm afraid the only solution is more debugging of each step in your process.

    Check that state of all your variables at alle critical points. Maybe you could try setting it up on your local machine. If not with the real website, then with a new umbraco installation and a quick setup of your scenario. Maybe our starter kit. Then you'll be up and running very fast and can debug everything in visual studio.

    Sorry I'm not much of an help.

    /Rune

  • Luke Alderton 192 posts 509 karma points
    Dec 14, 2012 @ 10:34
    Luke Alderton
    0

    It's weird since the same code works in another site, both sites started out from the starter kit too. It seems like it's not able to save the value to order.ShippingFeeWithoutVAT unless theres another shipping var that I should be saving to or a way I can disable some of the original shipping cost calculations?

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 14, 2012 @ 10:46
    Rune Grønkjær
    0

    Nope you are doing it correctly it looks like. Try checking what this line calculates to:

    order.ShippingMethod.GetFeeWithoutVAT(order)+ totalShippingPriceWithoutVAT

    /Rune

  • Luke Alderton 192 posts 509 karma points
    Dec 14, 2012 @ 11:38
    Luke Alderton
    0

    Okay so I've added a log for the value before it gets added to the order and it shows as

    'value before setting in ecommerce: 106.0000'

    and then pulling the value back out of the order shows

    'final value added to order: 0'

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 14, 2012 @ 12:12
    Rune Grønkjær
    0

    What is the order.IsOrder property at this point? If it's true that's your problem. Then a transaction have been made and the shipping price have been locked for the order!

    /Rune

  • Luke Alderton 192 posts 509 karma points
    Dec 14, 2012 @ 14:18
    Luke Alderton
    0

    Sorry for the late reply, I've just added a test in for the 'isorder' and it's showing false.

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 14, 2012 @ 14:24
    Rune Grønkjær
    0

    Hmm. Then I'm running out of questions. :(

  • Luke Alderton 192 posts 509 karma points
    Dec 14, 2012 @ 14:26
    Luke Alderton
    0

    Not completely out then? ;) It's a very strange output since another site that also started out fromt he starter kit runs it fine with the same values and same dll.

Please Sign in or register to post replies

Write your reply to:

Draft