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

Commit 3b81973

Browse files
author
Jens Schulze
committed
feat(Cart): support add line item by sky
Closes #341
1 parent d4ea598 commit 3b81973

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/Core/Model/Cart/LineItemDraft.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Commercetools\Core\Model\Cart;
77

88
use Commercetools\Core\Model\Channel\ChannelReference;
9+
use Commercetools\Core\Model\Common\Context;
910
use Commercetools\Core\Model\Common\JsonObject;
1011
use Commercetools\Core\Model\Common\LocalizedString;
1112
use Commercetools\Core\Model\Common\Money;
@@ -38,6 +39,8 @@
3839
* @method LineItemDraft setExternalPrice(Money $externalPrice = null)
3940
* @method ExternalLineItemTotalPrice getExternalTotalPrice()
4041
* @method LineItemDraft setExternalTotalPrice(ExternalLineItemTotalPrice $externalTotalPrice = null)
42+
* @method string getSku()
43+
* @method LineItemDraft setSku(string $sku = null)
4144
*/
4245
class LineItemDraft extends JsonObject
4346
{
@@ -53,6 +56,29 @@ public function fieldDefinitions()
5356
'externalPrice' => [static::TYPE => Money::class],
5457
'externalTotalPrice' => [static::TYPE => ExternalLineItemTotalPrice::class],
5558
'custom' => [static::TYPE => CustomFieldObject::class],
59+
'sku' => [static::TYPE => 'string'],
5660
];
5761
}
62+
63+
/**
64+
* @param string $productId
65+
* @param Context|callable $context
66+
* @return LineItemDraft
67+
*/
68+
public static function ofProductId($productId, $context = null)
69+
{
70+
$draft = static::of($context);
71+
return $draft->setProductId($productId);
72+
}
73+
74+
/**
75+
* @param string $sku
76+
* @param Context|callable $context
77+
* @return LineItemDraft
78+
*/
79+
public static function ofSku($sku, $context = null)
80+
{
81+
$draft = static::of($context);
82+
return $draft->setSku($sku);
83+
}
5884
}

src/Core/Request/Carts/Command/CartAddLineItemAction.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* @method CartAddLineItemAction setExternalPrice(Money $externalPrice = null)
3737
* @method ExternalLineItemTotalPrice getExternalTotalPrice()
3838
* @method CartAddLineItemAction setExternalTotalPrice(ExternalLineItemTotalPrice $externalTotalPrice = null)
39+
* @method string getSku()
40+
* @method CartAddLineItemAction setSku(string $sku = null)
3941
*/
4042
class CartAddLineItemAction extends AbstractAction
4143
{
@@ -45,6 +47,7 @@ public function fieldDefinitions()
4547
'action' => [static::TYPE => 'string'],
4648
'productId' => [static::TYPE => 'string'],
4749
'variantId' => [static::TYPE => 'int'],
50+
'sku' => [static::TYPE => 'string'],
4851
'quantity' => [static::TYPE => 'int'],
4952
'supplyChannel' => [static::TYPE => ChannelReference::class],
5053
'distributionChannel' => [static::TYPE => ChannelReference::class],
@@ -67,6 +70,17 @@ public static function ofProductIdVariantIdAndQuantity($productId, $variantId, $
6770
return static::of($context)->setProductId($productId)->setVariantId($variantId)->setQuantity($quantity);
6871
}
6972

73+
/**
74+
* @param string $sku
75+
* @param Context|callable $context
76+
* @param int $quantity
77+
* @return CartAddLineItemAction
78+
*/
79+
public static function ofSkuAndQuantity($sku, $quantity, $context = null)
80+
{
81+
return static::of($context)->setSku($sku)->setQuantity($quantity);
82+
}
83+
7084
/**
7185
* @param array $data
7286
* @param Context|callable $context

tests/integration/Cart/CartUpdateRequestTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ public function testLineItem()
176176
$this->assertCount(0, $cart->getLineItems());
177177
}
178178

179+
public function testLineItemBySku()
180+
{
181+
$product = $this->getProduct();
182+
$variant = $product->getMasterData()->getCurrent()->getMasterVariant();
183+
184+
$draft = $this->getDraft();
185+
$draft->setLineItems(LineItemDraftCollection::of()->add(LineItemDraft::ofSku($variant->getSku())));
186+
$cart = $this->createCart($draft);
187+
188+
$this->assertSame(1, $cart->getLineItems()->current()->getQuantity());
189+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
190+
->addAction(
191+
CartAddLineItemAction::ofSkuAndQuantity($variant->getSku(), 1)
192+
)
193+
;
194+
$response = $request->executeWithClient($this->getClient());
195+
$cart = $request->mapResponse($response);
196+
var_dump((string)$response->getBody());
197+
$this->deleteRequest->setVersion($cart->getVersion());
198+
199+
$this->assertSame(2, $cart->getLineItems()->current()->getQuantity());
200+
}
201+
179202
public function testSetExternalLineItemTotalPrice()
180203
{
181204
$draft = $this->getDraft();

tests/unit/Request/GenericActionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public function actionFieldProvider()
598598
],
599599
[
600600
CartAddLineItemAction::class,
601-
['action', 'productId', 'variantId', 'quantity', 'supplyChannel', 'distributionChannel', 'custom', 'externalTaxRate', 'externalPrice', 'externalTotalPrice']
601+
['action', 'productId', 'variantId', 'quantity', 'supplyChannel', 'distributionChannel', 'custom', 'externalTaxRate', 'externalPrice', 'externalTotalPrice', 'sku']
602602
],
603603
[
604604
CartChangeLineItemQuantityAction::class,

0 commit comments

Comments
 (0)