0

Using Magento 2.4.

I need some help in tackling a task.

In a store developed with Magento2 I need to create/extend the validation for coupon codes. This particular store relies heavily on coupon codes and some of them gives the customer 100% off on their orders. But, when this store integrates their sales with an external tool, the sales that are 100% off can't be integrated, because this tool doesn't accept the value 0 for sales' imports.

The lead, along with the stakeholders, decided to do the following:

  • Allow the creation of 100% off coupons and allow customers to use them, BUT, when they do use them (cart or checkout), process the coupon in a custom validation that will give them a message that says "Coupon AABBCC can only be used in sales that are over $$value", where "$$value" is the sale's value plus one.

I've been trying to run this validation for a while now, but, since I am new to Magento, I am having trouble in tackling this issue.

I am trying to extend salesrules validations' in a custom module, but, if I remember it correctly, the validation is triggered in js, and I am not succeeding in doing it.

If there are any github modules that may help me in this, please, name them for me. Or if any of you could point me in the right direction, it would be appreciated.

Thanks in advance.

1 Answer 1

0

I'm doing the same thing, here's how I did it.


For the shopping cart (before buying the product):

  • Add a custom observer and observe the controller_action_postdispatch_checkout_cart_couponPost event
    • You can get the coupon code with $request->getParam('coupon_code')
    • You can get the remove param with (bool)$request->getParam('remove')
    • You can check if the coupon code is valid by getting the quote and comparing the coupon code with the param: $quote->getCouponCode() === $couponFromRequest

For the checkout process itself:

  • Create a plugin for Magento\Quote\Api\CouponManagementInterface
    • Add an aroundSet method in your plugin
      • You can check if the coupon code is valid by calling $proceed($cartId, $couponCode)
    • Add an aroundRemove method in your plugin
      • You can check if the coupon code has been successfully removed by calling $proceed($cartId)

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.