0

I have created a custom cart rule "Percent of product original price discount"

I am checking If coupon discount is better deal than special price for the customer then to use the coupon discounted price

/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */

   $discountData = $this->discountFactory->create();

//If coupon discount is better deal than special price for the customer then to use the coupon discounted price
if ($itemPrice > $originalpricewithdiscount || $baseItemPrice > $originalbasepricewithdiscount) {

      //Here i set discount             
   
      } elseif ($itemPrice == $itemOriginalPrice) { //Products with no special price

       //Here i set discount             
}
if ($discountData->getAmount() < 1){ //Here i check if dicount is 0 then throw exception. **(ISSUE - WHEN A COUPON IS APPLIED ON CART, THEN I TRY TO ADD PRODUCT WHICH IS NOT ELIGIBLE FOR DISCOUNT THEN DISCOUNT IS 0 & THAT'S WHY THIS EXCEPTION APPEARS ON ADD TO CART ALSO.)**
        throw new \Magento\Framework\Exception\LocalizedException(__("The Gift Card or Promo Code couldn't be applied. Verify and try again"));
    }
    return $discountData;

(ISSUE - WHEN A COUPON IS APPLIED ON CART, THEN I TRY TO ADD PRODUCT WHICH IS NOT ELIGIBLE FOR DISCOUNT THEN DISCOUNT IS 0 & THAT'S WHY THIS EXCEPTION APPEARS ON ADD TO CART ALSO.)

Please advise me some thoughts on this to make it work

1
  • any thoughts on this
    – devhs
    Jun 7, 2021 at 6:01

1 Answer 1

1

You must first check if the product meets your initial conditions.

Then you have to check whether the variables you are comparing are declared or have data. For example, if your $itemPrice has no data it will suppress the exception and your program will stop the moment you call it to compare.

1
  • Thanks for the answer
    – devhs
    Jun 10, 2021 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.