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

Commit 80c72ec

Browse files
author
Jens Schulze
committed
feat(Cart): support setCustomerGroup update action
Closes #343
1 parent 09057a6 commit 80c72ec

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

src/Core/Model/Cart/CartDraft.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Commercetools\Core\Model\Common\Context;
99
use Commercetools\Core\Model\Common\JsonObject;
1010
use Commercetools\Core\Model\Common\LocaleTrait;
11+
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
1112
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
1213
use Commercetools\Core\Model\Common\Address;
1314
use Commercetools\Core\Model\ShippingMethod\ShippingMethodReference;
@@ -46,6 +47,8 @@
4647
* @method CartDraft setTaxRoundingMode(string $taxRoundingMode = null)
4748
* @method int getDeleteDaysAfterLastModification()
4849
* @method CartDraft setDeleteDaysAfterLastModification(int $deleteDaysAfterLastModification = null)
50+
* @method CustomerGroupReference getCustomerGroup()
51+
* @method CartDraft setCustomerGroup(CustomerGroupReference $customerGroup = null)
4952
*/
5053
class CartDraft extends JsonObject
5154
{
@@ -69,7 +72,8 @@ public function fieldDefinitions()
6972
'anonymousId' => [static::TYPE => 'string'],
7073
'locale' => [static::TYPE => 'string'],
7174
'taxRoundingMode' => [static::TYPE => 'string'],
72-
'deleteDaysAfterLastModification' => [static::TYPE => 'int']
75+
'deleteDaysAfterLastModification' => [static::TYPE => 'int'],
76+
'customerGroup' => [static::TYPE => CustomerGroupReference::class],
7377
];
7478
}
7579

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Carts\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
10+
use Commercetools\Core\Request\AbstractAction;
11+
12+
/**
13+
* @package Commercetools\Core\Request\Carts\Command
14+
*
15+
* @method string getAction()
16+
* @method CartSetCustomerGroupAction setAction(string $action = null)
17+
* @method CustomerGroupReference getCustomerGroup()
18+
* @method CartSetCustomerGroupAction setCustomerGroup(CustomerGroupReference $customerGroup = null)
19+
*/
20+
class CartSetCustomerGroupAction extends AbstractAction
21+
{
22+
public function fieldDefinitions()
23+
{
24+
return [
25+
'action' => [static::TYPE => 'string'],
26+
'customerGroup' => [static::TYPE => CustomerGroupReference::class],
27+
];
28+
}
29+
30+
/**
31+
* @param string $customerGroup
32+
* @param Context|callable $context
33+
* @return CartSetCustomerGroupAction
34+
*/
35+
public static function ofCustomerGroup($customerGroup, $context = null)
36+
{
37+
return static::of($context)->setCustomerGroup($customerGroup);
38+
}
39+
40+
/**
41+
* @param array $data
42+
* @param Context|callable $context
43+
*/
44+
public function __construct(array $data = [], $context = null)
45+
{
46+
parent::__construct($data, $context);
47+
$this->setAction('setCustomerGroup');
48+
}
49+
}

tests/integration/Cart/CartUpdateRequestTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Commercetools\Core\Model\Common\PriceDraft;
2626
use Commercetools\Core\Model\Common\PriceTier;
2727
use Commercetools\Core\Model\Common\PriceTierCollection;
28+
use Commercetools\Core\Model\Common\ResourceIdentifier;
29+
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
2830
use Commercetools\Core\Model\CustomField\CustomFieldObject;
2931
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
3032
use Commercetools\Core\Model\CustomField\FieldContainer;
@@ -52,6 +54,7 @@
5254
use Commercetools\Core\Request\Carts\Command\CartSetBillingAddressAction;
5355
use Commercetools\Core\Request\Carts\Command\CartSetCountryAction;
5456
use Commercetools\Core\Request\Carts\Command\CartSetCustomerEmailAction;
57+
use Commercetools\Core\Request\Carts\Command\CartSetCustomerGroupAction;
5558
use Commercetools\Core\Request\Carts\Command\CartSetCustomerIdAction;
5659
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomFieldAction;
5760
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomTypeAction;
@@ -1282,6 +1285,26 @@ public function testLocale($locale, $expectedLocale)
12821285
$this->assertSame($expectedLocale, $cart->getLocale());
12831286
}
12841287

1288+
public function testCustomerGroup()
1289+
{
1290+
$customerGroup = $this->getCustomerGroup();
1291+
$draft = $this->getDraft();
1292+
$draft->setCustomerGroup($customerGroup->getReference());
1293+
$cart = $this->createCart($draft);
1294+
1295+
$this->assertSame($customerGroup->getId(), $cart->getCustomerGroup()->getId());
1296+
1297+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1298+
->addAction(CartSetCustomerGroupAction::of())
1299+
;
1300+
$response = $request->executeWithClient($this->getClient());
1301+
$cart = $request->mapResponse($response);
1302+
1303+
$this->deleteRequest->setVersion($cart->getVersion());
1304+
1305+
$this->assertNull($cart->getCustomerGroup());
1306+
}
1307+
12851308
public function invalidLocaleProvider()
12861309
{
12871310
return [

0 commit comments

Comments
 (0)