Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Volume Discount on Total Shopping cart View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 11/28/2011 10:14:36 PM
   
Volume Discount on Total Shopping cart
Hi Guys,

I can see that the volume discount works on individual products in the shopping cart.

Is it possible to have volume discount for the total shopping cart?
Ex: If there are total 10 products in the shopping cart then $2 discount on each quantity.

Thanks
Gitesh Shah


User avatar
Member
Member
kentico_michal - 11/29/2011 5:50:05 AM
   
RE:Volume Discount on Total Shopping cart
Hello,

You will need to override the CalculateOrderDiscountInternal method of the ShoppingCartInfoProvider provider. This method calculates discount which should be applied to the total items price.

For more information about customizing specific public method of the specific provider, I would like to point to this blog: E-commerce 6 - New customization model.

Best regards,
Michal Legen

User avatar
Member
Member
info-kentico-template - 12/1/2011 7:59:30 PM
   
RE:Volume Discount on Total Shopping cart
Hi,

I am looking for a similar customization.

But I am not very good at C# coding.

Could you please tell me in which file am I suppose to make this changes?

And also a link to the example or a code would be really appreciated.

Thanks
Gitesh Shah

User avatar
Member
Member
kentico_michal - 12/2/2011 3:14:42 AM
   
RE:Volume Discount on Total Shopping cart
Please take a look at the following code snippet that shows a simple implementation of this method:

public class CustomShoppingCartInfoProvider : ShoppingCartInfoProvider
{
/// <summary>
/// Calculates discount which should be applied to the total items price.
/// </summary>
/// <param name="cart">Shopping cart</param>
protected override double CalculateOrderDiscountInternal(ShoppingCartInfo cart)
{
double result = base.CalculateOrderDiscountInternal(cart);

// Example of order discount based on the time of shopping - Happy hours (4 PM - 7 PM)
if ((DateTime.Now.Hour >= 16) && (DateTime.Now.Hour <= 19))
{
// 20% discount
result = result + cart.TotalItemsPriceInMainCurrency * 0.2;
}
// Example of order discount based on the total price of all cart items
else if (cart.TotalItemsPriceInMainCurrency > 500)
{
// 10% discount
result = result + cart.TotalItemsPriceInMainCurrency * 0.1;
}

return result;
}
}


For more information about registration of this custom provider, please read the following blog: New customization model.


Best regards,
Michal Legen

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 12/6/2011 8:41:54 PM
   
RE:Volume Discount on Total Shopping cart
Hi michal,

Thanks for the code example. It works perfect in my scenerio.

Just one more help on this, How can I get the Total Units value in the shopping cart?

I am trying to divide the cart.TotalItemsPriceInMainCurrency with the help of currency

Thanks
Gitesh Shah

User avatar
Member
Member
kentico_michal - 12/9/2011 1:33:17 AM
   
RE:Volume Discount on Total Shopping cart
Hi Gitesh,

If I am getting you right, the following code could help you:


ShoppingCartInfo cartInfo = CMS.Ecommerce.ECommerceContext.CurrentShoppingCart;

int totalUnits = 0;

foreach (ShoppingCartItemInfo item in cartInfo.CartItems)
{
totalUnits += item.CartItemUnits;
}

Best regards,
Michal Legen

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 2/19/2012 9:47:54 PM
   
RE:Volume Discount on Total Shopping cart
HI Michal,

Thanks for the above code. It works.

I need one more help on this.

Can you please tell me the code for checking, whether the cart is having a coupon code in it or not?

Thanks
Gitesh Shah


User avatar
Member
Member
kentico_michal - 2/20/2012 3:34:40 AM
   
RE:Volume Discount on Total Shopping cart
Hi Gitesh,

You can check the ShoppingCartInfoObj.ShoppingCartDiscountCouponID property that contains the DiscountCouponID if the coupon has been provided, otherwise, it contains 0.

Then, you can use the following code to get the DiscountCouponInfo object:

DiscountCouponInfo dci = DiscountCouponInfoProvider.GetDiscountCouponInfo(DiscountCouponID);

Best regards,
Michal Legen

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 2/20/2012 4:48:05 PM
   
RE:Volume Discount on Total Shopping cart
Hi Michal,

I have added those lines to my code but I think I am not sure under which class am I suppose to add it.

Below is the error message that I am getting afteer adding those lines:


CS1061: 'CustomShoppingCartInfoProvider' does not contain a definition for 'ShoppingCartInfoObj' and no extension method 'ShoppingCartInfoObj' accepting a first argument of type 'CustomShoppingCartInfoProvider' could be found (are you missing a using directive or an assembly reference?)

Error Line: if (this.ShoppingCartInfoObj.ShoppingCartDiscountCouponID > 0)




Below is my complete code for CustomShoppingCartInfoProvider.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using CMS.Ecommerce;

using CMS.GlobalHelper;
using CMS.EcommerceProvider;


public class CustomShoppingCartInfoProvider : ShoppingCartInfoProvider
{

protected override double CalculateOrderDiscountInternal(ShoppingCartInfo cart)
{
double result = base.CalculateOrderDiscountInternal(cart);

double UnitPrice = 24.90;
double CartAmount = cart.TotalItemsPriceInMainCurrency;
double CartQuantity = CartAmount/UnitPrice;
double DiscountAmount =0;//Discount should be declared in if Condition
double NewPricePerUnit = 0;//UnitPrice - DiscountAmount
double NewCartAmountAfterDiscount = 0;//CartQuantity * NewPricePerUnit



if (this.ShoppingCartInfoObj.ShoppingCartDiscountCouponID > 0)
{
DiscountCouponInfo dci = DiscountCouponInfoProvider.GetDiscountCouponInfo(ShoppingCartInfoObj.ShoppingCartDiscountCouponID);
}

if(CMS.CMSHelper.CMSContext.CurrentSiteName == "EcommerceSite")
{

if (CartQuantity == 1)
{
}

else if (CartQuantity == 2)
{
DiscountAmount = 1;
NewPricePerUnit = UnitPrice - DiscountAmount;
NewCartAmountAfterDiscount = CartQuantity * NewPricePerUnit;
result = CartAmount - NewCartAmountAfterDiscount;
}

else if (CartQuantity == 4)
{
DiscountAmount = 3;
NewPricePerUnit = UnitPrice - DiscountAmount;
NewCartAmountAfterDiscount = CartQuantity * NewPricePerUnit;
result = CartAmount - NewCartAmountAfterDiscount;
}

else if ((CartQuantity >= 5) && (CartQuantity < 10))
{
DiscountAmount = 4;
NewPricePerUnit = UnitPrice - DiscountAmount;
NewCartAmountAfterDiscount = CartQuantity * NewPricePerUnit;
result = CartAmount - NewCartAmountAfterDiscount;
}

else if (CartQuantity >= 10)
{
DiscountAmount = 5.40;
NewPricePerUnit = UnitPrice - DiscountAmount;
NewCartAmountAfterDiscount = CartQuantity * NewPricePerUnit;
result = CartAmount - NewCartAmountAfterDiscount;
}
else
{

result = 6;
}

}

return result;
}
}


Am I missing a assembly?

Your help is much appreciated.

Thanks
Gitesh Shah

User avatar
Member
Member
kentico_michal - 2/21/2012 3:21:26 AM
   
RE:Volume Discount on Total Shopping cart
Hello Gitesh,

The CalculateOrderDiscountInternal method accepts the ShoppingCartInfo object as an input parameter and this class provides the property ShoppingCartDiscountCouponID. So, please try to use the following condition:

if (cart.ShoppingCartDiscountCouponID > 0)
{
....
}

Best regards,
Michal Legen

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 2/21/2012 3:34:48 PM
   
RE:Volume Discount on Total Shopping cart
Thanks Michal,

That works great.

Cheers
Gitesh Shah