Developer forum

Forum » Development » Custom ProductDiscount

Custom ProductDiscount

Kasper Vesth
Reply

I am trying to make a custom version of Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount.

In short I want to be able to only let it apply to products that isn't already included in other discounts. That part of the code is working.

The problem is, that when a product is rendered it only looks at the discounts that has the specific type "Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount"

if (Operators.CompareString(current2.DiscountType, typeof(ProductDiscount).FullName, false) == 0)

this is in: 

private System.Collections.Generic.List<ProductDiscount> GetAllProductDiscounts(Language language)

Are there, or can there be made, any way to make custom discounts available in RenderProducts? And on the same node, when it tries to apply my custom discount (inherited from ProductDiscount), this.ProductDiscountProduct is always empty, so it can't even be used in the cart.

Regards

Kasper


Replies

 
Nicolai Høeg Pedersen
Reply

Can I see your discount code?

We cannot render an orderline or order discount on a product, hence the implementation.

However, you can always make a ProductTemplateExtender and add the rendering your self.

You can also inherit ProductDiscount instead of SalesDiscountProvider - then it should show up in your rendering (i think!)

 
Kasper Vesth
Reply

This is my code. I inherit from ProductDiscount, but copied the fields to get a proper sorting on them and my custom field. When you save a SalesDiscount where you have selected this, it sets "SalesDiscountDiscountType" to "NoZebra.S_DW_FeenOgFroen.CustomCode.NZTotalSalesPriceDiscount", and as stated previously, the GetAllProductDiscounts method only looks for "Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount". 

    [AddInName("NZProduct discount")]
    [AddInIsAdditional(false)]
    [AddInDescription("The product specific discount lets you apply a discount to a certain product. E.g. a customer buys product X and gets a 10 percent discount on that product.")]
    [AddInOrder(1)]
    [AddInActive(true)]
    public class NZProductDiscount : ProductDiscount
    {
        private ProductsAndGroupsHandler _ProductsAndGroups;

        [AddInParameter("Products")]
        [AddInParameterEditor(typeof(ProductsAndGroupsEditor), "width=300px;height=100px")]
        public string ProductsAndGroups
        {
            get
            {
                if (this._ProductsAndGroups == null)
                    this._ProductsAndGroups = new ProductsAndGroupsHandler();
                return this._ProductsAndGroups.ToString();
            }
            set
            {
                if (this._ProductsAndGroups == null)
                    this._ProductsAndGroups = new ProductsAndGroupsHandler();
                this._ProductsAndGroups.ParseData(value, ProductsAndGroupsHandler.Types.All);
            }
        }

        [AddInParameterEditor(typeof(YesNoParameterEditor), "")]
        [AddInParameter("Don't apply to already discounted products")]
        public bool NotAlreadyDiscounted { get; set; }

        public NZProductDiscount()
        {
            this._ProductsAndGroups = new ProductsAndGroupsHandler();
        }

        public override void ProcessOrder(Order order)
        {
            List<OrderLine> orderLinesForDiscount = NotAlreadyDiscounted ? DiscountHelper.GetLinesWithoutDiscount(order) : order.ProductOrderLines.ToList();
            switch (this.DiscountValue.Type)
            {
                case DiscountTypes.FixedAmount:
                    OrderLine amountOrderline1 = DiscountHelper.CreateAmountOrderline(order, this.DiscountValue.Amount, false, null, this.IsProductDiscount, this.ProductDiscountProduct, this.DiscountName, this.DiscountID, orderLinesForDiscount);
                    order.OrderLines.Add(amountOrderline1, false);
                    break;
                case DiscountTypes.Percent:
                    OrderLine amountOrderline2 = DiscountHelper.CreateAmountOrderline(order, this.DiscountValue.Amount, true, null, this.IsProductDiscount, this.ProductDiscountProduct, this.DiscountName, this.DiscountID, orderLinesForDiscount);
                    order.OrderLines.Add(amountOrderline2, false);
                    break;
                case DiscountTypes.Products:
                    foreach (Product product in this.DiscountValue.ProductCollection)
                    {
                        order.OrderLines.Add(DiscountHelper.CreateProductOrderline(order, product, this.IsProductDiscount, this.ProductDiscountProduct, this.PageId, this.DiscountID), false);
                    }
                    break;
            }
        }
    }

 

 
Nicolai Høeg Pedersen
Reply

Hi Kasper

Ok. Just made a change to the Discount renderer so it renders everything that is a ProductDiscount or inherits from it.

TFS#21934, ready for the next 8.8.0.* hotfix - should be built today or Monday.

BR Nicolai

 
Kasper Vesth
Reply

It seems it hasn't been built yet :) Do you have a newer deadline for it?

 
Nicolai Høeg Pedersen
Reply

I'll push the build...

 
Christian Rud Skovgaard
Reply

Can be downloaded here once logged in: http://developer.dynamicweb.com/downloads/dynamicweb-8.aspx

Please test the mentioned functionality with your code before going live.

 

Kind regards

Christian

 
Kasper Vesth
Reply

It seems to be working as expected :) Thank you very much.

/Kasper

 

You must be logged in to post in the forum