Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 93802cd

Browse files
author
Jens Schulze
authored
Tiered pricing (#305)
* feat(Price): Add Price Tiers to price * test(PriceTier): test PriceTiers for add update and remove LineItems * WIP: tiered price review changes
1 parent 8aa457a commit 93802cd

File tree

8 files changed

+160
-9
lines changed

8 files changed

+160
-9
lines changed

src/Model/Common/Price.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* @method Price setValidUntil(DateTime $validUntil = null)
3333
* @method CustomFieldObject getCustom()
3434
* @method Price setCustom(CustomFieldObject $custom = null)
35+
* @method PriceTierCollection getTiers()
36+
* @method Price setTiers(PriceTierCollection $tiers = null)
3537
*/
3638
class Price extends JsonObject
3739
{
@@ -44,6 +46,7 @@ class Price extends JsonObject
4446
const VALID_UNTIL = 'validUntil';
4547
const DISCOUNTED = 'discounted';
4648
const CUSTOM = 'custom';
49+
const TIERS = 'tiers';
4750

4851
public function fieldDefinitions()
4952
{
@@ -63,6 +66,7 @@ public function fieldDefinitions()
6366
],
6467
static::DISCOUNTED => [self::TYPE => DiscountedPrice::class],
6568
static::CUSTOM => [static::TYPE => CustomFieldObject::class],
69+
static::TIERS => [static::TYPE => PriceTierCollection::class]
6670
];
6771
}
6872

src/Model/Common/PriceDraft.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* @method PriceDraft setValidUntil(DateTime $validUntil = null)
2828
* @method CustomFieldObject getCustom()
2929
* @method PriceDraft setCustom(CustomFieldObject $custom = null)
30+
* @method PriceTierCollection getTiers()
31+
* @method PriceDraft setTiers(PriceTierCollection $tiers = null)
3032
*/
3133
class PriceDraft extends JsonObject
3234
{
@@ -38,6 +40,7 @@ class PriceDraft extends JsonObject
3840
const VALID_UNTIL = 'validUntil';
3941
const DISCOUNTED = 'discounted';
4042
const CUSTOM = 'custom';
43+
const TIERS = 'tiers';
4144

4245
public function fieldDefinitions()
4346
{
@@ -55,6 +58,7 @@ public function fieldDefinitions()
5558
self::DECORATOR => DateTimeDecorator::class
5659
],
5760
static::CUSTOM => [static::TYPE => CustomFieldObject::class],
61+
static::TIERS => [static::TYPE => PriceTierCollection::class]
5862
];
5963
}
6064

src/Model/Common/PriceTier.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Model\Common;
4+
5+
/**
6+
* @package Commercetools\Core\Model\Common
7+
*
8+
* @method int getMinimumQuantity()
9+
* @method PriceTier setMinimumQuantity(int $minimumQuantity = null)
10+
* @method Money getValue()
11+
* @method PriceTier setValue(Money $value = null)
12+
*/
13+
class PriceTier extends JsonObject
14+
{
15+
const MINIMUMQUANTITY = 'minimumQuantity';
16+
const VALUE = 'value';
17+
18+
public function fieldDefinitions()
19+
{
20+
return [
21+
static::MINIMUMQUANTITY => [static::TYPE => 'int'],
22+
static::VALUE => [self::TYPE => Money::class]
23+
];
24+
}
25+
/**
26+
* @param int $quantity
27+
* @param Money $money
28+
* @param Context|callable $context
29+
* @return PriceTier
30+
*/
31+
public static function ofQuantityAndMoney($quantity, Money $money, $context = null)
32+
{
33+
return static::of($context)->setValue($money)->setMinimumQuantity($quantity);
34+
}
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Model\Common;
4+
5+
/**
6+
* @package Commercetools\Core\Model\Common
7+
*
8+
* @method PriceTierCollection add(PriceTier $element)
9+
* @method PriceTier current()
10+
* @method PriceTier getAt($offset)
11+
*/
12+
class PriceTierCollection extends Collection
13+
{
14+
protected $type = PriceTier::class;
15+
}

src/Request/ShoppingLists/Command/ShoppingListSetDeleteDaysAfterLastModificationAction.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: ibrahimselim
5-
* Date: 16/03/17
6-
* Time: 15:02
7-
*/
82

93
namespace Commercetools\Core\Request\ShoppingLists\Command;
104

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ price:
273273
- validUntil
274274
- discounted
275275
- custom
276+
- tiers
276277

277278
customer:
278279
domain: customer

tests/integration/ApiTestCase.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@
108108
use Monolog\Handler\ErrorLogHandler;
109109
use Monolog\Handler\StreamHandler;
110110
use Monolog\Logger;
111+
use PHPUnit\Framework\TestCase;
111112
use Psr\Log\LogLevel;
112113
use Symfony\Component\Yaml\Yaml;
113114

114-
class ApiTestCase extends \PHPUnit\Framework\TestCase
115+
class ApiTestCase extends TestCase
115116
{
116117
private static $testRun;
117118
private static $client = [];
@@ -606,10 +607,13 @@ protected function getProductDraft()
606607
return $draft;
607608
}
608609

609-
protected function getProduct()
610+
protected function getProduct(ProductDraft $draft = null)
610611
{
611612
if (is_null($this->product)) {
612-
$request = ProductCreateRequest::ofDraft($this->getProductDraft());
613+
if (is_null($draft)) {
614+
$draft = $this->getProductDraft();
615+
}
616+
$request = ProductCreateRequest::ofDraft($draft);
613617
$response = $request->executeWithClient($this->getClient());
614618
$product = $request->mapResponse($response);
615619
$request = ProductUpdateRequest::ofIdAndVersion($product->getId(), $product->getVersion())

tests/integration/Cart/CartUpdateRequestTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Commercetools\Core\Model\Common\Money;
2424
use Commercetools\Core\Model\Common\MoneyCollection;
2525
use Commercetools\Core\Model\Common\PriceDraft;
26+
use Commercetools\Core\Model\Common\PriceTier;
27+
use Commercetools\Core\Model\Common\PriceTierCollection;
2628
use Commercetools\Core\Model\CustomField\CustomFieldObject;
2729
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
2830
use Commercetools\Core\Model\CustomField\FieldContainer;
@@ -1161,6 +1163,98 @@ public function testAutomaticDelete()
11611163
$this->assertSame(2, $cart->getDeleteDaysAfterLastModification());
11621164
}
11631165

1166+
public function testPriceTiersOnAddLineItem()
1167+
{
1168+
$productDraft = $this->getProductDraft();
1169+
$productDraft->getMasterVariant()->getPrices()->current()->setTiers(PriceTierCollection::of()
1170+
->add(
1171+
PriceTier::of()->setValue(Money::ofCurrencyAndAmount('EUR', 10))->setMinimumQuantity(2)
1172+
)
1173+
->add(
1174+
PriceTier::of()->setValue(Money::ofCurrencyAndAmount('EUR', 1))->setMinimumQuantity(3)
1175+
)
1176+
);
1177+
$product = $this->getProduct($productDraft);
1178+
$variant = $product->getMasterData()->getCurrent()->getMasterVariant();
1179+
1180+
$draft = $this->getDraft();
1181+
$draft->setLineItems(
1182+
LineItemDraftCollection::of()
1183+
->add(LineItemDraft::of()->setProductId($product->getId())->setVariantId($variant->getId())->setQuantity(1))
1184+
);
1185+
$cart = $this->createCart($draft);
1186+
1187+
$this->deleteRequest->setVersion($cart->getVersion());
1188+
1189+
$this->assertSame($product->getId(), $cart->getLineItems()->current()->getProductId());
1190+
$this->assertSame(
1191+
$product->getProductType()->getId(),
1192+
$cart->getLineItems()->current()->getProductType()->getId()
1193+
);
1194+
$this->assertSame(
1195+
100,
1196+
$cart->getLineItems()->current()->getPrice()->getValue()->getCentAmount()
1197+
);
1198+
1199+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1200+
->addAction(
1201+
CartAddLineItemAction::ofProductIdVariantIdAndQuantity($product->getId(), $variant->getId(), 1)
1202+
);
1203+
1204+
$response = $request->executeWithClient($this->getClient());
1205+
$cart = $request->mapResponse($response);
1206+
$this->deleteRequest->setVersion($cart->getVersion());
1207+
1208+
$this->assertSame($product->getId(), $cart->getLineItems()->current()->getProductId());
1209+
$this->assertSame(
1210+
$product->getProductType()->getId(),
1211+
$cart->getLineItems()->current()->getProductType()->getId()
1212+
);
1213+
$this->assertSame(
1214+
10,
1215+
$cart->getLineItems()->current()->getPrice()->getValue()->getCentAmount()
1216+
);
1217+
1218+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1219+
->addAction(
1220+
CartChangeLineItemQuantityAction::ofLineItemIdAndQuantity($cart->getLineItems()->current()->getId(), 3)
1221+
)
1222+
;
1223+
$response = $request->executeWithClient($this->getClient());
1224+
$cart = $request->mapResponse($response);
1225+
$this->deleteRequest->setVersion($cart->getVersion());
1226+
$this->assertSame(3, $cart->getLineItems()->current()->getQuantity());
1227+
$this->assertSame(
1228+
1,
1229+
$cart->getLineItems()->current()->getPrice()->getValue()->getCentAmount()
1230+
);
1231+
1232+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1233+
->addAction(
1234+
CartRemoveLineItemAction::ofLineItemId($cart->getLineItems()->current()->getId())->setQuantity(1)
1235+
)
1236+
;
1237+
$response = $request->executeWithClient($this->getClient());
1238+
$cart = $request->mapResponse($response);
1239+
$this->deleteRequest->setVersion($cart->getVersion());
1240+
1241+
$this->assertSame(2, $cart->getLineItems()->current()->getQuantity());
1242+
$this->assertSame(
1243+
10,
1244+
$cart->getLineItems()->current()->getPrice()->getValue()->getCentAmount()
1245+
);
1246+
1247+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1248+
->addAction(
1249+
CartRemoveLineItemAction::ofLineItemId($cart->getLineItems()->current()->getId())
1250+
)
1251+
;
1252+
$response = $request->executeWithClient($this->getClient());
1253+
$cart = $request->mapResponse($response);
1254+
$this->deleteRequest->setVersion($cart->getVersion());
1255+
$this->assertCount(0, $cart->getLineItems());
1256+
}
1257+
11641258
/**
11651259
* @return CartDraft
11661260
*/

0 commit comments

Comments
 (0)