Copied to clipboard

Flag this post as spam?

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


  • Ben McKean 272 posts 549 karma points
    Aug 10, 2021 @ 10:30
    Ben McKean
    0

    Adding discount programmatically and setting product to exclude

    Hi,

    I'm trying to create a discount programmatically and add a rule to exclude the discount on a specific product.

    If I create the discount manually, I can see the following JSON for the rule which I'm trying to match:

    { "ruleProviderAlias":"groupDiscountRule","settings":{ "matchType":"All"},"children":[{ "ruleProviderAlias":"orderLineProductExclude","settings":{ "nodeId":"umb://document/84487eb8cfe5473cb2cc169495ddaab1"},"children":[]}]}
    

    and I'm adding this rule in code as follows:

    var rule = new DiscountRuleConfig("groupDiscountRule", new Dictionary<string, string>()
    {
                        { "settings", MatchType.All.ToString() },
                        { "ruleProviderAlias", "orderLineProductExclude" },
                        { "nodeId", "umb://document/84487eb8cfe5473cb2cc169495ddaab1" }
    
    });
    
    discount.SetRules(rule, true, true);
    

    but that doesn't seem to work.

    Thanks in advance

    Ben

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Aug 10, 2021 @ 10:41
    Matt Brailsford
    100

    Hi Ben,

    The JSON you display in the first code block doesn't match what you are creating in the second block.

    You are actually trying to create two rule configs with the second being a child of the first so you'll need something more like this.

    var excludeRule = new DiscountRuleConfig("orderLineProductExclude", new new Dictionary<string, string>
    {
        { "nodeId", "umb://document/84487eb8cfe5473cb2cc169495ddaab1" }
    });
    
    var groupRule = new DiscountRuleConfig("groupDiscountRule", new Dictionary<string, string>
    {
        { "matchType", MatchType.All.ToString() }
    },
    new[]{ excludeRule });
    
    discount.SetRules(groupRule, true, true);
    

    Notice that the inner rule is defined separately and passed as a child rule to the group rule. With what you had before, you were basically trying to push all the config values into the settings of the group rule, which isn't what the first code block was doing.

    Hope that helps

    Matt

  • Ben McKean 272 posts 549 karma points
    Aug 10, 2021 @ 11:01
    Ben McKean
    0

    Works perfectly and makes sense Matt. Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft