diff --git a/src/Core/Request/Carts/Command/CartSetAnonymousIdAction.php b/src/Core/Request/Carts/Command/CartSetAnonymousIdAction.php new file mode 100644 index 0000000000..19c117005e --- /dev/null +++ b/src/Core/Request/Carts/Command/CartSetAnonymousIdAction.php @@ -0,0 +1,38 @@ + + */ + +namespace Commercetools\Core\Request\Carts\Command; + +use Commercetools\Core\Model\Common\Context; +use Commercetools\Core\Request\AbstractAction; + +/** + * @package Commercetools\Core\Request\Carts\Command + * @link https://dev.commercetools.com/http-api-projects-carts.html#set-anonymous-id + * @method string getAction() + * @method CartSetAnonymousIdAction setAction(string $action = null) + * @method string getAnonymousId() + * @method CartSetAnonymousIdAction setAnonymousId(string $anonymousId = null) + */ +class CartSetAnonymousIdAction extends AbstractAction +{ + public function fieldDefinitions() + { + return [ + 'action' => [static::TYPE => 'string'], + 'anonymousId' => [static::TYPE => 'string'], + ]; + } + + /** + * @param array $data + * @param Context|callable $context + */ + public function __construct(array $data = [], $context = null) + { + parent::__construct($data, $context); + $this->setAction('setAnonymousId'); + } +} diff --git a/tests/integration/Cart/CartUpdateRequestTest.php b/tests/integration/Cart/CartUpdateRequestTest.php index 68ee8c1aab..73aebe4c5b 100644 --- a/tests/integration/Cart/CartUpdateRequestTest.php +++ b/tests/integration/Cart/CartUpdateRequestTest.php @@ -48,6 +48,7 @@ use Commercetools\Core\Request\Carts\Command\CartRemoveDiscountCodeAction; use Commercetools\Core\Request\Carts\Command\CartRemoveLineItemAction; use Commercetools\Core\Request\Carts\Command\CartRemovePaymentAction; +use Commercetools\Core\Request\Carts\Command\CartSetAnonymousIdAction; use Commercetools\Core\Request\Carts\Command\CartSetBillingAddressAction; use Commercetools\Core\Request\Carts\Command\CartSetCountryAction; use Commercetools\Core\Request\Carts\Command\CartSetCustomerEmailAction; @@ -654,6 +655,36 @@ public function testSetCountry() $this->assertSame($country, $cart->getCountry()); } + public function testSetAnonymousId() + { + $anonymousId = uniqid(); + $draft = $this->getDraft(); + $draft->setAnonymousId($anonymousId); + $cart = $this->createCart($draft); + $this->assertSame($anonymousId, $cart->getAnonymousId()); + + $newAnonymousId = uniqid(); + + $request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion()) + ->addAction(CartSetAnonymousIdAction::of()->setAnonymousId($newAnonymousId)) + ; + $response = $request->executeWithClient($this->getClient()); + $cart = $request->mapResponse($response); + $this->deleteRequest->setVersion($cart->getVersion()); + + $this->assertSame($newAnonymousId, $cart->getAnonymousId()); + + $request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion()) + ->addAction(CartSetAnonymousIdAction::of()) + ; + $response = $request->executeWithClient($this->getClient()); + $cart = $request->mapResponse($response); + $this->deleteRequest->setVersion($cart->getVersion()); + + $this->assertNull($cart->getAnonymousId()); + + } + public function testSetShippingMethod() { $draft = $this->getDraft();