@@ -73,22 +73,22 @@ class NewsletterSubscriptionDiscountApi implements NewsletterSubscriptionDiscoun
73
73
/**
74
74
* Constructor for NewsletterSubscriptionDiscountApi.
75
75
*
76
- * @param SubscriberFactory $subscriberFactory Factory for creating newsletter subscriber instances.
77
- * @param RuleFactory $ruleFactory Factory for creating sales rule instances.
78
- * @param OrderHelper $orderHelper Helper for order-related functionalities.
79
- * @param FBEHelper $fbeHelper Helper for Facebook Business Extension related functionalities.
80
- * @param MassgeneratorFactory $massGeneratorFactory Factory for creating mass coupon code generator instances.
76
+ * @param SubscriberFactory $subscriberFactory Factory for creating newsletter subscriber instances.
77
+ * @param RuleFactory $ruleFactory Factory for creating sales rule instances.
78
+ * @param OrderHelper $orderHelper Helper for order-related functionalities.
79
+ * @param FBEHelper $fbeHelper Helper for Facebook Business Extension related functionalities.
80
+ * @param MassgeneratorFactory $massGeneratorFactory Factory for creating mass coupon code generator instances.
81
81
* @param NewsletterSubscriptionDiscountStatus $newsletterSubscriptionDiscountStatus
82
- * @param Authenticator $authenticator Authenticator for API requests.
82
+ * @param Authenticator $authenticator Authenticator for API requests.
83
83
*/
84
84
public function __construct (
85
- SubscriberFactory $ subscriberFactory ,
86
- RuleFactory $ ruleFactory ,
87
- OrderHelper $ orderHelper ,
88
- FBEHelper $ fbeHelper ,
89
- MassgeneratorFactory $ massGeneratorFactory ,
85
+ SubscriberFactory $ subscriberFactory ,
86
+ RuleFactory $ ruleFactory ,
87
+ OrderHelper $ orderHelper ,
88
+ FBEHelper $ fbeHelper ,
89
+ MassgeneratorFactory $ massGeneratorFactory ,
90
90
NewsletterSubscriptionDiscountStatus $ newsletterSubscriptionDiscountStatus ,
91
- Authenticator $ authenticator
91
+ Authenticator $ authenticator
92
92
) {
93
93
$ this ->subscriberFactory = $ subscriberFactory ;
94
94
$ this ->ruleFactory = $ ruleFactory ;
@@ -102,16 +102,17 @@ public function __construct(
102
102
/**
103
103
* Subscribes a user for a coupon based on newsletter subscription.
104
104
*
105
- * @param string $externalBusinessId The external business ID.
106
- * @param string $email The email address of the subscriber.
107
- * @param int $ruleId The ID of the sales rule.
105
+ * @param string $externalBusinessId The external business ID.
106
+ * @param string $email The email address of the subscriber.
107
+ * @param int $ruleId The ID of the sales rule.
108
108
* @return CouponInterface The generated coupon.
109
109
* @throws LocalizedException If an error occurs during the process.
110
110
*/
111
111
public function subscribeForCoupon (string $ externalBusinessId , string $ email , int $ ruleId ): CouponInterface
112
112
{
113
113
$ this ->authenticator ->authenticateRequest ();
114
114
115
+ $ storeId = null ;
115
116
try {
116
117
$ storeId = (int )$ this ->orderHelper ->getStoreIdByExternalBusinessId ($ externalBusinessId );
117
118
if ($ this ->newsletterSubscriptionDiscountStatus ->checkSubscriptionStatus (
@@ -155,12 +156,16 @@ public function subscribeForCoupon(string $externalBusinessId, string $email, in
155
156
/**
156
157
* Generates a coupon code based on a sales rule ID.
157
158
*
158
- * @param int $ruleId The sales rule ID.
159
+ * @param int $ruleId The sales rule ID.
159
160
* @return string The generated coupon code.
160
161
*/
161
162
private function generateCoupon (int $ ruleId ): CouponInterface
162
163
{
163
164
$ rule = $ this ->ruleFactory ->create ()->load ($ ruleId );
165
+ if (!$ rule ->getId ()) {
166
+ throw new LocalizedException (__ ('The specified discount rule does not exist. ' ));
167
+ }
168
+
164
169
$ generator = $ this ->massGeneratorFactory ->create ();
165
170
$ generator ->setFormat (\Magento \SalesRule \Helper \Coupon::COUPON_FORMAT_ALPHANUMERIC );
166
171
$ generator ->setRuleId ($ ruleId );
@@ -169,8 +174,15 @@ private function generateCoupon(int $ruleId): CouponInterface
169
174
$ generator ->setLength (9 );
170
175
$ generator ->setPrefix ('META_SUBSCRIBER_ ' );
171
176
$ generator ->setSuffix ('' );
172
- $ rule ->setCouponCodeGenerator ($ generator );
173
- $ rule ->setCouponType (\Magento \SalesRule \Model \Rule::COUPON_TYPE_AUTO );
174
- return $ rule ->acquireCoupon ();
177
+ $ generator ->setQty (1 );
178
+
179
+ $ generator ->generatePool ();
180
+ $ coupons = $ generator ->getGeneratedCodes ();
181
+
182
+ if (empty ($ coupons )) {
183
+ throw new LocalizedException (__ ('Failed to generate coupon code. ' ));
184
+ }
185
+
186
+ return $ coupons [0 ];
175
187
}
176
188
}
0 commit comments