Restrict promotion code apply

Vote:
 

Hi Team,

Problem - Our customers are using first-time order promotions multiple with new accounts with different emails and the same address and same mobile.

How to restrict the promotion based on the user's shipping address/mobile number /user device?

How to implement our custom logic?

Can anyone please help this solve.

Advance thanks.

#268566
Dec 17, 2021 9:41
Vote:
 

Hi Karyan,

You can validate the promo code on checkout page if it's already in use on basis of address and phone number as following and then removed it from the cart.

Note: The shared query is expensive so might be take few seconds to execute.

  public bool IsUsedPromo(string line1, string line2, string daytimePhoneNumber, string eveningPhoneNumber, string couponCode)
  {
            OrderSearchOptions searchOptions = new OrderSearchOptions
            {
                CacheResults = false,
                Namespace = "Mediachase.Commerce.Orders"
            };

            searchOptions.Classes.Add("PurchaseOrder");

            StringBuilder sqlQuery = new StringBuilder();
            sqlQuery.Append("OrderGroupId IN (SELECT DISTINCT ");
            sqlQuery.Append("Summary.OrderGroupId ");
            sqlQuery.Append("FROM ( ");
            sqlQuery.Append("SELECT DISTINCT ");
            sqlQuery.Append("OG.OrderGroupId AS OrderGroupId, CAST(PO.Created AS Date) AS Created FROM OrderGroup_PurchaseOrder PO ");
            sqlQuery.Append("INNER JOIN OrderGroup OG on PO.ObjectId = OG.OrderGroupId ");
            sqlQuery.Append("INNER JOIN LineItem LG ON LG.OrderGroupId = OG.OrderGroupId ");
            sqlQuery.Append("INNER JOIN OrderGroupAddress OGA on OGA.OrderGroupId = OG.OrderGroupId ");
            sqlQuery.Append("INNER JOIN OrderFormEx OREX on OREX.ObjectId = LG.OrderFormId ");

            sqlQuery.Append("Where OGA.Line1 = '" + line1 + "' ");
            sqlQuery.Append("AND OGA.Line2 = '" + line2 + "' ");
            sqlQuery.Append("AND OGA.DaytimePhoneNumber = '" + daytimePhoneNumber + "' ");
            sqlQuery.Append("AND OGA.EveningPhoneNumber = '" + eveningPhoneNumber + "' ");
            sqlQuery.Append("AND OREX.Epi_CouponCodes = '" + couponCode + "' ");          
            sqlQuery.Append("ORDER BY Created DESC ");

            OrderSearchParameters parameters = new OrderSearchParameters
            {
                SqlMetaWhereClause = string.Empty,
                SqlWhereClause = sqlQuery.ToString()
            };

            PurchaseOrder[] purchaseOrderCollection = OrderContext.Current.Search<PurchaseOrder>(parameters, searchOptions);

            return purchaseOrderCollection.IsEmpty();
 }
#268569
Dec 17, 2021 12:08
Vote:
 

Just a note, order search could be very expensive to run and cause serious performance issue Don’t let order search kill your site – Quan Mai's blog (vimvq1987.com)

#268752
Dec 21, 2021 12:57
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.