I'm having trouble adding a Tea Commerce extension. I've followed http://rune.gronkjaer.dk/en-US/2010/11/26/how-to-use-the-tea-commerce-events/ whilst setting it up however it's not calling 'Initialize()' (or as far as I can tell its not) so I'm wondering what I'm doing wrong, the class is supposed to add shipping by weight into the shop however the shipping price isnt updated. Here's my code:
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 description for TeaCommerceExtension
///
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);
}
///
/// On currency change
///
///
///
void WebshopEvents_CurrencyChanged(Order order, Currency currency)
{
UpdateOrderShippingCost(order);
}
///
/// On shipping method change
///
///
///
void WebshopEvents_ShippingMethodChanged(Order order, ShippingMethod shippingMethod)
{
UpdateOrderShippingCost(order);
}
void WebshopEvents_OrderLineChanged(Order order, OrderLine orderLine)
{
UpdateOrderShippingCost(order);
}
#endregion
private void UpdateOrderShippingCost(Order order)
{
XPathNodeIterator allXml = umbraco.library.GetXmlAll();
XPathNavigator shippingRules = allXml.Current.SelectSingleNode("//Shipping Rules");
decimal defaultWeightInKg = (decimal)PriceHelper.ParsePrice(shippingRules.SelectSingleNode("defaultWeight").Value);
decimal totalWeightInKg = 0;
foreach (OrderLine ol in order.OrderLines)
{
OrderLineProperty weightProp = ol.Properties.FirstOrDefault(p => p.Alias == "productWeight");
if (weightProp != null && !string.IsNullOrEmpty(weightProp.Value))
totalWeightInKg += (decimal)PriceHelper.ParsePrice(weightProp.Value.ToString()) * ol.Quantity;
else
totalWeightInKg += defaultWeightInKg * ol.Quantity;
}
XPathExpression weightPathXPath = XPathExpression.Compile("./weightRange");
weightPathXPath.AddSort("upToWeight", XmlSortOrder.Ascending, XmlCaseOrder.None, string.Empty, XmlDataType.Number);
XPathNodeIterator weightRules = shippingRules.Select(weightPathXPath);
decimal shippingPricePerKgWithoutVAT = 0;
while (weightRules.MoveNext())
{
if (PriceHelper.ParsePrice(weightRules.Current.SelectSingleNode("upToWeight").Value) >= totalWeightInKg)
{
shippingPricePerKgWithoutVAT = (decimal)PriceHelper.ParsePrice(weightRules.Current.SelectSingleNode("shippingPrice" + order.Currency.ISOCode).Value);
break;
}
};
decimal totalShippingPriceWithoutVAT = shippingPricePerKgWithoutVAT * totalWeightInKg;
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();
}
}
}
Have you tried debugging it on your local IIS? That's a must do when doing advanced calculations like that. You need to be able to step throught your code and see if it runs, and runs properly.
If that's not an option you can use the Umbraco Log.Add() method to write to the Umbraco database log table. Then you can see what happens. But debugging locally is much faster.
Your code looks alright, so I can't say what is wrong.
Running localy is not realy an option since it's a remote site and would take a while to run. I've added the umbraco log and suddenly my code was being called? Not sure what I did but now I'm getting an error stating that 'Shipping Rules' has an invalid token, any ideas?
Might be some xpath in your .net code that is wrong.
Usually when I wan't to debug locally I copy both website files and database to my local maschine. In my experience that's much faster than fumbling in the dark :)
To to do an alernative to 'ParsePrice(System.String)' the error you need to look at is usualy under the inner exception.
System.MissingMethodException: Method not found: 'System.Decimal TeaCommerce.Data.Tools.PriceHelper.ParsePrice(System.String)'
Edit:
If you're building it in a seperate library (you should be) then make sure you have referenced the tea commerce dll or it won't find any helpers like the parse price one.
I'm working on a similar solution and need to make it work with shipping by weight. Would any of you be able to make a short guide on how-to-do and maybe even share your final code? I know it's a lot to ask for, but I'm having a tight deadline on the project .. you know ... should've been yesterday'ish.
Shipping by weight
I'm having trouble adding a Tea Commerce extension. I've followed http://rune.gronkjaer.dk/en-US/2010/11/26/how-to-use-the-tea-commerce-events/ whilst setting it up however it's not calling 'Initialize()' (or as far as I can tell its not) so I'm wondering what I'm doing wrong, the class is supposed to add shipping by weight into the shop however the shipping price isnt updated. Here's my code:
Hi Luke,
Have you tried debugging it on your local IIS? That's a must do when doing advanced calculations like that. You need to be able to step throught your code and see if it runs, and runs properly.
If that's not an option you can use the Umbraco Log.Add() method to write to the Umbraco database log table. Then you can see what happens. But debugging locally is much faster.
Your code looks alright, so I can't say what is wrong.
/Rune
Running localy is not realy an option since it's a remote site and would take a while to run. I've added the umbraco log and suddenly my code was being called? Not sure what I did but now I'm getting an error stating that 'Shipping Rules' has an invalid token, any ideas?
Might be some xpath in your .net code that is wrong.
Usually when I wan't to debug locally I copy both website files and database to my local maschine. In my experience that's much faster than fumbling in the dark :)
/Rune
Nvm it was the wrong name for the node. Thanks for your help anyway. ;)
Yep, thought so. :)
I am using this code and am now getting the following error when adding to cart:
Cart Error:
<error><![CDATA[MESSAGE:
Exception has been thrown by the target of an invocation.
STACKTRACE:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at TeaCommerce.Presentation.TeaCommerceBase.RequestModule.invokeMethod(restExtension myExtension, Object[] paras)
INNEREXCEPTION:
System.MissingMethodException: Method not found: 'System.Decimal TeaCommerce.Data.Tools.PriceHelper.ParsePrice(System.String)'.
at TeaCommerce.WebShop.Integration.TeaCommerceExtension.UpdateOrderShippingCost(Order order)
at TeaCommerce.WebShop.Integration.TeaCommerceExtension.WebshopEvents_OrderLineChanged(Order order, OrderLine orderLine)
at TeaCommerce.Data.Extensibility.WebshopEvents.SendOrderLineChanged(Order order, OrderLine orderLine)
at TeaCommerce.Data.Order.Save()
at TeaCommerce.Base.AddOrderLine(Order order, Int32 nodeId, Nullable`1 orderLineId, Decimal quantity, Boolean isUnique, Boolean overwriteQuantity, IDictionary`2 dynamicProperties)
at TeaCommerce.Base.AddOrderLine(Int32 nodeId, Nullable`1 orderLineId, Decimal quantity, Boolean isUnique)
at TeaCommerce.Base.AddOrderLine(Int32 nodeId, Decimal quantity)]]></error>
ANy ideas what to look at next?
Thanks
To to do an alernative to 'ParsePrice(System.String)' the error you need to look at is usualy under the inner exception.
System.MissingMethodException: Method not found: 'System.Decimal TeaCommerce.Data.Tools.PriceHelper.ParsePrice(System.String)'
Edit:
If you're building it in a seperate library (you should be) then make sure you have referenced the tea commerce dll or it won't find any helpers like the parse price one.
Luke, but it says Method not found, does it mean the value that I am trying to parse cannot be found?
Doubt 'System.String' wouldn't be found, make sure that you've referenced the Tea Commerce dll or it won't run.
Sure it is. It builds fine. Then I copy the DLL into the /bin of the Umbraco project.
Replaced with Decimal.Parse and it is working now. But should be able to use the helpers
My DLLs were different version on my build and live project.
Thought so ;)
Hi Luke or Rich.
I'm working on a similar solution and need to make it work with shipping by weight. Would any of you be able to make a short guide on how-to-do and maybe even share your final code? I know it's a lot to ask for, but I'm having a tight deadline on the project .. you know ... should've been yesterday'ish.
Thx a lot.
Søren
is working on a reply...