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

Commit 64f18b8

Browse files
author
Jens Schulze
committed
feat(CartDiscount): support cart discount stacking mode
Closes #354
1 parent 80aa86f commit 64f18b8

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

src/Core/Model/CartDiscount/CartDiscount.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,15 @@
4444
* @method CartDiscount setRequiresDiscountCode(bool $requiresDiscountCode = null)
4545
* @method ReferenceCollection getReferences()
4646
* @method CartDiscount setReferences(ReferenceCollection $references = null)
47+
* @method string getStackingMode()
48+
* @method CartDiscount setStackingMode(string $stackingMode = null)
4749
* @method CartDiscountReference getReference()
4850
*/
4951
class CartDiscount extends Resource
5052
{
53+
const MODE_STACKING = 'Stacking';
54+
const MODE_STOP = 'StopAfterThisDiscount';
55+
5156
public function fieldDefinitions()
5257
{
5358
return [
@@ -77,7 +82,8 @@ public function fieldDefinitions()
7782
static::DECORATOR => DateTimeDecorator::class
7883
],
7984
'requiresDiscountCode' => [static::TYPE => 'bool'],
80-
'references' => [static::TYPE => ReferenceCollection::class]
85+
'references' => [static::TYPE => ReferenceCollection::class],
86+
'stackingMode' => [static::TYPE => 'string'],
8187
];
8288
}
8389
}

src/Core/Model/CartDiscount/CartDiscountDraft.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
* @method CartDiscountDraft setValidUntil(DateTime $validUntil = null)
3535
* @method bool getRequiresDiscountCode()
3636
* @method CartDiscountDraft setRequiresDiscountCode(bool $requiresDiscountCode = null)
37+
* @method string getStackingMode()
38+
* @method CartDiscountDraft setStackingMode(string $stackingMode = null)
3739
*/
3840
class CartDiscountDraft extends JsonObject
3941
{
@@ -47,6 +49,7 @@ class CartDiscountDraft extends JsonObject
4749
const VALID_FROM = 'validFrom';
4850
const VALID_UNTIL = 'validUntil';
4951
const REQUIRES_DISCOUNT_CODE = 'requiresDiscountCode';
52+
const STACKING_MODE = 'stackingMode';
5053

5154
public function fieldDefinitions()
5255
{
@@ -67,6 +70,7 @@ public function fieldDefinitions()
6770
static::DECORATOR => DateTimeDecorator::class
6871
],
6972
static::REQUIRES_DISCOUNT_CODE => [static::TYPE => 'bool'],
73+
static::STACKING_MODE => [static::TYPE => 'string'],
7074
];
7175
}
7276

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\CartDiscounts\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\CartDiscounts\Command
13+
* @link https://dev.commercetools.com/http-api-projects-cartDiscounts.html#change-stacking-mode
14+
* @method string getAction()
15+
* @method CartDiscountChangeStackingModeAction setAction(string $action = null)
16+
* @method CartDiscountChangeStackingModeAction setStackingMode(string $stackingMode = null)
17+
* @method string getStackingMode()
18+
*/
19+
class CartDiscountChangeStackingModeAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'stackingMode' => [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('changeStackingMode');
37+
}
38+
39+
/**
40+
* @param string $stackingMode
41+
* @param Context|callable $context
42+
* @return CartDiscountChangeStackingModeAction
43+
*/
44+
public static function ofStackingMode($stackingMode, $context = null)
45+
{
46+
return static::of($context)->setStackingMode($stackingMode);
47+
}
48+
}

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ cartDiscount:
139139
- validUntil
140140
- requiresDiscountCode
141141
- references
142+
- stackingMode
142143

143144
cartDiscountValue:
144145
domain: cartDiscount

tests/integration/CartDiscount/CartDiscountUpdateRequestTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeRequiresDiscountCodeAction;
2020
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeSortOrderAction;
2121
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeTargetAction;
22+
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeStackingModeAction;
2223
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountChangeValueAction;
2324
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetDescriptionAction;
2425
use Commercetools\Core\Request\CartDiscounts\CartDiscountCreateRequest;
@@ -264,6 +265,40 @@ public function testSetValidUntil()
264265
$this->assertSame($validUntil->format('c'), $result->getValidUntil()->format('c'));
265266
$this->assertNotSame($cartDiscount->getVersion(), $result->getVersion());
266267
}
268+
269+
public function testStackingMode()
270+
{
271+
$draft = $this->getDraft('stacking-mode');
272+
$cartDiscount = $this->createCartDiscount($draft);
273+
274+
$this->assertSame(CartDiscount::MODE_STACKING, $cartDiscount->getStackingMode());
275+
276+
$request = CartDiscountUpdateRequest::ofIdAndVersion(
277+
$cartDiscount->getId(),
278+
$cartDiscount->getVersion()
279+
)
280+
->addAction(CartDiscountChangeStackingModeAction::ofStackingMode(CartDiscount::MODE_STOP))
281+
;
282+
$response = $request->executeWithClient($this->getClient());
283+
$result = $request->mapResponse($response);
284+
$this->deleteRequest->setVersion($result->getVersion());
285+
286+
$this->assertSame(CartDiscount::MODE_STOP, $result->getStackingMode());
287+
$cartDiscount = $result;
288+
289+
$request = CartDiscountUpdateRequest::ofIdAndVersion(
290+
$cartDiscount->getId(),
291+
$cartDiscount->getVersion()
292+
)
293+
->addAction(CartDiscountChangeStackingModeAction::ofStackingMode(CartDiscount::MODE_STACKING))
294+
;
295+
$response = $request->executeWithClient($this->getClient());
296+
$result = $request->mapResponse($response);
297+
$this->deleteRequest->setVersion($result->getVersion());
298+
299+
$this->assertSame(CartDiscount::MODE_STACKING, $result->getStackingMode());
300+
}
301+
267302
/**
268303
* @param $name
269304
* @return CartDiscountDraft

tests/integration/DiscountCode/DiscountCodeUpdateRequestTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function createCartDiscount($draft)
6666
$response = $request->executeWithClient($this->getClient());
6767
$cartDiscount = $request->mapResponse($response);
6868

69-
$this->discountDeleteRequests[] = $this->deleteRequest = CartDiscountDeleteRequest::ofIdAndVersion(
69+
$this->discountDeleteRequests[] = CartDiscountDeleteRequest::ofIdAndVersion(
7070
$cartDiscount->getId(),
7171
$cartDiscount->getVersion()
7272
);
@@ -116,7 +116,7 @@ protected function createDiscountCode(DiscountCodeDraft $draft)
116116
$request = DiscountCodeCreateRequest::ofDraft($draft);
117117
$response = $request->executeWithClient($this->getClient());
118118
$discountCode = $request->mapResponse($response);
119-
$this->cleanupRequests[] = DiscountCodeDeleteRequest::ofIdAndVersion(
119+
$this->cleanupRequests[] = $this->deleteRequest = DiscountCodeDeleteRequest::ofIdAndVersion(
120120
$discountCode->getId(),
121121
$discountCode->getVersion()
122122
);
@@ -130,6 +130,7 @@ public function testCustomTypeCreate()
130130
$draft = $this->getDraft('discount-type');
131131
$draft->setCustom(CustomFieldObjectDraft::ofTypeKey($type->getKey()));
132132
$discountCode = $this->createDiscountCode($draft);
133+
$this->deleteRequest->setVersion($discountCode->getVersion());
133134

134135
$this->assertSame($type->getId(), $discountCode->getCustom()->getType()->getId());
135136
}

0 commit comments

Comments
 (0)