Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add originalPrice to product response #502

Merged
merged 3 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,8 @@ definitions:
type: "string"
price:
$ref: "#/definitions/Price"
originalPrice:
$ref: "#/definitions/Price"
images:
type: "array"
items:
Expand Down
64 changes: 64 additions & 0 deletions spec/Factory/Product/ProductVariantViewFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function it_builds_product_variant_view(
$channel->getBaseCurrency()->willReturn($currency);
$currency->getCode()->willReturn('PLN');
$channelPrice->getPrice()->willReturn(500);
$channelPrice->getOriginalPrice()->willReturn(null);
alexander-schranz marked this conversation as resolved.
Show resolved Hide resolved

$variantView->code = 'SMALL_RED_LOGAN_HAT_CODE';
$variantView->name = 'Small red Logan hat code';
Expand All @@ -93,6 +94,69 @@ function it_builds_product_variant_view(
$this->create($variant, $channel, 'en_GB')->shouldBeLike($variantView);
}

function it_builds_product_variant_view_with_original_price(
PriceViewFactoryInterface $priceViewFactory,
ChannelInterface $channel,
CurrencyInterface $currency,
ChannelPricingInterface $channelPrice,
ProductOptionInterface $firstOption,
ProductOptionInterface $secondOption,
ProductOptionTranslationInterface $firstOptionTranslation,
ProductOptionTranslationInterface $secondOptionTranslation,
ProductOptionValueInterface $firstOptionValue,
ProductOptionValueInterface $secondOptionValue,
ProductOptionValueTranslationInterface $firstOptionValueTranslation,
ProductOptionValueTranslationInterface $secondOptionValueTranslation,
ProductVariantInterface $variant,
ProductVariantTranslationInterface $productVariantTranslation
): void {
$variantView = new ProductVariantView();

$variant->getCode()->willReturn('SMALL_RED_LOGAN_HAT_CODE');
$variant->getTranslation('en_GB')->willReturn($productVariantTranslation);
$variant->getChannelPricingForChannel($channel)->willReturn($channelPrice);
$variant->getOptionValues()->willReturn(new ArrayCollection([
$firstOptionValue->getWrappedObject(),
$secondOptionValue->getWrappedObject(),
]));

$priceViewFactory->create(500, 'PLN')->willReturn(new PriceView());
$priceViewFactory->create(999, 'PLN')->willReturn(new PriceView());

$firstOptionValue->getCode()->willReturn('HAT_SIZE_S');
$firstOptionValue->getTranslation('en_GB')->willReturn($firstOptionValueTranslation);
$firstOptionValue->getOption()->willReturn($firstOption);
$firstOption->getTranslation('en_GB')->willReturn($firstOptionTranslation);
$firstOptionTranslation->getName()->willReturn('Size');
$firstOptionValueTranslation->getValue()->willReturn('S');

$secondOptionValue->getCode()->willReturn('HAT_COLOR_RED');
$secondOptionValue->getTranslation('en_GB')->willReturn($secondOptionValueTranslation);
$secondOptionValue->getOption()->willReturn($secondOption);
$secondOption->getTranslation('en_GB')->willReturn($secondOptionTranslation);
$secondOptionTranslation->getName()->willReturn('Color');
$secondOptionValueTranslation->getValue()->willReturn('Red');

$productVariantTranslation->getName()->willReturn('Small red Logan hat code');

$channel->getBaseCurrency()->willReturn($currency);
$currency->getCode()->willReturn('PLN');
$channelPrice->getPrice()->willReturn(500);
$channelPrice->getOriginalPrice()->willReturn(999);

$variantView->code = 'SMALL_RED_LOGAN_HAT_CODE';
$variantView->name = 'Small red Logan hat code';
$variantView->price = new PriceView();
$variantView->originalPrice = new PriceView();
$variantView->axis = ['HAT_SIZE_S', 'HAT_COLOR_RED'];
$variantView->nameAxis = [
'HAT_SIZE_S' => 'Size S',
'HAT_COLOR_RED' => 'Color Red',
];

$this->create($variant, $channel, 'en_GB')->shouldBeLike($variantView);
}

function it_throws_an_exception_if_there_is_no_price(
ChannelInterface $channel,
CurrencyInterface $currency,
Expand Down
8 changes: 8 additions & 0 deletions src/Factory/Product/ProductVariantViewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public function create(ProductVariantInterface $variant, ChannelInterface $chann
$channel->getBaseCurrency()->getCode()
);

$originalPrice = $channelPricing->getOriginalPrice();
if (null !== $originalPrice) {
mamazu marked this conversation as resolved.
Show resolved Hide resolved
$variantView->originalPrice = $this->priceViewFactory->create(
$originalPrice,
$channel->getBaseCurrency()->getCode()
);
}

foreach ($variant->getOptionValues() as $optionValue) {
$variantView->axis[] = $optionValue->getCode();
$variantView->nameAxis[$optionValue->getCode()] = sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Sylius\ShopApiPlugin\View\Product\ProductVariantView:
price:
expose: true
type: Sylius\ShopApiPlugin\View\PriceView
originalPrice:
expose: true
type: Sylius\ShopApiPlugin\View\PriceView
images:
expose: true
type: array
3 changes: 3 additions & 0 deletions src/View/Product/ProductVariantView.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class ProductVariantView
/** @var PriceView */
public $price;

/** @var PriceView|null */
public $originalPrice;

/** @var ImageView[] */
public $images = [];

Expand Down
1 change: 1 addition & 0 deletions tests/DataFixtures/ORM/shop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Sylius\Component\Core\Model\ChannelPricing:
gb_mug_web_channel_pricing:
channelCode: "WEB_GB"
price: 1999
originalPrice: 2999
gb_small_t_shirt_web_channel_pricing:
channelCode: "WEB_GB"
price: 1999
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"code": 401,
"message": "Bad credentials"
"message": "Bad credentials."
}
4 changes: 4 additions & 0 deletions tests/Responses/Expected/order/order_details_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any possibility to add original price in a different currency than the current one? What would you say, about merging these originalPrice and price fields?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both should be used with the same structure. The price is renderd using a Priceview so should the original price be otherwise it would make things more difficult when consuming this API.

},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
4 changes: 4 additions & 0 deletions tests/Responses/Expected/order/orders_list_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": []
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": [
{
"code": "thumbnail",
Expand Down Expand Up @@ -171,6 +175,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": [
{
"code": "thumbnail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": [
{
"code": "thumbnail",
Expand Down Expand Up @@ -171,6 +175,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": [
{
"code": "thumbnail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": [
{
"code": "thumbnail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"current": 1999,
"currency": "GBP"
},
"originalPrice": {
"current": 2999,
"currency": "GBP"
},
"images": [
{
"code": "thumbnail",
Expand Down
Loading