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

Commit

Permalink
feat(ShoppingList): support add line item by SKU
Browse files Browse the repository at this point in the history
Closes #374
  • Loading branch information
Jens Schulze committed Feb 22, 2018
1 parent 641d0ba commit 7172e1c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Core/Model/ShoppingList/LineItemDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Commercetools\Core\Model\ShoppingList;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\Common\JsonObject;
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
use Commercetools\Core\Model\Common\DateTimeDecorator;
Expand All @@ -23,6 +24,8 @@
* @method LineItemDraft setCustom(CustomFieldObjectDraft $custom = null)
* @method DateTimeDecorator getAddedAt()
* @method LineItemDraft setAddedAt(DateTime $addedAt = null)
* @method string getSku()
* @method LineItemDraft setSku(string $sku = null)
*/
class LineItemDraft extends JsonObject
{
Expand All @@ -37,6 +40,29 @@ public function fieldDefinitions()
static::TYPE => DateTime::class,
static::DECORATOR => DateTimeDecorator::class
],
'sku' => [static::TYPE => 'string'],
];
}

/**
* @param string $productId
* @param Context|callable $context
* @return LineItemDraft
*/
public static function ofProductId($productId, $context = null)
{
$draft = static::of($context);
return $draft->setProductId($productId);
}

/**
* @param string $sku
* @param Context|callable $context
* @return LineItemDraft
*/
public static function ofSku($sku, $context = null)
{
$draft = static::of($context);
return $draft->setSku($sku);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Commercetools\Core\Model\Common\DateTimeDecorator;
use Commercetools\Core\Request\AbstractAction;
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
use Commercetools\Core\Request\Carts\Command\CartAddLineItemAction;
use DateTime;

/**
Expand All @@ -26,6 +27,8 @@
* @method ShoppingListAddLineItemAction setCustom(CustomFieldObjectDraft $custom = null)
* @method DateTimeDecorator getAddedAt()
* @method ShoppingListAddLineItemAction setAddedAt(DateTime $addedAt = null)
* @method string getSku()
* @method ShoppingListAddLineItemAction setSku(string $sku = null)
*/
class ShoppingListAddLineItemAction extends AbstractAction
{
Expand All @@ -41,6 +44,7 @@ public function fieldDefinitions()
static::DECORATOR => DateTimeDecorator::class
],
'custom' => [static::TYPE => CustomFieldObjectDraft::class],
'sku' => [static::TYPE => 'string'],
];
}

Expand All @@ -56,6 +60,17 @@ public static function ofProductIdVariantIdAndQuantity($productId, $variantId, $
return static::of($context)->setProductId($productId)->setVariantId($variantId)->setQuantity($quantity);
}

/**
* @param string $sku
* @param Context|callable $context
* @param int $quantity
* @return ShoppingListAddLineItemAction
*/
public static function ofSkuAndQuantity($sku, $quantity, $context = null)
{
return static::of($context)->setSku($sku)->setQuantity($quantity);
}

/**
* @param array $data
* @param Context|callable $context
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/ShoppingList/ShoppingListUpdateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,29 @@ public function testSetDeleteDaysAfterLastModification()
$this->assertInstanceOf(ShoppingList::class, $result);
$this->assertSame($days, $result->getDeleteDaysAfterLastModification());
}

public function testAddLineItemBySku()
{
$product = $this->getProduct();
$variant = $product->getMasterData()->getCurrent()->getMasterVariant();
$draft = $this->getDraft('add-line-item-by-sku');
$draft->setLineItems(LineItemDraftCollection::of()->add(LineItemDraft::ofSku($variant->getSku())));
$shoppingList = $this->createShoppingList($draft);

$this->assertSame(1, $shoppingList->getLineItems()->current()->getQuantity());

$request = ShoppingListUpdateRequest::ofIdAndVersion($shoppingList->getId(), $shoppingList->getVersion())
->addAction(ShoppingListAddLineItemAction::ofSkuAndQuantity(
$variant->getSku(),
1
))
;
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);
$this->deleteRequest->setVersion($result->getVersion());

$this->assertInstanceOf(ShoppingList::class, $result);
$this->assertSame(2, $result->getLineItems()->current()->getQuantity());
$this->assertSame($product->getId(), $result->getLineItems()->current()->getProductId());
}
}

0 comments on commit 7172e1c

Please # to comment.