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

Commit 756811d

Browse files
committed
feat(Order): support setCustomerId
1 parent 75f654d commit 756811d

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

src/Core/Builder/Update/OrdersActionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Commercetools\Core\Request\Orders\Command\OrderSetCustomLineItemShippingDetailsAction;
2626
use Commercetools\Core\Request\Orders\Command\OrderSetCustomTypeAction;
2727
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerEmail;
28+
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerIdAction;
2829
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryAddressAction;
2930
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryItemsAction;
3031
use Commercetools\Core\Request\Orders\Command\OrderSetLineItemCustomFieldAction;
@@ -279,6 +280,17 @@ public function setCustomerEmail($action = null)
279280
return $this;
280281
}
281282

283+
/**
284+
* @link https://docs.commercetools.com/http-api-projects-orders.html#set-customer-id
285+
* @param OrderSetCustomerIdAction|callable $action
286+
* @return $this
287+
*/
288+
public function setCustomerId($action = null)
289+
{
290+
$this->addAction($this->resolveAction(OrderSetCustomerIdAction::class, $action));
291+
return $this;
292+
}
293+
282294
/**
283295
* @link https://docs.commercetools.com/http-api-projects-orders.html#set-delivery-address
284296
* @param OrderSetDeliveryAddressAction|callable $action
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Model\Message;
6+
7+
use Commercetools\Core\Model\Customer\CustomerReference;
8+
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
9+
use Commercetools\Core\Model\Common\DateTimeDecorator;
10+
use DateTime;
11+
use Commercetools\Core\Model\Common\Reference;
12+
13+
/**
14+
* @package Commercetools\Core\Model\Message
15+
* @link https://docs.commercetools.com/http-api-projects-messages.html#ordercustomerset-message
16+
*
17+
* @method string getId()
18+
* @method OrderCustomerSetMessage setId(string $id = null)
19+
* @method int getVersion()
20+
* @method OrderCustomerSetMessage setVersion(int $version = null)
21+
* @method DateTimeDecorator getCreatedAt()
22+
* @method OrderCustomerSetMessage setCreatedAt(DateTime $createdAt = null)
23+
* @method DateTimeDecorator getLastModifiedAt()
24+
* @method OrderCustomerSetMessage setLastModifiedAt(DateTime $lastModifiedAt = null)
25+
* @method int getSequenceNumber()
26+
* @method OrderCustomerSetMessage setSequenceNumber(int $sequenceNumber = null)
27+
* @method Reference getResource()
28+
* @method OrderCustomerSetMessage setResource(Reference $resource = null)
29+
* @method int getResourceVersion()
30+
* @method OrderCustomerSetMessage setResourceVersion(int $resourceVersion = null)
31+
* @method string getType()
32+
* @method OrderCustomerSetMessage setType(string $type = null)
33+
* @method CustomerReference getCustomer()
34+
* @method OrderCustomerSetMessage setCustomer(CustomerReference $customer = null)
35+
* @method CustomerGroupReference getCustomerGroup()
36+
* @method OrderCustomerSetMessage setCustomerGroup(CustomerGroupReference $customerGroup = null)
37+
* @method CustomerReference getOldCustomer()
38+
* @method OrderCustomerSetMessage setOldCustomer(CustomerReference $oldCustomer = null)
39+
* @method CustomerGroupReference getOldCustomerGroup()
40+
* @method OrderCustomerSetMessage setOldCustomerGroup(CustomerGroupReference $oldCustomerGroup = null)
41+
*/
42+
class OrderCustomerSetMessage extends Message
43+
{
44+
const MESSAGE_TYPE = 'OrderCustomerSet';
45+
46+
public function fieldDefinitions()
47+
{
48+
$definitions = parent::fieldDefinitions();
49+
$definitions['customer'] = [static::TYPE => CustomerReference::class];
50+
$definitions['customerGroup'] = [static::TYPE => CustomerGroupReference::class];
51+
$definitions['oldCustomer'] = [static::TYPE => CustomerReference::class];
52+
$definitions['oldCustomerGroup'] = [static::TYPE => CustomerGroupReference::class];
53+
54+
return $definitions;
55+
}
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\Orders\Command;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Request\AbstractAction;
9+
10+
/**
11+
* @package Commercetools\Core\Request\Orders\Command
12+
*
13+
* @link https://docs.commercetools.com/http-api-projects-orders.html#set-customer-id
14+
* @method string getAction()
15+
* @method OrderSetCustomerIdAction setAction(string $action = null)
16+
* @method string getCustomerId()
17+
* @method OrderSetCustomerIdAction setCustomerId(string $customerId = null)
18+
*/
19+
class OrderSetCustomerIdAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'customerId' => [static::TYPE => 'string'],
26+
];
27+
}
28+
29+
/**
30+
* @param array $data
31+
* @param Context|callable $context
32+
*/
33+
public function __construct(array $data = [], $context = null)
34+
{
35+
parent::__construct($data, $context);
36+
$this->setAction('setCustomerId');
37+
}
38+
}

tests/integration/Order/OrderUpdateRequestTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Commercetools\Core\Model\Common\PriceDraft;
2323
use Commercetools\Core\Model\Common\PriceDraftCollection;
2424
use Commercetools\Core\Model\Customer\Customer;
25+
use Commercetools\Core\Model\Customer\CustomerDraft;
26+
use Commercetools\Core\Model\Customer\CustomerSigninResult;
2527
use Commercetools\Core\Model\Order\DeliveryItem;
2628
use Commercetools\Core\Model\Order\DeliveryItemCollection;
2729
use Commercetools\Core\Model\Order\Order;
@@ -43,6 +45,8 @@
4345
use Commercetools\Core\Request\Carts\CartCreateRequest;
4446
use Commercetools\Core\Request\Carts\CartDeleteRequest;
4547
use Commercetools\Core\Request\Carts\CartReplicateRequest;
48+
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
49+
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
4650
use Commercetools\Core\Request\Orders\Command\OrderAddDeliveryAction;
4751
use Commercetools\Core\Request\Orders\Command\OrderAddItemShippingAddressAction;
4852
use Commercetools\Core\Request\Orders\Command\OrderAddParcelToDeliveryAction;
@@ -57,6 +61,7 @@
5761
use Commercetools\Core\Request\Orders\Command\OrderRemovePaymentAction;
5862
use Commercetools\Core\Request\Orders\Command\OrderSetBillingAddress;
5963
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerEmail;
64+
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerIdAction;
6065
use Commercetools\Core\Request\Orders\Command\OrderSetCustomLineItemShippingDetailsAction;
6166
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryAddressAction;
6267
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryItemsAction;
@@ -669,6 +674,42 @@ public function testSetCustomerEmail()
669674
$this->assertNotSame($draft->getCustomerEmail(), $order->getCustomerEmail());
670675
}
671676

677+
public function testSetCustomerId()
678+
{
679+
$draft = $this->getCartDraft();
680+
$order = $this->createOrder($draft);
681+
682+
$customerDraft = CustomerDraft::ofEmailNameAndPassword(
683+
$this->getTestRun() . '-another@example.com',
684+
'firstName',
685+
'lastName',
686+
'password'
687+
);
688+
$request = CustomerCreateRequest::ofDraft($customerDraft);
689+
$response = $request->executeWithClient($this->getClient());
690+
$customerSignInResult = $request->mapResponse($response);
691+
$customer = $customerSignInResult->getCustomer();
692+
693+
$this->cleanupRequests[] = CustomerDeleteRequest::ofIdAndVersion(
694+
$customer->getId(),
695+
$customer->getVersion()
696+
);
697+
698+
$this->assertNotSame($order->getCustomerId(), $customer->getId());
699+
700+
$request = OrderUpdateRequest::ofIdAndVersion($order->getId(), $order->getVersion())
701+
->addAction(OrderSetCustomerIdAction::of()->setCustomerId($customer->getId()))
702+
;
703+
704+
$response = $request->executeWithClient($this->getClient());
705+
$order = $request->mapResponse($response);
706+
$this->deleteRequest->setVersion($order->getVersion());
707+
708+
$this->assertInstanceOf(Order::class, $order);
709+
$this->assertNotSame($draft->getCustomerId(), $order->getCustomerId());
710+
$this->assertSame($customer->getId(), $order->getCustomerId());
711+
}
712+
672713
public function testSetShippingAddress()
673714
{
674715
$draft = $this->getCartDraft();

0 commit comments

Comments
 (0)