Posted on

Magento 2.4.0 = How to I can apply Cart Price Rule Condition in this coupon Code?

I want to Add Cart price rule condition in my custom auto generated Coupon Code. If email is equal to [email protected] How to i’ll do this. Please check my code. This code is working Perfectly i just want to add condition. Kindly help me on this. Thanks

protected function generateCouponCode()
{

try {

$subscriberEmail = $this->getEmail();
//$shoppingCartPriceRule = $this->_salesRule->create();

$couponData = [];
$couponData[‘name’] = ‘10% Gift Voucher Newsletter Subscription (‘.$this->getEmail().’)’;
$couponData[‘is_active’] = ‘1’;
$couponData[‘simple_action’] = ‘by_percent’;
$couponData[‘discount_amount’] = ’10’;
$couponData[‘from_date’] = date(‘Y-m-d’);
$couponData[‘to_date’] = ‘2025-12-31 23:59:59’;
$couponData[‘uses_per_coupon’] = ‘1’;
$couponData[‘coupon_type’] = ‘2’;
$couponData[‘customer_group_ids’] = $this->getCustomerGroupIds();
$couponData[‘website_ids’] = $this->getWebsiteIds();
/** @var MagentoSalesRuleModelRule $rule */
$rule = $this->_getSalesRule();

$couponCode = $rule->getCouponCodeGenerator()->setLength(7)->setAlphabet(
‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789’
)->generateCode().”;
$couponData[‘coupon_code’] = $couponCode;
$rule->loadPost($couponData);
$rule->save();
return $couponCode;
} catch (Exception $e) {
return null;
}
}

Leave a Reply

Your email address will not be published.