In Magento 2.2.6, How to give 5% discount for a newly registered customer ?

Thanks in advanced!


For this requirement, you have to build a custom module and your custom module have below section.

  1. Run an observer on customer_register_success.On this observer create a Shopping Cart rules programmatically.

  2. Send that Coupon to the customer using a custom email.

Observer code:


namespace Stackexchange\Magento\Observer;

use Magento\SalesRule\Api\CouponRepositoryInterface;
use Magento\SalesRule\Api\Data\CouponInterface;
use Magento\SalesRule\Api\Data\RuleInterface;
use Magento\SalesRule\Api\RuleRepositoryInterface;
use Magento\SalesRule\Model\CouponFactory;
use Magento\SalesRule\Model\RuleFactory;
use Magento\Framework\Math\Random;
class CreateCouponCustomer  implements \Magento\Framework\Event\ObserverInterface{

     * @var Random
    private $random;

     * @var CouponFactory
    private $couponFactory;

     * @var RuleFactory
    private $ruleFactory;

     * @var CouponRepositoryInterface
    private $couponRepository;

     * @var RuleRepositoryInterface
    private $RuleRepository;

    public function __construct(
      CouponRepositoryInterface $couponRepository,
      RuleRepositoryInterface $RuleRepository,
      CouponFactory $couponFactory,   
      RuleFactory $ruleFactory,
      Random $random      
    ) {

        $this->RuleRepository = $RuleRepository;
        $this->couponRepository = $couponRepository;
        $this->ruleFactory = $ruleFactory;
        $this->couponFactory = $couponFactory;
        $this->random = $random;
    public function execute(\Magento\Framework\Event\Observer $observer)
        $customer = $observer->getEvent()->getCustomer();
        $rule =  $this->ruleFactory->create();
        $rule->setName('5% discount')

            $resultRules = $this->RuleRepository->save($rule);
        } catch (\Magento\Framework\Exception\LocalizedException $ex) {


    private function createCouponCode(RuleInterface $rule)
        $couponCode = $this->random->getRandomString(8);
        $coupon = $this->couponFactory->create();

For sending the email,you can use this link.

  • any readymate module available for this ? – Hardik Makwana Apr 10 at 7:54
  • I don't have the idea on the readymate module – Amit Bera Apr 10 at 8:00
  • any reference of a extension ? – Hardik Makwana Apr 10 at 8:49

Based on your question, i assume that you want to give discount to your customer for their first order.

So you can create a shopping cart rule with 5% discount keeping Uses per Customer as 1. So it will apply discount for only one time or for the first order you can say. For the second order, it wont be applicable as per magento default. If you go by this, you do not need to create coupon code and let it entered by custom on cart. You can advertise any where on your site that customer will get 5% discount on their first order. For example, you can show this message to register page. In this case, discount will automatically apply without any custom input.

But in case you want only for customers, signup after certain date, you need to create a plugin and a small function which will do the job for you.

Let me know if above will fulfil your requirement or you still want to create a plugin.


You can achieve this by the following manner.

Create a simple cart price rule.

  • Go to Admin > Marketing > Promotions > Cart Price Rule.
  • Click on the Add new rule
  • Make that cart price rule to have & applied using a specific coupon code set it as you want.
  • Allow that coupon code to be applied only once per customer (User Per Customer) & coupon can be used so many times because same coupon is going to be used by multiple users (better leave this field (Uses Per Coupons) empty).
  • Define the amount of discount & the expiry date of that coupon code.

Create a custom email template

  • Go to Admin > Marketing > Communication > Email Templates section.
  • Click on the New Email Template.
  • In load default template Template drop-down select New Account from Magento_Customer click on the load template.
  • Set the name of the template for proper identification.
  • Add your coupon code into that email template with the required text.
  • Click on the Preview Template to see the preview of the template & if everything is okay click on the save button.

Now lets replace this custom mail with the default mail.

  • Go to Admin > Stores > Settings > Configuration.
  • Go to Customer > Customer Configuration.
  • In Default Welcome Email drop-down select the custom mail that you have created.
  • Save the configuration & flush the cache.

Now to test this create a new customer & test the coupon code on the cart page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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