Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Sep 05, 2013 @ 01:27
    Dan
    0

    Tea Commerce: Get all orders in C# where custom property is matched

    Hi,

    I've built a promo code facility into Tea Commerce 2. One of the requirements is to allow customers to only use each promo code once.

    The promo code only works with registered members, and the value of the promo code is stored as a text string on the order object, as a property called 'promo'.

    I have some logic which finds all orders by the current member when they try to apply a promo code to their order. It looks like this:

    XElement allFinalizedOrdersAsXml = TC.GetAllFinalizedOrdersAsXml(storeId);
    var orders = allFinalizedOrdersAsXml.Elements().Where(e => e.Attribute("customerId").Value == memberId);
    

    This works - I can do 'orders.Count()' to tell whether the member has placed an order previously, but I also want to tell if they've used the promo code before. I've tried the following but it doesn't work:

    XElement allFinalizedOrdersAsXml = TC.GetAllFinalizedOrdersAsXml(storeId);
    var orders = allFinalizedOrdersAsXml.Elements().Where(e => e.Attribute("customerId").Value == memberId && e.Attribute("promo").Value == promoCode);
    

    Can anyone suggest how to get only orders which match the member and the promo code?

    Many thanks!

  • Rune Grønkjær 1372 posts 3103 karma points
    Sep 05, 2013 @ 08:28
    Rune Grønkjær
    100

    Hi Dan,

    You can just use TC.GetAllFinalizedOrdersForCustomer() to get all orders for the member. Then normal linq will do the rest. It will return a list of order objects instead of a XElement.

    /Rune

  • Anders Burla 2560 posts 8256 karma points
    Sep 05, 2013 @ 11:28
    Anders Burla
    1

    And if you want to use xml - then you should know that the xml structure for order properties is order/properties/promo

    Kind regards
    Anders

  • Dan 1288 posts 3921 karma points c-trib
    Sep 05, 2013 @ 23:08
    Dan
    0

    Great, thanks chaps. The final solution, which counts how many times a member has used the promo code, was like this:

    IEnumerable<Order> allFinalizedOrdersAsXml = TC.GetAllFinalizedOrdersForCustomer(storeId, memberId);
    int countPreviousOrders = 0;
    foreach (Order order in allFinalizedOrdersAsXml)
    {
        if(order.Properties["promo"] == promoCode){
            countPreviousOrders++;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft