Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.4-develop' into MCP-304
Browse files Browse the repository at this point in the history
  • Loading branch information
adifucan committed May 13, 2021
2 parents d2731af + f80f0d9 commit 0c226b0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
Expand All @@ -23,8 +24,13 @@ class SpecialPrice implements ResolverInterface
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

/** @var ProductInterface $product */
$product = $value['model'];

/** @var PricingSpecialPrice $specialPrice */
$specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function process(
ContextInterface $context = null
): Collection {
$collection->addAttributeToSelect('special_price');
$collection->addAttributeToSelect('special_price_from');
$collection->addAttributeToSelect('special_price_to');
$collection->addAttributeToSelect('special_from_date');
$collection->addAttributeToSelect('special_to_date');
$collection->addAttributeToSelect('tax_class_id');

return $collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,72 @@ public function testProductWithCatalogDiscount()
}
}

/**
* Check if the special price visible if the current date is in the date range set
* for the special price
*
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php
*/
public function testSpecialPriceVisibleIfInDateRange()
{
$query = <<<QUERY
{
products(filter: {sku: {eq: "simple_product"}}) {
items {
price_range {
minimum_price {
regular_price {
value
}
}
}
special_price
}
}
}
QUERY;
$result = $this->graphQlQuery($query);
$productInformation = $result['products']['items'][0];
$productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value'];

self::assertEquals('10', $productRegularPrice);
self::assertEquals('5.99', $productInformation['special_price']);
}

/**
* Check if the special price is not visible if the current date is not in the date range set
* for the special price
*
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php
*/
public function testSpecialPriceNotVisibleIfNotInDateRange()
{
$query = <<<QUERY
{
products(filter: {sku: {eq: "simple_product"}}) {
items {
price_range {
minimum_price {
regular_price {
value
}
}
}
special_price
}
}
}
QUERY;
$result = $this->graphQlQuery($query);
$productInformation = $result['products']['items'][0];
$productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value'];

self::assertEquals('10', $productRegularPrice);
self::assertEquals(null, $productInformation['special_price']);
}

/**
* Get GraphQl query to fetch products by sku
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);

$product = $productRepository->get('simple_product');
$product->setSpecialPrice('5.99');

$product->setSpecialFromDate(date('Y-m-d', strtotime('-1 day')));
$product->setSpecialToDate(date('Y-m-d', strtotime('+1 day')));

$productRepository->save($product);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);

$product = $productRepository->get('simple_product');
$product->setSpecialPrice('5.99');

$product->setSpecialFromDate(date('Y-m-d', strtotime('+3 day')));
$product->setSpecialToDate(date('Y-m-d', strtotime('+5 day')));

$productRepository->save($product);

0 comments on commit 0c226b0

Please sign in to comment.