Create a custom rule type option (criteria type)
Describes how to provide a discount to customers who have the default payment method set to ACH or Credit Card in Optimizely Configured Commerce.
In this example, the website needs to provide a discount to customers who have the default payment method set to ACH or Credit Card. This requires building a custom derived criteriatype class that will serve as the rule type option for promotions.
This example will use a LookupObject to populate the simple value for the Terms Code when configuring the Rule Type Option in the Admin Console.
Prerequisites
- The payment methods of ACH and CC must be configured in the Admin console
- The customers must have the Default Payment Method configured on their account
- Shipping carriers and services must be configured in order to complete the checkout process
-
In the Configured Commerce Web project in Visual Studio create a new folder called Extensions.
-
Add a new Class called CriteriaTypeOrderTermsCode
-
Derive from the base class CriteriaTypeBase and decorate the class with the Dependency attribute with a name of OrderTermsCode.
[DependencyName("OrderTermsCode")] public class CriteriaTypeOrderTermsCode : CriteriaTypeBase ```
-
Override the LookupObject and CriteriaObject properties. These are two important overrides to note as the PaymentMethod will populate the drop down in the Admin Console and the CustomerOrder will be the object this rule operates on.
public override string LookupObject => nameof(PaymentMethod); public override string CriteriaObject => nameof(CustomerOrder); ```
-
Override the remaining abstract properties as follows:
/// <summary>The requires criteria object.</summary> public override bool RequiresCriteriaObject => false; // <summary>The requires criteria property.</summary> public override bool RequiresCriteriaProperty => false; /// <summary>The requires criteria value.</summary> public override bool RequiresCriteriaValue => false; /// <summary>The criteria value label.</summary> public override string CriteriaValueLabel => string.Empty; /// <summary>The requires value type.</summary> public override bool RequiresValueType => false; /// <summary>The requires geocode.</summary> public override bool RequiresGeocode => false;
-
The Terms Code will validate whether the customer has a particular Terms Code or not. Override and add the equals and not equals rule type comparison operators to the ValidRuleClauseComparisonOperators collection.
/// <summary>The valid rule clause comparison operators.</summary> public override IList<RuleClauseComparisonOperator> ValidRuleClauseComparisonOperators => new List<RuleClauseComparisonOperator> { RuleClauseComparisonOperator.Equals, RuleClauseComparisonOperator.NotEquals };
-
Next define the parameters that can be configured using the Admin Console. In this example it will use a "Select Based On" parameter to choose between the equals or not equals comparison operators. It will also use a Terms Code value that will be selected from a drop down of all the valid options. This is where the LookupObject is leveraged. Override the ParameterDescriptions property and add the two dictionary values.
/// <summary>The parameter descriptions.</summary> public override Dictionary<string, CriteriaTypeParameter> ParameterDescriptions => new Dictionary<string, CriteriaTypeParameter> { { nameof(RuleClause.ComparisonOperator), new CriteriaTypeParameter { Label = "Select Based On", PossibleValues = this.ValidRuleClauseComparisonOperators.Select(x => x.ToString()).ToArray() } }, { nameof(RuleClause.SimpleValue), new CriteriaTypeParameter { Label = "Terms Code", LookupField = true } } }; ```
-
Override the Validate method which will handle the logic of the configuration defined and selected. The following will validate the argument is a CustomerOrder, as configured in the CriteriaObject property, and return true or false based on the value configured to the customer.
/// <summary>The validate.</summary> /// <param name="ruleClause">The rule clause.</param> /// <param name="argument">The argument.</param> /// <returns>The <see cref="bool"/>.</returns> public override bool Validate(RuleClause ruleClause, object argument) { if (!(argument is CustomerOrder)) { throw new ArgumentException("argument must be of type CustomerOrder"); } var customerOrder = (CustomerOrder)argument; if (ruleClause.ComparisonOperator == RuleClauseComparisonOperator.Equals.ToString()) { if (ruleClause.SimpleValue == customerOrder.TermsCode) { return true; } else return false; } if (ruleClause.ComparisonOperator == RuleClauseComparisonOperator.NotEquals.ToString()) { if (ruleClause.SimpleValue != customerOrder.TermsCode) { return true; } else return false; } throw new ArgumentException(nameof(ruleClause.ComparisonOperator) + " is not valid, it was " + ruleClause.ComparisonOperator); }
-
Build the solution and log into the Admin Console.
-
Go to Administration > System > Rule Types, and edit the Promotion Rule Type.
-
Go to the Rule Options finger tab and click the Add Rule Type button.
-
Configure the new Rule Type Option as follows:
- Description – Terms Code
- Criteria Type – Order Terms Code
- Criteria Object – CustomerOrder
-
Save the changes.
-
Go to Marketing > Promotions and create two new promotions. Create one promotion with the Terms Code rule using the ACH terms code value and equals operator. Create the other promotion rule with the Terms Code rule using CC as the terms code value and equals operator. For both, the result should be Percentage off the order. Experiment with these values where appropriate. For example, set the ACH terms code result to 25% off the order and set the CC terms code result to 5% off the order.
-
Save the changes.
-
Go to Websites and edit the existing Configured Commerce website.
-
Go to the Promotions finger tab and assign the two Promotions to the website.
-
Finally, go to the Settings finger tab and set the AllowCreditcardPaymentMethod as false.
-
Save the changes and go to the website site.
-
Complete the checkout process signed in as a customer with ACH and note the discount applied on the order.
Updated over 1 year ago