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

Commit 80aa86f

Browse files
author
Jens Schulze
committed
feat(DiscountCode): support custom fields for discount codes
Closes #352
1 parent 6f35c31 commit 80aa86f

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

src/Core/Model/CustomField/CustomFieldObjectDraft.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,8 @@
1818
* @method TypeReference getType()
1919
* @method CustomFieldObjectDraft setType(TypeReference $type = null)
2020
*/
21-
class CustomFieldObjectDraft extends JsonObject
21+
class CustomFieldObjectDraft extends CustomFieldObject
2222
{
23-
public function fieldDefinitions()
24-
{
25-
return [
26-
'type' => [static::TYPE => TypeReference::class],
27-
'fields' => [static::TYPE => FieldContainer::class]
28-
];
29-
}
30-
3123
/**
3224
* @param $typeKey
3325
* @param Context|callable $context

src/Core/Model/DiscountCode/DiscountCode.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Commercetools\Core\Model\Common\LocalizedString;
1111
use Commercetools\Core\Model\Common\ReferenceCollection;
1212
use Commercetools\Core\Model\Common\DateTimeDecorator;
13+
use Commercetools\Core\Model\CustomField\CustomFieldObject;
1314
use DateTime;
1415

1516
/**
@@ -41,6 +42,8 @@
4142
* @method DiscountCode setMaxApplications(int $maxApplications = null)
4243
* @method int getMaxApplicationsPerCustomer()
4344
* @method DiscountCode setMaxApplicationsPerCustomer(int $maxApplicationsPerCustomer = null)
45+
* @method CustomFieldObject getCustom()
46+
* @method DiscountCode setCustom(CustomFieldObject $custom = null)
4447
* @method DiscountCodeReference getReference()
4548
*/
4649
class DiscountCode extends Resource
@@ -69,6 +72,7 @@ public function fieldDefinitions()
6972
'references' => [static::TYPE => ReferenceCollection::class],
7073
'maxApplications' => [static::TYPE => 'int'],
7174
'maxApplicationsPerCustomer' => [static::TYPE => 'int'],
75+
'custom' => [static::TYPE => CustomFieldObject::class],
7276
];
7377
}
7478
}

src/Core/Model/DiscountCode/DiscountCodeDraft.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Commercetools\Core\Model\Common\Context;
1010
use Commercetools\Core\Model\Common\JsonObject;
1111
use Commercetools\Core\Model\Common\LocalizedString;
12+
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
1213

1314
/**
1415
* @package Commercetools\Core\Model\DiscountCode
@@ -29,6 +30,8 @@
2930
* @method DiscountCodeDraft setMaxApplications(int $maxApplications = null)
3031
* @method int getMaxApplicationsPerCustomer()
3132
* @method DiscountCodeDraft setMaxApplicationsPerCustomer(int $maxApplicationsPerCustomer = null)
33+
* @method CustomFieldObjectDraft getCustom()
34+
* @method DiscountCodeDraft setCustom(CustomFieldObjectDraft $custom = null)
3235
*/
3336
class DiscountCodeDraft extends JsonObject
3437
{
@@ -45,6 +48,7 @@ public function fieldDefinitions()
4548
'isActive' => [static::TYPE => 'bool'],
4649
'maxApplications' => [static::TYPE => 'int'],
4750
'maxApplicationsPerCustomer' => [static::TYPE => 'int'],
51+
'custom' => [static::TYPE => CustomFieldObjectDraft::class],
4852
];
4953
}
5054

tests/fixtures/models.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ discountCode:
726726
- references
727727
- maxApplications
728728
- maxApplicationsPerCustomer
729-
729+
- custom
730730
productDiscount:
731731
domain: productDiscount
732732
model: ProductDiscount

tests/integration/DiscountCode/DiscountCodeUpdateRequestTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
use Commercetools\Core\Model\CartDiscount\CartDiscountValue;
1414
use Commercetools\Core\Model\Common\Money;
1515
use Commercetools\Core\Model\Common\MoneyCollection;
16+
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
1617
use Commercetools\Core\Model\DiscountCode\DiscountCode;
1718
use Commercetools\Core\Model\DiscountCode\DiscountCodeDraft;
1819
use Commercetools\Core\Model\Common\LocalizedString;
20+
use Commercetools\Core\Model\Type\Type;
1921
use Commercetools\Core\Request\CartDiscounts\CartDiscountCreateRequest;
2022
use Commercetools\Core\Request\CartDiscounts\CartDiscountDeleteRequest;
23+
use Commercetools\Core\Request\CustomField\Command\SetCustomFieldAction;
24+
use Commercetools\Core\Request\CustomField\Command\SetCustomTypeAction;
2125
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeCartDiscountsAction;
2226
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeIsActiveAction;
2327
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetCartPredicateAction;
@@ -62,7 +66,7 @@ protected function createCartDiscount($draft)
6266
$response = $request->executeWithClient($this->getClient());
6367
$cartDiscount = $request->mapResponse($response);
6468

65-
$this->discountDeleteRequests[] = CartDiscountDeleteRequest::ofIdAndVersion(
69+
$this->discountDeleteRequests[] = $this->deleteRequest = CartDiscountDeleteRequest::ofIdAndVersion(
6670
$cartDiscount->getId(),
6771
$cartDiscount->getVersion()
6872
);
@@ -120,6 +124,51 @@ protected function createDiscountCode(DiscountCodeDraft $draft)
120124
return $discountCode;
121125
}
122126

127+
public function testCustomTypeCreate()
128+
{
129+
$type = $this->getType($this->getTestRun() . '-discount-type', 'discount-code');
130+
$draft = $this->getDraft('discount-type');
131+
$draft->setCustom(CustomFieldObjectDraft::ofTypeKey($type->getKey()));
132+
$discountCode = $this->createDiscountCode($draft);
133+
134+
$this->assertSame($type->getId(), $discountCode->getCustom()->getType()->getId());
135+
}
136+
137+
public function testCustomTypeUpdate()
138+
{
139+
$type = $this->getType($this->getTestRun() . '-discount-type', 'discount-code');
140+
$draft = $this->getDraft('discount-type');
141+
$discountCode = $this->createDiscountCode($draft);
142+
143+
$request = DiscountCodeUpdateRequest::ofIdAndVersion($discountCode->getId(), $discountCode->getVersion())
144+
->addAction(SetCustomTypeAction::ofTypeKey($type->getKey()))
145+
;
146+
$response = $request->executeWithClient($this->getClient());
147+
$result = $request->mapResponse($response);
148+
$this->deleteRequest->setVersion($result->getVersion());
149+
150+
$this->assertSame($type->getId(), $result->getCustom()->getType()->getId());
151+
}
152+
153+
public function testCustomFieldUpdate()
154+
{
155+
$type = $this->getType($this->getTestRun() . '-discount-type', 'discount-code');
156+
$draft = $this->getDraft('discount-type');
157+
$draft->setCustom(CustomFieldObjectDraft::ofTypeKey($type->getKey()));
158+
$discountCode = $this->createDiscountCode($draft);
159+
160+
$newValue = $this->getTestRun() . '-value';
161+
$request = DiscountCodeUpdateRequest::ofIdAndVersion($discountCode->getId(), $discountCode->getVersion())
162+
->addAction(SetCustomFieldAction::ofName('testField')->setValue($newValue))
163+
;
164+
$response = $request->executeWithClient($this->getClient());
165+
$result = $request->mapResponse($response);
166+
$this->deleteRequest->setVersion($result->getVersion());
167+
168+
$this->assertSame($newValue, $result->getCustom()->getFields()->getTestField());
169+
}
170+
171+
123172
public function testChangeCartDiscountAction()
124173
{
125174
$draft = $this->getDraft('change-cart-discount');

0 commit comments

Comments
 (0)