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

Commit cfbc0bb

Browse files
author
Jens Schulze
committed
feat(CustomFields): add customs fields and types for prices
BREAKING CHANGE: added PriceDraft to price update actions The new PriceDraft object has been added as type hint to ProductAddPriceAction and ProductChangePriceAction. The ProductVariantDraft expects now a PriceDraftCollection Closes #156
1 parent d81dc56 commit cfbc0bb

File tree

10 files changed

+215
-18
lines changed

10 files changed

+215
-18
lines changed

src/Model/Common/Price.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Commercetools\Core\Model\Channel\ChannelReference;
1010
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
11+
use Commercetools\Core\Model\CustomField\CustomFieldObject;
1112

1213
/**
1314
* @package Commercetools\Core\Model\Common
@@ -28,6 +29,8 @@
2829
* @method Price setValidFrom(\DateTime $validFrom = null)
2930
* @method DateTimeDecorator getValidUntil()
3031
* @method Price setValidUntil(\DateTime $validUntil = null)
32+
* @method CustomFieldObject getCustom()
33+
* @method Price setCustom(CustomFieldObject $custom = null)
3134
*/
3235
class Price extends JsonObject
3336
{
@@ -39,6 +42,7 @@ class Price extends JsonObject
3942
const VALID_FROM = 'validFrom';
4043
const VALID_UNTIL = 'validUntil';
4144
const DISCOUNTED = 'discounted';
45+
const CUSTOM = 'custom';
4246

4347
public function fieldDefinitions()
4448
{
@@ -57,6 +61,7 @@ public function fieldDefinitions()
5761
self::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
5862
],
5963
static::DISCOUNTED => [self::TYPE => '\Commercetools\Core\Model\Common\DiscountedPrice'],
64+
static::CUSTOM => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
6065
];
6166
}
6267

src/Model/Common/PriceDraft.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* @author @ct-jensschulze <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Common;
7+
8+
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
9+
use Commercetools\Core\Model\Channel\ChannelReference;
10+
use Commercetools\Core\Model\CustomField\CustomFieldObject;
11+
12+
/**
13+
* @package Commercetools\Core\Model\Common
14+
*
15+
* @method Money getValue()
16+
* @method PriceDraft setValue(Money $value = null)
17+
* @method string getCountry()
18+
* @method PriceDraft setCountry(string $country = null)
19+
* @method CustomerGroupReference getCustomerGroup()
20+
* @method PriceDraft setCustomerGroup(CustomerGroupReference $customerGroup = null)
21+
* @method ChannelReference getChannel()
22+
* @method PriceDraft setChannel(ChannelReference $channel = null)
23+
* @method DateTimeDecorator getValidFrom()
24+
* @method PriceDraft setValidFrom(\DateTime $validFrom = null)
25+
* @method DateTimeDecorator getValidUntil()
26+
* @method PriceDraft setValidUntil(\DateTime $validUntil = null)
27+
* @method CustomFieldObject getCustom()
28+
* @method PriceDraft setCustom(CustomFieldObject $custom = null)
29+
*/
30+
class PriceDraft extends JsonObject
31+
{
32+
const VALUE = 'value';
33+
const COUNTRY = 'country';
34+
const CUSTOMER_GROUP = 'customerGroup';
35+
const CHANNEL = 'channel';
36+
const VALID_FROM = 'validFrom';
37+
const VALID_UNTIL = 'validUntil';
38+
const DISCOUNTED = 'discounted';
39+
const CUSTOM = 'custom';
40+
41+
public function fieldDefinitions()
42+
{
43+
return [
44+
static::VALUE => [self::TYPE => '\Commercetools\Core\Model\Common\Money'],
45+
static::COUNTRY => [self::TYPE => 'string'],
46+
static::CUSTOMER_GROUP => [self::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'],
47+
static::CHANNEL => [self::TYPE => '\Commercetools\Core\Model\Channel\ChannelReference'],
48+
static::VALID_FROM => [
49+
self::TYPE => '\DateTime',
50+
self::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
51+
],
52+
static::VALID_UNTIL => [
53+
self::TYPE => '\DateTime',
54+
self::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
55+
],
56+
static::CUSTOM => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
57+
];
58+
}
59+
60+
/**
61+
* @param Money $money
62+
* @param Context|callable $context
63+
* @return Price
64+
*/
65+
public static function ofMoney(Money $money, $context = null)
66+
{
67+
$price = static::of($context);
68+
return $price->setValue($money);
69+
}
70+
71+
public function __toString()
72+
{
73+
return $this->getValue()->__toString();
74+
}
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* @author @ct-jensschulze <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Common;
7+
8+
/**
9+
* @package Commercetools\Core\Model\Common
10+
*
11+
* @method PriceDraft getAt($offset)
12+
* @method PriceDraftCollection add(PriceDraft $element)
13+
* @method PriceDraft current()
14+
*/
15+
class PriceDraftCollection extends Collection
16+
{
17+
protected $type = '\Commercetools\Core\Model\Common\PriceDraft';
18+
}

src/Model/Product/ProductVariantDraft.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
use Commercetools\Core\Model\Common\AttributeCollection;
1010
use Commercetools\Core\Model\Common\JsonObject;
11-
use Commercetools\Core\Model\Common\PriceCollection;
11+
use Commercetools\Core\Model\Common\PriceDraftCollection;
1212
use Commercetools\Core\Model\Common\ImageCollection;
1313

1414
/**
1515
* @package Commercetools\Core\Model\Product
1616
* @apidoc http://dev.sphere.io/http-api-projects-products.html#new-product-variant
1717
* @method string getSku()
1818
* @method ProductVariantDraft setSku(string $sku = null)
19-
* @method ProductVariantDraft setPrices(PriceCollection $prices = null)
19+
* @method ProductVariantDraft setPrices(PriceDraftCollection $prices = null)
2020
* @method ProductVariantDraft setAttributes(AttributeCollection $attributes = null)
21-
* @method PriceCollection getPrices()
21+
* @method PriceDraftCollection getPrices()
2222
* @method AttributeCollection getAttributes()
2323
* @method ImageCollection getImages()
2424
* @method ProductVariantDraft setImages(ImageCollection $images = null)
@@ -29,7 +29,7 @@ public function fieldDefinitions()
2929
{
3030
return [
3131
'sku' => [self::TYPE => 'string'],
32-
'prices' => [self::TYPE => '\Commercetools\Core\Model\Common\PriceCollection'],
32+
'prices' => [self::TYPE => '\Commercetools\Core\Model\Common\PriceDraftCollection'],
3333
'images' => [static::TYPE => '\Commercetools\Core\Model\Common\ImageCollection'],
3434
'attributes' => [self::TYPE => '\Commercetools\Core\Model\Common\AttributeCollection'],
3535
];

src/Request/Products/Command/ProductAddPriceAction.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Commercetools\Core\Request\Products\Command;
77

88
use Commercetools\Core\Model\Common\Context;
9-
use Commercetools\Core\Model\Common\Price;
9+
use Commercetools\Core\Model\Common\PriceDraft;
1010
use Commercetools\Core\Request\AbstractAction;
1111

1212
/**
@@ -16,8 +16,8 @@
1616
* @method ProductAddPriceAction setAction(string $action = null)
1717
* @method int getVariantId()
1818
* @method ProductAddPriceAction setVariantId(int $variantId = null)
19-
* @method Price getPrice()
20-
* @method ProductAddPriceAction setPrice(Price $price = null)
19+
* @method PriceDraft getPrice()
20+
* @method ProductAddPriceAction setPrice(PriceDraft $price = null)
2121
* @method bool getStaged()
2222
* @method ProductAddPriceAction setStaged(bool $staged = null)
2323
*/
@@ -28,7 +28,7 @@ public function fieldDefinitions()
2828
return [
2929
'action' => [static::TYPE => 'string'],
3030
'variantId' => [static::TYPE => 'int'],
31-
'price' => [static::TYPE => '\Commercetools\Core\Model\Common\Price'],
31+
'price' => [static::TYPE => '\Commercetools\Core\Model\Common\PriceDraft'],
3232
'staged' => [static::TYPE => 'bool'],
3333
];
3434
}
@@ -45,11 +45,11 @@ public function __construct(array $data = [], $context = null)
4545

4646
/**
4747
* @param int $variantId
48-
* @param Price $price
48+
* @param PriceDraft $price
4949
* @param Context|callable $context
5050
* @return ProductAddPriceAction
5151
*/
52-
public static function ofVariantIdAndPrice($variantId, Price $price, $context = null)
52+
public static function ofVariantIdAndPrice($variantId, PriceDraft $price, $context = null)
5353
{
5454
return static::of($context)->setVariantId($variantId)->setPrice($price);
5555
}

src/Request/Products/Command/ProductChangePriceAction.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
namespace Commercetools\Core\Request\Products\Command;
77

88
use Commercetools\Core\Model\Common\Context;
9-
use Commercetools\Core\Model\Common\Price;
9+
use Commercetools\Core\Model\Common\PriceDraft;
1010
use Commercetools\Core\Request\AbstractAction;
1111

1212
/**
1313
* @package Commercetools\Core\Request\Products\Command
1414
* @apidoc http://dev.sphere.io/http-api-projects-products.html#change-price
1515
* @method string getAction()
1616
* @method ProductChangePriceAction setAction(string $action = null)
17-
* @method Price getPrice()
18-
* @method ProductChangePriceAction setPrice(Price $price = null)
17+
* @method PriceDraft getPrice()
18+
* @method ProductChangePriceAction setPrice(PriceDraft $price = null)
1919
* @method bool getStaged()
2020
* @method ProductChangePriceAction setStaged(bool $staged = null)
2121
* @method int getPriceId()
@@ -28,7 +28,7 @@ public function fieldDefinitions()
2828
return [
2929
'action' => [static::TYPE => 'string'],
3030
'priceId' => [static::TYPE => 'int'],
31-
'price' => [static::TYPE => '\Commercetools\Core\Model\Common\Price'],
31+
'price' => [static::TYPE => '\Commercetools\Core\Model\Common\PriceDraft'],
3232
'staged' => [static::TYPE => 'bool'],
3333
];
3434
}
@@ -45,11 +45,11 @@ public function __construct(array $data = [], $context = null)
4545

4646
/**
4747
* @param int $priceId
48-
* @param Price $price
48+
* @param PriceDraft $price
4949
* @param Context|callable $context
5050
* @return ProductChangePriceAction
5151
*/
52-
public static function ofPriceIdAndPrice($priceId, Price $price, $context = null)
52+
public static function ofPriceIdAndPrice($priceId, PriceDraft $price, $context = null)
5353
{
5454
return static::of($context)->setPriceId($priceId)->setPrice($price);
5555
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* @author @ct-jensschulze <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Products\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\CustomField\Command\SetCustomFieldAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\Products\Command
13+
*
14+
* @method string getAction()
15+
* @method ProductSetPriceCustomFieldAction setAction(string $action = null)
16+
* @method int getPriceId()
17+
* @method ProductSetPriceCustomFieldAction setPriceId(int $priceId = null)
18+
* @method bool getStaged()
19+
* @method ProductSetPriceCustomFieldAction setStaged(bool $staged = null)
20+
* @method string getName()
21+
* @method ProductSetPriceCustomFieldAction setName(string $name = null)
22+
* @method getValue()
23+
* @method ProductSetPriceCustomFieldAction setValue($value = null)
24+
*/
25+
class ProductSetPriceCustomFieldAction extends SetCustomFieldAction
26+
{
27+
public function fieldDefinitions()
28+
{
29+
return [
30+
'action' => [static::TYPE => 'string'],
31+
'priceId' => [static::TYPE => 'int'],
32+
'staged' => [static::TYPE => 'bool'],
33+
'name' => [static::TYPE => 'string'],
34+
'value' => [static::TYPE => null],
35+
];
36+
}
37+
38+
/**
39+
* @param array $data
40+
* @param Context|callable $context
41+
*/
42+
public function __construct(array $data = [], $context = null)
43+
{
44+
parent::__construct($data, $context);
45+
$this->setAction('setProductPriceCustomField');
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* @author @ct-jensschulze <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Products\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\CustomField\Command\SetCustomTypeAction;
10+
use Commercetools\Core\Model\CustomField\FieldContainer;
11+
12+
/**
13+
* @package Commercetools\Core\Request\Products\Command
14+
*
15+
* @method string getAction()
16+
* @method ProductSetPriceCustomTypeAction setAction(string $action = null)
17+
* @method string getTypeId()
18+
* @method ProductSetPriceCustomTypeAction setTypeId(string $typeId = null)
19+
* @method string getTypeKey()
20+
* @method ProductSetPriceCustomTypeAction setTypeKey(string $typeKey = null)
21+
* @method int getPriceId()
22+
* @method ProductSetPriceCustomTypeAction setPriceId(int $priceId = null)
23+
* @method bool getStaged()
24+
* @method ProductSetPriceCustomTypeAction setStaged(bool $staged = null)
25+
* @method FieldContainer getFields()
26+
* @method ProductSetPriceCustomTypeAction setFields(FieldContainer $fields = null)
27+
*/
28+
class ProductSetPriceCustomTypeAction extends SetCustomTypeAction
29+
{
30+
public function fieldDefinitions()
31+
{
32+
return [
33+
'action' => [static::TYPE => 'string'],
34+
'typeId' => [static::TYPE => 'string'],
35+
'typeKey' => [static::TYPE => 'string'],
36+
'priceId' => [static::TYPE => 'int'],
37+
'staged' => [static::TYPE => 'bool'],
38+
'fields' => [static::TYPE => '\Commercetools\Core\Model\CustomField\FieldContainer'],
39+
];
40+
}
41+
42+
/**
43+
* @param array $data
44+
* @param Context|callable $context
45+
*/
46+
public function __construct(array $data = [], $context = null)
47+
{
48+
parent::__construct($data, $context);
49+
$this->setAction('setProductPriceCustomType');
50+
}
51+
}

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ price:
252252
- validFrom
253253
- validUntil
254254
- discounted
255+
- custom
255256

256257
customer:
257258
domain: customer

tests/unit/Request/GenericActionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ public function actionArgumentProvider()
816816
'ofVariantIdAndPrice',
817817
[
818818
10,
819-
$this->getInstance('\Commercetools\Core\Model\Common\Price')
819+
$this->getInstance('\Commercetools\Core\Model\Common\PriceDraft')
820820
]
821821
],
822822
[
@@ -842,7 +842,7 @@ public function actionArgumentProvider()
842842
'ofPriceIdAndPrice',
843843
[
844844
10,
845-
$this->getInstance('\Commercetools\Core\Model\Common\Price')
845+
$this->getInstance('\Commercetools\Core\Model\Common\PriceDraft')
846846
]
847847
],
848848
[

0 commit comments

Comments
 (0)