|
17 | 17 | use Commercetools\Core\Model\Cart\LineItemCollection; |
18 | 18 | use Commercetools\Core\Model\Cart\LineItemDraft; |
19 | 19 | use Commercetools\Core\Model\Cart\LineItemDraftCollection; |
| 20 | +use Commercetools\Core\Model\CartDiscount\AbsoluteCartDiscountValue; |
20 | 21 | use Commercetools\Core\Model\CartDiscount\CartDiscountDraft; |
21 | 22 | use Commercetools\Core\Model\CartDiscount\CartDiscountTarget; |
22 | 23 | use Commercetools\Core\Model\CartDiscount\CartDiscountValue; |
| 24 | +use Commercetools\Core\Model\CartDiscount\LineItemsTarget; |
| 25 | +use Commercetools\Core\Model\CartDiscount\MultiBuyLineItemsTarget; |
| 26 | +use Commercetools\Core\Model\CartDiscount\RelativeCartDiscountValue; |
23 | 27 | use Commercetools\Core\Model\Common\Address; |
24 | 28 | use Commercetools\Core\Model\Common\LocalizedString; |
25 | 29 | use Commercetools\Core\Model\Common\Money; |
@@ -1185,11 +1189,11 @@ public function testDiscountCodeCustomPredicate() |
1185 | 1189 |
|
1186 | 1190 | $draft = CartDiscountDraft::ofNameValuePredicateTargetOrderActiveAndDiscountCode( |
1187 | 1191 | LocalizedString::ofLangAndText('en', 'test-' . $this->getTestRun() . '-discount'), |
1188 | | - CartDiscountValue::of()->setType('absolute')->setMoney( |
| 1192 | + AbsoluteCartDiscountValue::of()->setMoney( |
1189 | 1193 | MoneyCollection::of()->add(Money::ofCurrencyAndAmount('EUR', 100)) |
1190 | 1194 | ), |
1191 | 1195 | 'custom(testField = "' . $this->getTestRun() . '")', |
1192 | | - CartDiscountTarget::of()->setType('lineItems')->setPredicate('1=1'), |
| 1196 | + LineItemsTarget::of()->setPredicate('1=1'), |
1193 | 1197 | '0.9' . trim((string)mt_rand(1, 1000), '0'), |
1194 | 1198 | true, |
1195 | 1199 | true |
@@ -1218,6 +1222,58 @@ public function testDiscountCodeCustomPredicate() |
1218 | 1222 | ); |
1219 | 1223 | } |
1220 | 1224 |
|
| 1225 | + public function testMultiBuyDiscount() |
| 1226 | + { |
| 1227 | + $draft = $this->getDraft(); |
| 1228 | + $draft->setLineItems( |
| 1229 | + LineItemDraftCollection::of() |
| 1230 | + ->add(LineItemDraft::of()->setProductId($this->getProduct()->getId())->setVariantId(1)->setQuantity(3)) |
| 1231 | + ); |
| 1232 | + |
| 1233 | + $cart = $this->createCart($draft); |
| 1234 | + |
| 1235 | + $draft = CartDiscountDraft::ofNameValuePredicateTargetOrderActiveAndDiscountCode( |
| 1236 | + LocalizedString::ofLangAndText('en', 'test-' . $this->getTestRun() . '-discount'), |
| 1237 | + RelativeCartDiscountValue::of()->setPermyriad(10000), |
| 1238 | + '1=1', |
| 1239 | + MultiBuyLineItemsTarget::ofPredicateTriggerDiscountedAndMode( |
| 1240 | + '1=1', |
| 1241 | + 3, |
| 1242 | + 1, |
| 1243 | + MultiBuyLineItemsTarget::MODE_CHEAPEST |
| 1244 | + ), |
| 1245 | + '0.9' . trim((string)mt_rand(1, 1000), '0'), |
| 1246 | + true, |
| 1247 | + true |
| 1248 | + ); |
| 1249 | + $request = CartDiscountCreateRequest::ofDraft($draft); |
| 1250 | + $response = $request->executeWithClient($this->getClient()); |
| 1251 | + $this->cartDiscount = $request->mapResponse($response); |
| 1252 | + |
| 1253 | + $discountCode = $this->getDiscountCode(); |
| 1254 | + |
| 1255 | + $request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion()) |
| 1256 | + ->addAction(CartAddDiscountCodeAction::ofCode($discountCode->getCode())) |
| 1257 | + ; |
| 1258 | + $response = $request->executeWithClient($this->getClient()); |
| 1259 | + $cart = $request->mapResponse($response); |
| 1260 | + $this->deleteRequest->setVersion($cart->getVersion()); |
| 1261 | + |
| 1262 | + $this->assertSame($discountCode->getId(), $cart->getDiscountCodes()->current()->getDiscountCode()->getId()); |
| 1263 | + |
| 1264 | + $this->assertSame( |
| 1265 | + $this->cartDiscount->getId(), |
| 1266 | + $cart->getLineItems()->current() |
| 1267 | + ->getDiscountedPricePerQuantity()->current() |
| 1268 | + ->getDiscountedPrice()->getIncludedDiscounts()->current() |
| 1269 | + ->getDiscount()->getId() |
| 1270 | + ); |
| 1271 | + $this->assertSame( |
| 1272 | + $cart->getLineItems()->current()->getPrice()->getValue()->getCentAmount() * 2, |
| 1273 | + $cart->getLineItems()->current()->getTotalPrice()->getCentAmount() |
| 1274 | + ); |
| 1275 | + } |
| 1276 | + |
1221 | 1277 | public function testDiscountCodeCustomLineItemPredicate() |
1222 | 1278 | { |
1223 | 1279 | $type = $this->getType('key-' . $this->getTestRun(), 'line-item'); |
|
0 commit comments