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

Commit 47c89b3

Browse files
author
Jens Schulze
committed
feat(Cart): support automatic deletion of old carts
Closes #294
1 parent 33bb512 commit 47c89b3

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/Model/Cart/Cart.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
* @method string getLocale()
6666
* @method string getTaxRoundingMode()
6767
* @method Cart setTaxRoundingMode(string $taxRoundingMode = null)
68+
* @method int getDeleteDaysAfterLastModification()
69+
* @method Cart setDeleteDaysAfterLastModification(int $deleteDaysAfterLastModification = null)
6870
* @method CartReference getReference()
6971
*/
7072
class Cart extends Resource
@@ -111,6 +113,7 @@ public function fieldDefinitions()
111113
'anonymousId' => [static::TYPE => 'string'],
112114
'locale' => [static::TYPE => 'string'],
113115
'taxRoundingMode' => [static::TYPE => 'string'],
116+
'deleteDaysAfterLastModification' => [static::TYPE => 'int']
114117
];
115118
}
116119

src/Model/Cart/CartDraft.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* @method string getLocale()
4545
* @method string getTaxRoundingMode()
4646
* @method CartDraft setTaxRoundingMode(string $taxRoundingMode = null)
47+
* @method int getDeleteDaysAfterLastModification()
48+
* @method CartDraft setDeleteDaysAfterLastModification(int $deleteDaysAfterLastModification = null)
4749
*/
4850
class CartDraft extends JsonObject
4951
{
@@ -67,6 +69,7 @@ public function fieldDefinitions()
6769
'anonymousId' => [static::TYPE => 'string'],
6870
'locale' => [static::TYPE => 'string'],
6971
'taxRoundingMode' => [static::TYPE => 'string'],
72+
'deleteDaysAfterLastModification' => [static::TYPE => 'int']
7073
];
7174
}
7275

src/Model/Cart/MyCartDraft.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ public function fieldDefinitions()
5252
'shippingMethod' => [static::TYPE => '\Commercetools\Core\Model\ShippingMethod\ShippingMethodReference'],
5353
'custom' => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObjectDraft'],
5454
'locale' => [static::TYPE => 'string'],
55+
'deleteDaysAfterLastModification' => [static::TYPE => 'int']
5556
];
5657
}
5758

5859
/**
5960
* @param string $currency
6061
* @param Context|callable $context
61-
* @return CartDraft
62+
* @return MyCartDraft
6263
*/
6364
public static function ofCurrency($currency, $context = null)
6465
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\Carts\Command
13+
* @link http://dev.commercetools.com/http-api-projects-carts.html#set-deletedaysafterlastmodification-beta
14+
* @method string getAction()
15+
* @method CartSetDeleteDaysAfterLastModificationAction setAction(string $action = null)
16+
* @method int getDeleteDaysAfterLastModification()
17+
* @codingStandardsIgnoreStart
18+
* @method CartSetDeleteDaysAfterLastModificationAction setDeleteDaysAfterLastModification(int $deleteDaysAfterLastModification = null)
19+
* @codingStandardsIgnoreEnd
20+
*/
21+
class CartSetDeleteDaysAfterLastModificationAction extends AbstractAction
22+
{
23+
public function fieldDefinitions()
24+
{
25+
return [
26+
'action' => [static::TYPE => 'string'],
27+
'deleteDaysAfterLastModification' => [static::TYPE => 'int'],
28+
];
29+
}
30+
31+
/**
32+
* @param array $data
33+
* @param Context|callable $context
34+
*/
35+
public function __construct(array $data = [], $context = null)
36+
{
37+
parent::__construct($data, $context);
38+
$this->setAction('setDeleteDaysAfterLastModification');
39+
}
40+
41+
/**
42+
* @param int $days
43+
* @param Context|callable $context
44+
* @return CartSetDeleteDaysAfterLastModificationAction
45+
*/
46+
public static function ofDays($days, $context = null)
47+
{
48+
return static::of($context)->setDeleteDaysAfterLastModification($days);
49+
}
50+
}

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cart:
2626
- anonymousId
2727
- locale
2828
- taxRoundingMode
29+
- deleteDaysAfterLastModification
2930
lineItem:
3031
domain: cart
3132
model: LineItem

tests/integration/Cart/CartUpdateRequestTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomFieldAction;
5454
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomTypeAction;
5555
use Commercetools\Core\Request\Carts\Command\CartSetCustomShippingMethodAction;
56+
use Commercetools\Core\Request\Carts\Command\CartSetDeleteDaysAfterLastModificationAction;
5657
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomFieldAction;
5758
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomTypeAction;
5859
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTotalPriceAction;
@@ -1141,6 +1142,25 @@ public function testCreateWithTaxRoundingMode()
11411142
$this->assertSame(Cart::TAX_ROUNDING_MODE_HALF_DOWN, $cart->getTaxRoundingMode());
11421143
}
11431144

1145+
public function testAutomaticDelete()
1146+
{
1147+
$draft = $this->getDraft();
1148+
$draft->setDeleteDaysAfterLastModification(1);
1149+
$cart = $this->createCart($draft);
1150+
1151+
$this->assertSame(1, $cart->getDeleteDaysAfterLastModification());
1152+
1153+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1154+
->addAction(CartSetDeleteDaysAfterLastModificationAction::ofDays(2))
1155+
;
1156+
$response = $request->executeWithClient($this->getClient());
1157+
$cart = $request->mapResponse($response);
1158+
1159+
$this->deleteRequest->setVersion($cart->getVersion());
1160+
1161+
$this->assertSame(2, $cart->getDeleteDaysAfterLastModification());
1162+
}
1163+
11441164
/**
11451165
* @return CartDraft
11461166
*/

0 commit comments

Comments
 (0)