-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[GraphQl] 28200 fixed issue with attribute label #29180
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 all commits
24ebc2e
0d904ab
3cfc907
6356cfa
71132b1
21b8964
19bb551
e736f16
dac9483
de49f18
04198e8
660c9bc
18f2b08
9e85947
61deae7
7e1e74e
392b096
c6b3577
4f38034
9aa76d2
ec8e1d2
a092ff5
008fb16
6666c40
8b6a3cd
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 |
---|---|---|
|
@@ -7,19 +7,23 @@ | |
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\Query; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResultFactory; | ||
use Magento\Framework\Api\Search\SearchCriteriaInterface; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\Phrase; | ||
use Magento\GraphQl\Model\Query\ContextInterface; | ||
use Magento\Search\Api\SearchInterface; | ||
use Magento\Search\Model\Search\PageSizeProvider; | ||
|
||
/** | ||
* Full text search for catalog using given search criteria. | ||
* | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class Search implements ProductQueryInterface | ||
{ | ||
|
@@ -115,10 +119,17 @@ public function getResult( | |
$totalPages = $realPageSize ? ((int)ceil($searchResults->getTotalCount() / $realPageSize)) : 0; | ||
|
||
$productArray = []; | ||
/** @var \Magento\Catalog\Model\Product $product */ | ||
/** @var Product $product */ | ||
foreach ($searchResults->getItems() as $product) { | ||
$productArray[$product->getId()] = $product->getData(); | ||
$productArray[$product->getId()]['model'] = $product; | ||
foreach ($queryFields as $field) { | ||
$productArray[$product->getId()][$field] = $this->getAttributeValue( | ||
$product, | ||
$productArray, | ||
$field | ||
); | ||
} | ||
} | ||
|
||
return $this->searchResultFactory->create( | ||
|
@@ -148,4 +159,27 @@ private function buildSearchCriteria(array $args, ResolveInfo $info): SearchCrit | |
|
||
return $searchCriteria; | ||
} | ||
|
||
/** | ||
* Get product attribute value | ||
* | ||
* @param Product $product | ||
* @param array $productArray | ||
* @param string $field | ||
* | ||
* @return string|null|int|bool | ||
*/ | ||
private function getAttributeValue(Product $product, array $productArray, string $field) | ||
{ | ||
if ($attribute = $product->getResource()->getAttribute($field)) { | ||
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. I wonder what performance impact will this have Also we tried to stay away from $product->getResource() |
||
$attributeValue = $attribute->getFrontend()->getValue($product); | ||
if ($attributeValue && !($attributeValue instanceof Phrase)) { | ||
return $attributeValue; | ||
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. is this how we translate the label? or what does $attribute->getFrontend()->getValue($product) do? |
||
} else { | ||
return $productArray[$product->getId()][$field] ?? null; | ||
} | ||
} | ||
|
||
return ""; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Catalog; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\Helper\CacheCleaner; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test class for product with multiselect attributes | ||
*/ | ||
class ProductWithMultiselectAttributeTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* Check correct displaying for multiselect attributes | ||
* | ||
* @magentoApiDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php | ||
*/ | ||
public function testQueryProductWithMultiselectAttribute() | ||
{ | ||
$this->reIndexAndCleanCache(); | ||
$query = <<<QUERY | ||
{ | ||
products(filter: {sku: {eq: "simple_ms_2" }}) { | ||
items { | ||
name | ||
multiselect_attribute | ||
} | ||
} | ||
} | ||
QUERY; | ||
$response = $this->graphQlQuery($query); | ||
$this->assertStringContainsString( | ||
'Option 2, Option 3, Option 4 "!@#$%^&*', | ||
$response['products']['items'][0]['multiselect_attribute'] | ||
); | ||
} | ||
|
||
/** | ||
* Reindex and clean cache | ||
* | ||
* @return void | ||
* @throws LocalizedException | ||
*/ | ||
private function reIndexAndCleanCache() : void | ||
{ | ||
$appDir = dirname(Bootstrap::getInstance()->getAppTempDir()); | ||
$out = ''; | ||
// phpcs:ignore Magento2.Security.InsecureFunction | ||
exec("php -f {$appDir}/bin/magento indexer:reindex", $out); | ||
$this->cleanCache(); | ||
Usik2203 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest specifying the return type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rogyar

I can't apply this change because attribute value can have
int
andbool
valuesI am providing this test as example
Thanks