Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Marianne Hildesheim Bertelsen 26 posts 169 karma points
    Dec 01, 2014 @ 21:50
    Marianne Hildesheim Bertelsen
    0

    Calculating discount in a custom basket pipeline task

    Hi

    I'm trying to calculate a volume discount on an orderline (if user buys 10 or more of the same product, he gets a 10% discount).

    Marketing Foundation can't do this for me the way I want it to, so if have to do it in a custom task in the Basket pipeline.

    <!-- Apply discounts -->
    <value>${Basket.ApplyAwards}</value>
    <value>${Basket.BasketCalculateDiscount}</value> <!-- <-------------- Here's my custom task -->
    <!-- Calculate order level discount total based on applied discounts -->
    <value>${Basket.CalculateOrderLinesUnitDiscounts}</value>

    I placed my custom task in the basket.config file as shown above. Is this the correct placement, chronologically speaking? I don't want subsequent tasks to delete or override my custom discount.

    My real problem is that I can't find any documentation as to how the syntax for the discount should be.

    Should I simply set a Discount = 123 on the OrderLine, or should I create a Discount object, and set its AmountOffTotal = 123 and add that to the OrderLine (and perhaps to the Order itself)?

    Should I save the Discount and/or the OrderLine and/or the Order since they all have Save-methods?

    An example of how to add a custom discount to an OrderLine programmatically would be greatly appreciated.

  • Morten Skjoldager 440 posts 1499 karma points
    Dec 02, 2014 @ 08:45
    Morten Skjoldager
    0

    Hi Marianne,

    Create a new discount object and set the order and orderline as references on that. You have to do just after ApplyAwards as you configured. Also set the discount amount as well :) 

    The entire order will be saved as the last step in the Pipeline so don't issue any saves at this point. 

    Hope that helps.

  • Marianne Hildesheim Bertelsen 26 posts 169 karma points
    Dec 02, 2014 @ 09:57
    Marianne Hildesheim Bertelsen
    0

    Hi again

    I put this in my pipeline task.

    var discountAmount = 100;

    var discount = new Discount();
    discount.AmountOffTotal = discountAmount;
    discount.PurchaseOrder = order;
    discount.AddOrderLine(orderline);

    The code is executed when I update the basket, but the discount doesn't appear on the website.

    Did I misunderstand what you wrote, or am I (still) missing something, Morten?

     

  • Morten Skjoldager 440 posts 1499 karma points
    Dec 02, 2014 @ 14:06
    Morten Skjoldager
    0

    We do it like this (more or less in our code):

    var discount = new Discount()
                {
                    CampaignName = CampaignItem.Campaign.Name,
                    CampaignItemName = CampaignItem.Name,
                    Description = GetDescriptionForDiscount(order),
                };
    discount.AmountOffTotal = AmountOff;
    
    order.AddDiscount(discount);
    orderLine.AddDiscount(discount); 

    Don't add it to the orderLine if it is an Orderlevel discount.

  • Marianne Hildesheim Bertelsen 26 posts 169 karma points
    Dec 02, 2014 @ 15:16
    Marianne Hildesheim Bertelsen
    0

    My code ended up looking like this:

    var discount = new Discount();
    discount.AmountOffTotal = discountAmount;
    
    order.AddDiscount(discount);
    orderline.AddDiscount(discount);

     I kept getting an nHibernate error when updating the quantity of the order line multiple times.

    NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: 0; expected: 1

     I think I found out that the problem was that we're using Angular to update the basket view, and that the basket pipeline was executed as soon as I touched the quantity input field, resulting in the pipeline being executed several times when changing the quantity e.g. from 10 to 20. 

    These multiple executions resulted in the nHibernate error since the save for execution #1 had not completed before execution #2 wanted to do something with the same order/orderline/discount.

    It should work if we fix it so that the basket pipeline won't be executed as often.

Please Sign in or register to post replies

Write your reply to:

Draft