Is there a way that I can add VAT to the basket total on cart.aspx page? The scenario is, I want customer to select either add VAT to the basket or not to add VAT.
Customer will have 2 radion buttons, 1) add VAT, 2) do not add VAT on cart.aspx. If 1 is selected VAT should be added to basket total.
I understand that I can add VAT to the price group but is this possible to have something like above?
There's no problem doing something like that. Basically you'll add the radio button to either include or exclude VAT. Depending on how you've got your price groups set up you can either subtract the VAT (if your prices include VAT from the start) or add it (if your prices are set exlcuing VAT).
The selection the customer makes can be stored on the basket by calling
CommerceLibrary:SetOrderProperty("IncludeVAT", "true"); // or false ;)
With that in place I'd do the actual calculation in a pipeline task that you add to the basket pipeline. You can read all about creating pipeline task in the article uCommerce Pipelines Explained. Basically your new pipeline task will look at the order property (IncludeVAT) and perform the VAT calculation and set that on the individual order lines.
PurchaseOrder.VAT total is calculated automatically for you if you set on the individual order lines.
Relevant fields to store VAT are PurchaseOrder.VAT, OrderLine.VAT.
For your scenario uCommerce won't add VAT automatically as it's done when you add the product to the basket.
However you can trigger the internal VAT calculation inside your own pipeline task. You would look up the VAT values based on the current config and do the calculation yourself. The pipeline task could look like this:
public class MyPipelineTask : IPipelineTask<PurchaseOrder>
{
public PipelineExecutionResult Execute(PurchaseOrder subject)
{
bool includeVat = false;
if (!bool.TryParse(subject["IncludeVat"], out includeVat)) return PipelineExecutionResult.Success;
if (includeVat)
{
// Find the price group - assume only one configured
PriceGroup priceGroup = PriceGroup.All().FirstOrDefault(x => !x.Deleted);
// Find that configured price group
decimal vatRate = priceGroup.VATRate;
foreach (var orderLine in subject.OrderLines)
{
orderLine.VATRate = vatRate;
// VAT is stored on a per unit basis
orderLine.VAT = orderLine.Price*vatRate;
orderLine.Save();
// Line totals and VAT totals are calculated by a different task
}
}
return PipelineExecutionResult.Success;
}
}
Once you copy your assembly to the /bin folder you need to configure the pipeline task with uCommerce, more specifically in the basket pipeline.
The configuration file for the basket pipeline is found in /umbraco/ucommerce/pipelines/basket.config. My basket.config looks like this with the new task in place. My changes are highlighted in bold.
I have gone through your instructions and made a following applyVAT.cs
namespace applyVAT { public class MyPipelineTask : IPipelineTask<PurchaseOrder> { public PipelineExecutionResult Execute(PurchaseOrder subject) { bool includeVat = true; if (!bool.TryParse(subject["IncludeVat"], out includeVat)) return PipelineExecutionResult.Success;
if (includeVat) {
// Find the price group - assume only one configured PriceGroup priceGroup = PriceGroup.All().FirstOrDefault(x => !x.Deleted);
// Find that configured price group //decimal vatRate = priceGroup.VATRate; decimal vatRate = 17.5M; // I am seeting my own VAT here
foreach (var orderLine in subject.OrderLines) {
orderLine.VATRate = vatRate; // VAT is stored on a per unit basis orderLine.VAT = orderLine.Price * vatRate; orderLine.Save(); // Line totals and VAT totals are calculated by a different task } } return PipelineExecutionResult.Success; } } }
and the following is my /umbraco/ucommerce/pipelines/basket.config
But VAT is not appling for me, can you please advise where I am getting wrong? Enteires in my basket.config are ok as referenced to my namespace and class name?
The configuration looks right. Could you try outputting the basket XML when the basket pipeline is done executing so you can see the values of order line VAT and order total VAT?
Apply Vat on cart.aspx page
Hi Soren
Is there a way that I can add VAT to the basket total on cart.aspx page? The scenario is, I want customer to select either add VAT to the basket or not to add VAT.
Customer will have 2 radion buttons, 1) add VAT, 2) do not add VAT on cart.aspx. If 1 is selected VAT should be added to basket total.
I understand that I can add VAT to the price group but is this possible to have something like above?
Regards
Nauman
There's no problem doing something like that. Basically you'll add the radio button to either include or exclude VAT. Depending on how you've got your price groups set up you can either subtract the VAT (if your prices include VAT from the start) or add it (if your prices are set exlcuing VAT).
The selection the customer makes can be stored on the basket by calling
With that in place I'd do the actual calculation in a pipeline task that you add to the basket pipeline. You can read all about creating pipeline task in the article uCommerce Pipelines Explained. Basically your new pipeline task will look at the order property (IncludeVAT) and perform the VAT calculation and set that on the individual order lines.
PurchaseOrder.VAT total is calculated automatically for you if you set on the individual order lines.
Relevant fields to store VAT are PurchaseOrder.VAT, OrderLine.VAT.
Hope this helps.
Hi Soren
Thanks a lot for making the picture clear, however I need your some more help in writing the xslt code.
On top of cart.aspx I wrote <xsl:value-of select="CommerceLibrary:SetOrderProperty('IncludeVAT', 'true')"></xsl:value-of>
this I believe will tell ucommerce to add VAT or orderlines?
How can i execute orderpipeline here to set the VAT to orderlines???? (MY PRICE GROUP IS NOT INCLUDING VAT FROM START)
Further down the I am executing the basket pipeline <xsl:value-of select="CommerceLibrary:ExecuteBasketPipeline()"></xsl:value-of>
Regards
Nauman
For your scenario uCommerce won't add VAT automatically as it's done when you add the product to the basket.
However you can trigger the internal VAT calculation inside your own pipeline task. You would look up the VAT values based on the current config and do the calculation yourself. The pipeline task could look like this:
public class MyPipelineTask : IPipelineTask<PurchaseOrder> { public PipelineExecutionResult Execute(PurchaseOrder subject) { bool includeVat = false; if (!bool.TryParse(subject["IncludeVat"], out includeVat)) return PipelineExecutionResult.Success; if (includeVat) { // Find the price group - assume only one configured PriceGroup priceGroup = PriceGroup.All().FirstOrDefault(x => !x.Deleted); // Find that configured price group decimal vatRate = priceGroup.VATRate; foreach (var orderLine in subject.OrderLines) { orderLine.VATRate = vatRate; // VAT is stored on a per unit basis orderLine.VAT = orderLine.Price*vatRate; orderLine.Save(); // Line totals and VAT totals are calculated by a different task } } return PipelineExecutionResult.Success; } }
Once you copy your assembly to the /bin folder you need to configure the pipeline task with uCommerce, more specifically in the basket pipeline.
The configuration file for the basket pipeline is found in /umbraco/ucommerce/pipelines/basket.config. My basket.config looks like this with the new task in place. My changes are highlighted in bold.
Hi Soren
I have gone through your instructions and made a following applyVAT.cs
and the following is my /umbraco/ucommerce/pipelines/basket.config
But VAT is not appling for me, can you please advise where I am getting wrong? Enteires in my basket.config are ok as referenced to my namespace and class name?
Regards
Nauman
The configuration looks right. Could you try outputting the basket XML when the basket pipeline is done executing so you can see the values of order line VAT and order total VAT?
It should return an error if config is incorrect.
Thanks Soren, I have managed to resolve the above code.
Wonderful. Glad to hear it.
What was the fault? trying to do similar thing but gets "
Could not convert from 'ExcludeVAT.MyPipelineTask, ExcludeVAT' to System.Type - Maybe type could not be found"
Got a step further.. now gets
Value cannot be null.
It would be helpful to see where the code fails. I need a little more to go on. Thanks.
I ran into the same problem it was having issues converting the type.
I changed the pipeline task to use the updated type ie i changed "Entites" to "Entities2" and it worked.
change this line in basket.config
to this.
is working on a reply...