-
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
Changes from 9 commits
5c69aa1
cce0a99
89b968a
75f71a4
4c5086a
9bd5def
7a526e9
d99ff1b
831fb5b
f83763e
20cd3d0
9d80fa3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,230 @@ | ||||
<?php | ||||
/** | ||||
* Copyright © Magento, Inc. All rights reserved. | ||||
* See COPYING.txt for license details. | ||||
*/ | ||||
declare(strict_types=1); | ||||
|
||||
namespace Magento\GraphQl\CatalogCustomer; | ||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface; | ||||
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory; | ||||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||||
use Magento\Catalog\Model\Product; | ||||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||||
use Magento\Store\Api\StoreRepositoryInterface; | ||||
use Magento\TestFramework\Helper\Bootstrap; | ||||
|
||||
class PriceTiersTest extends GraphQlAbstract | ||||
{ | ||||
/** | ||||
* @var \Magento\TestFramework\ObjectManager | ||||
*/ | ||||
private $objectManager; | ||||
|
||||
protected function setUp(): void | ||||
{ | ||||
$this->objectManager = Bootstrap::getObjectManager(); | ||||
} | ||||
|
||||
/** | ||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||||
*/ | ||||
public function testAllGroups() | ||||
{ | ||||
/** @var string $productSku */ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Remove all unnecessary @var annotations |
||||
$productSku = 'simple'; | ||||
/** @var string $query */ | ||||
$query = $this->getProductSearchQuery($productSku); | ||||
|
||||
$response = $this->graphQlQuery($query); | ||||
|
||||
$itemTiers = $response['products']['items'][0]['price_tiers']; | ||||
$this->assertEquals(5, sizeof($itemTiers)); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use assertCount() |
||||
$this->assertEquals(8, $this->getValueForQuantity(2, $itemTiers)); | ||||
$this->assertEquals(5, $this->getValueForQuantity(3, $itemTiers)); | ||||
$this->assertEquals(6, $this->getValueForQuantity(3.2, $itemTiers)); | ||||
} | ||||
|
||||
/** | ||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||||
*/ | ||||
public function testLoggedInCustomer() | ||||
{ | ||||
/** @var string $productSku */ | ||||
$productSku = 'simple'; | ||||
/** @var ProductRepositoryInterface $productRepository */ | ||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class); | ||||
/** @var Product $product */ | ||||
$product = $productRepository->get($productSku, false, null, true); | ||||
$tierPriceData =[ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data changes should be done in fixtures |
||||
[ | ||||
'customer_group_id' => 1, | ||||
'percentage_value'=> null, | ||||
'qty'=> 2, | ||||
'value'=> 9 | ||||
], | ||||
[ | ||||
'customer_group_id' => 1, | ||||
'percentage_value'=> null, | ||||
'qty'=> 3, | ||||
'value'=> 8.25 | ||||
], | ||||
[ | ||||
'customer_group_id' => 1, | ||||
'percentage_value'=> null, | ||||
'qty'=> 5, | ||||
'value'=> 7 | ||||
], | ||||
[ | ||||
'customer_group_id' => 2, | ||||
'percentage_value'=> null, | ||||
'qty'=> 3, | ||||
'value'=> 8 | ||||
] | ||||
]; | ||||
|
||||
$this->saveTierPrices($product, $tierPriceData); | ||||
/** @var string $query */ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
$query = $this->getProductSearchQuery($productSku); | ||||
$response = $this->graphQlQuery( | ||||
$query, | ||||
[], | ||||
'', | ||||
$this->getHeaderAuthorization('[email protected]', 'password') | ||||
); | ||||
|
||||
$itemTiers = $response['products']['items'][0]['price_tiers']; | ||||
$this->assertEquals(3, sizeof($itemTiers)); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use assertCount() |
||||
$this->assertEquals(9, $this->getValueForQuantity(2, $itemTiers)); | ||||
$this->assertEquals(8.25, $this->getValueForQuantity(3, $itemTiers)); | ||||
$this->assertEquals(7, $this->getValueForQuantity(5, $itemTiers)); | ||||
} | ||||
|
||||
/** | ||||
* @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php | ||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php | ||||
*/ | ||||
public function testSecondStoreViewWithCurrencyRate() | ||||
{ | ||||
/** @var string $storeViewCode */ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Remove all unnecessary @var annotations |
||||
$storeViewCode = 'fixture_second_store'; | ||||
/** @var StoreRepositoryInterface $storeRepository */ | ||||
$storeRepository = $this->objectManager->get(StoreRepositoryInterface::class); | ||||
/** @var float $rate */ | ||||
$rate = $storeRepository->get($storeViewCode)->getCurrentCurrencyRate(); | ||||
/** @var string $productSku */ | ||||
$productSku = 'simple'; | ||||
/** @var string $query */ | ||||
$query = $this->getProductSearchQuery($productSku); | ||||
/** @var array $headers */ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
$headers = array_merge( | ||||
$this->getHeaderAuthorization('[email protected]', 'password'), | ||||
$this->getHeaderStore($storeViewCode) | ||||
); | ||||
|
||||
$response = $this->graphQlQuery( | ||||
$query, | ||||
[], | ||||
'', | ||||
$headers | ||||
); | ||||
|
||||
$itemTiers = $response['products']['items'][0]['price_tiers']; | ||||
$this->assertEquals(2, sizeof($itemTiers)); | ||||
$this->assertEquals(round(8 * $rate, 2), $this->getValueForQuantity(2, $itemTiers)); | ||||
$this->assertEquals(round(5 * $rate, 2), $this->getValueForQuantity(5, $itemTiers)); | ||||
} | ||||
|
||||
/** | ||||
* @param float $quantity | ||||
* @param array $tiers | ||||
* @return float | ||||
*/ | ||||
private function getValueForQuantity(float $quantity, array $tiers) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add short description for utility methods |
||||
{ | ||||
$filteredResult = array_values(array_filter($tiers, function ($tier) use ($quantity) { | ||||
if ((float)$tier['quantity'] == $quantity) { | ||||
return $tier; | ||||
} | ||||
})); | ||||
|
||||
return (float)$filteredResult[0]['final_price']['value']; | ||||
} | ||||
|
||||
/** | ||||
* @param ProductInterface $product | ||||
* @param array $tierPriceData | ||||
*/ | ||||
private function saveTierPrices(ProductInterface $product, array $tierPriceData) | ||||
{ | ||||
/** @var array $tierPrices */ | ||||
$tierPrices = []; | ||||
/** @var ProductTierPriceInterfaceFactory $tierPriceFactory */ | ||||
$tierPriceFactory = $this->objectManager->get(ProductTierPriceInterfaceFactory::class); | ||||
|
||||
foreach ($tierPriceData as $tierPrice) { | ||||
$tierPrices[] = $tierPriceFactory->create( | ||||
[ | ||||
'data' => $tierPrice | ||||
] | ||||
); | ||||
} | ||||
|
||||
$product->setTierPrices($tierPrices); | ||||
$product->save(); | ||||
} | ||||
|
||||
/** | ||||
* @param string $productSku | ||||
* @return string | ||||
*/ | ||||
private function getProductSearchQuery(string $productSku): string | ||||
{ | ||||
return <<<QUERY | ||||
{ | ||||
products(search: "{$productSku}") { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be safer to use "filter sku equals" to make sure you get an exact match |
||||
items { | ||||
price_tiers { | ||||
final_price { | ||||
currency | ||||
value | ||||
} | ||||
discount { | ||||
amount_off | ||||
percent_off | ||||
} | ||||
quantity | ||||
} | ||||
} | ||||
} | ||||
} | ||||
QUERY; | ||||
} | ||||
|
||||
/** | ||||
* @param string $username | ||||
* @param string $password | ||||
* @return array | ||||
*/ | ||||
private function getHeaderAuthorization(string $username, string $password): array | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use \Magento\GraphQl\GetCustomerAuthenticationHeader instead |
||||
{ | ||||
$customerToken = $this->objectManager->get(CustomerTokenServiceInterface::class) | ||||
->createCustomerAccessToken($username, $password); | ||||
|
||||
return ['Authorization' => 'Bearer ' . $customerToken]; | ||||
} | ||||
|
||||
/** | ||||
* @param string $storeViewCode | ||||
* @return array | ||||
*/ | ||||
private function getHeaderStore(string $storeViewCode): array | ||||
{ | ||||
return ['Store' => $storeViewCode]; | ||||
} | ||||
} |
Uh oh!
There was an error while loading. Please reload this page.