-
Notifications
You must be signed in to change notification settings - Fork 9.4k
#26121 - special price & tier price are coming in base currency #28890
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
Merged
magento-engcom-team
merged 12 commits into
magento:2.4-develop
from
pmarjan:graphql-26121
Jul 19, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5c69aa1
magento/magento2#26121: special price & tier price are coming in base…
pmarjan cce0a99
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 89b968a
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 75f71a4
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 4c5086a
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 9bd5def
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 7a526e9
magento/magento2#26121: special price & tier price are coming in base…
pmarjan d99ff1b
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 831fb5b
magento/magento2#26121: special price & tier price are coming in base…
pmarjan f83763e
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 20cd3d0
magento/magento2#26121: special price & tier price are coming in base…
pmarjan 9d80fa3
magento/magento2#26121: special price & tier price are coming in base…
pmarjan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Pricing\Price\SpecialPrice as PricingSpecialPrice; | ||
|
||
/** | ||
* Resolver for Special Price | ||
*/ | ||
class SpecialPrice implements ResolverInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $value['model']; | ||
/** @var PricingSpecialPrice $specialPrice */ | ||
$specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE); | ||
pmarjan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ($specialPrice->getValue()) { | ||
return $specialPrice->getValue(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\CatalogCustomer; | ||
|
||
use Magento\GraphQl\GetCustomerAuthenticationHeader; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\Store\Api\StoreRepositoryInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\ObjectManager; | ||
|
||
class PriceTiersTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var GetCustomerAuthenticationHeader | ||
*/ | ||
private $getCustomerAuthenticationHeader; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->objectManager = Bootstrap::getObjectManager(); | ||
$this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||
*/ | ||
public function testAllGroups() | ||
{ | ||
$productSku = 'simple'; | ||
$query = $this->getProductSearchQuery($productSku); | ||
|
||
$response = $this->graphQlQuery($query); | ||
|
||
$itemTiers = $response['products']['items'][0]['price_tiers']; | ||
$this->assertCount(5, $itemTiers); | ||
$this->assertEquals(8, $this->getValueForQuantity(2, $itemTiers)); | ||
$this->assertEquals(5, $this->getValueForQuantity(3, $itemTiers)); | ||
$this->assertEquals(6, $this->getValueForQuantity(3.2, $itemTiers)); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php | ||
*/ | ||
public function testLoggedInCustomer() | ||
{ | ||
$productSku = 'simple'; | ||
$query = $this->getProductSearchQuery($productSku); | ||
$response = $this->graphQlQuery( | ||
$query, | ||
[], | ||
'', | ||
$this->getCustomerAuthenticationHeader->execute('[email protected]', 'password') | ||
); | ||
|
||
$itemTiers = $response['products']['items'][0]['price_tiers']; | ||
$this->assertCount(3, $itemTiers); | ||
$this->assertEquals(9.25, $this->getValueForQuantity(2, $itemTiers)); | ||
$this->assertEquals(8.25, $this->getValueForQuantity(3, $itemTiers)); | ||
$this->assertEquals(7.25, $this->getValueForQuantity(5, $itemTiers)); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_multiple_groups.php | ||
*/ | ||
public function testSecondStoreViewWithCurrencyRate() | ||
{ | ||
$storeViewCode = 'fixture_second_store'; | ||
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class); | ||
$rate = $storeRepository->get($storeViewCode)->getCurrentCurrencyRate(); | ||
$productSku = 'simple'; | ||
$query = $this->getProductSearchQuery($productSku); | ||
$headers = array_merge( | ||
$this->getCustomerAuthenticationHeader->execute('[email protected]', 'password'), | ||
$this->getHeaderStore($storeViewCode) | ||
); | ||
|
||
$response = $this->graphQlQuery( | ||
$query, | ||
[], | ||
'', | ||
$headers | ||
); | ||
|
||
$itemTiers = $response['products']['items'][0]['price_tiers']; | ||
$this->assertCount(3, $itemTiers); | ||
$this->assertEquals(round(9.25 * $rate, 2), $this->getValueForQuantity(2, $itemTiers)); | ||
$this->assertEquals(round(8.25 * $rate, 2), $this->getValueForQuantity(3, $itemTiers)); | ||
$this->assertEquals(round(7.25 * $rate, 2), $this->getValueForQuantity(5, $itemTiers)); | ||
} | ||
|
||
/** | ||
* Get the tier price value for the given product quantity | ||
* | ||
* @param float $quantity | ||
* @param array $tiers | ||
* @return float | ||
*/ | ||
private function getValueForQuantity(float $quantity, array $tiers): float | ||
{ | ||
$filteredResult = array_values(array_filter($tiers, function ($tier) use ($quantity) { | ||
if ((float)$tier['quantity'] == $quantity) { | ||
return $tier; | ||
} | ||
})); | ||
|
||
return (float)$filteredResult[0]['final_price']['value']; | ||
} | ||
|
||
/** | ||
* Get a query which user filter for product sku and returns price_tiers | ||
* | ||
* @param string $productSku | ||
* @return string | ||
*/ | ||
private function getProductSearchQuery(string $productSku): string | ||
{ | ||
return <<<QUERY | ||
{ | ||
products(filter: {sku: {eq: "{$productSku}"}}) { | ||
items { | ||
price_tiers { | ||
final_price { | ||
currency | ||
value | ||
} | ||
discount { | ||
amount_off | ||
percent_off | ||
} | ||
quantity | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
} | ||
|
||
/** | ||
* Get array that would be used in request header | ||
* | ||
* @param string $storeViewCode | ||
* @return array | ||
*/ | ||
private function getHeaderStore(string $storeViewCode): array | ||
{ | ||
return ['Store' => $storeViewCode]; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/SpecialPriceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\CatalogCustomer; | ||
|
||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\Store\Api\StoreRepositoryInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\ObjectManager; | ||
|
||
class SpecialPriceTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->objectManager = Bootstrap::getObjectManager(); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_special_price.php | ||
*/ | ||
public function testSpecialPrice() | ||
{ | ||
$productSku = 'simple'; | ||
$query = $this->getProductSearchQuery($productSku); | ||
|
||
$response = $this->graphQlQuery($query); | ||
|
||
$specialPrice = (float)$response['products']['items'][0]['special_price']; | ||
$this->assertEquals(5.99, $specialPrice); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_special_price.php | ||
*/ | ||
public function testSpecialPriceWithCurrencyRate() | ||
{ | ||
$storeViewCode = 'fixture_second_store'; | ||
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class); | ||
$rate = $storeRepository->get($storeViewCode)->getCurrentCurrencyRate(); | ||
$productSku = 'simple'; | ||
$query = $this->getProductSearchQuery($productSku); | ||
$headers = $this->getHeaderStore($storeViewCode); | ||
|
||
$response = $this->graphQlQuery( | ||
$query, | ||
[], | ||
'', | ||
$headers | ||
); | ||
|
||
$specialPrice = (float)$response['products']['items'][0]['special_price']; | ||
$this->assertEquals(round(5.99 * $rate, 2), $specialPrice); | ||
} | ||
|
||
/** | ||
* Get a query which user filter for product sku and returns special_price | ||
* | ||
* @param string $productSku | ||
* @return string | ||
*/ | ||
private function getProductSearchQuery(string $productSku): string | ||
{ | ||
return <<<QUERY | ||
{ | ||
products(filter: {sku: {eq: "{$productSku}"}}) { | ||
items { | ||
special_price | ||
} | ||
} | ||
} | ||
QUERY; | ||
} | ||
|
||
/** | ||
* Get array that would be used in request header | ||
* | ||
* @param string $storeViewCode | ||
* @return array | ||
*/ | ||
private function getHeaderStore(string $storeViewCode): array | ||
{ | ||
return ['Store' => $storeViewCode]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.