@@ -267,6 +267,93 @@ public function testCartPromotionsSingleCartRulesWithTaxes()
267
267
}
268
268
}
269
269
270
+ /**
271
+ * Apply cart rule with a fixed discount when specific coupon code
272
+ *
273
+ * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
274
+ * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
275
+ */
276
+ public function testCartPromotionsWithCoupons ()
277
+ {
278
+ $ objectManager = Bootstrap::getObjectManager ();
279
+ /** @var ProductRepositoryInterface $productRepository */
280
+ $ productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
281
+ /** @var Product $prod2 */
282
+ $ prod1 = $ productRepository ->get ('simple1 ' );
283
+ $ prod2 = $ productRepository ->get ('simple2 ' );
284
+ $ productsInCart = [$ prod1 , $ prod2 ];
285
+ $ prod2 ->setVisibility (Visibility::VISIBILITY_BOTH );
286
+ $ productRepository ->save ($ prod2 );
287
+ $ skus =['simple1 ' , 'simple2 ' ];
288
+
289
+ /** @var Collection $ruleCollection */
290
+ $ ruleCollection = $ objectManager ->get (Collection::class);
291
+ $ ruleLabels = [];
292
+ /** @var Rule $rule */
293
+ foreach ($ ruleCollection as $ rule ) {
294
+ $ ruleLabels = $ rule ->getStoreLabels ();
295
+ }
296
+ $ qty = 2 ;
297
+ // coupon code obtained from the fixture
298
+ $ couponCode = '2?ds5!2d ' ;
299
+ $ cartId = $ this ->createEmptyCart ();
300
+ $ this ->addMultipleSimpleProductsToCart ($ cartId , $ qty , $ skus [0 ], $ skus [1 ]);
301
+ $ this ->applyCouponToCart ($ cartId , $ couponCode );
302
+ $ query = $ this ->getCartItemPricesQuery ($ cartId );
303
+ $ response = $ this ->graphQlMutation ($ query );
304
+ $ this ->assertCount (2 , $ response ['cart ' ]['items ' ]);
305
+ $ productsInResponse = array_map (null , $ response ['cart ' ]['items ' ], $ productsInCart );
306
+ $ count = count ($ productsInCart );
307
+ for ($ itemIndex = 0 ; $ itemIndex < $ count ; $ itemIndex ++) {
308
+ $ this ->assertNotEmpty ($ productsInResponse [$ itemIndex ]);
309
+ $ sumOfPricesForBothProducts = 43.96 ;
310
+ $ rowTotal = ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty );
311
+ $ this ->assertResponseFields (
312
+ $ productsInResponse [$ itemIndex ][0 ],
313
+ [
314
+ 'quantity ' => $ qty ,
315
+ 'prices ' => [
316
+ 'row_total ' => ['value ' => $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty ],
317
+ 'row_total_including_tax ' => ['value ' => $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty ],
318
+ 'discount ' => ['value ' => round (($ rowTotal /$ sumOfPricesForBothProducts )*5 , 2 )],
319
+ 'discounts ' => [
320
+ 0 =>[
321
+ 'amount ' =>
322
+ ['value ' => round (($ rowTotal /$ sumOfPricesForBothProducts )*5 , 2 )],
323
+ 'label ' => $ ruleLabels [0 ]
324
+ ]
325
+ ]
326
+ ],
327
+ ]
328
+ );
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Apply coupon to the cart
334
+ *
335
+ * @param string $cartId
336
+ * @param string $couponCode
337
+ */
338
+ private function applyCouponToCart (string $ cartId , string $ couponCode )
339
+ {
340
+ $ query = <<<QUERY
341
+ mutation {
342
+ applyCouponToCart(input: {cart_id: " $ cartId", coupon_code: " $ couponCode"}) {
343
+ cart {
344
+ applied_coupon {
345
+ code
346
+ }
347
+ }
348
+ }
349
+ }
350
+ QUERY ;
351
+ $ response = $ this ->graphQlMutation ($ query );
352
+
353
+ self ::assertArrayHasKey ('applyCouponToCart ' , $ response );
354
+ self ::assertEquals ($ couponCode , $ response ['applyCouponToCart ' ]['cart ' ]['applied_coupon ' ]['code ' ]);
355
+ }
356
+
270
357
/**
271
358
* @param string $cartId
272
359
* @return string
0 commit comments