-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LYNX-109: Add custom_Attributes field to product (#125)
- Loading branch information
1 parent
5b556f6
commit e9a93c6
Showing
5 changed files
with
596 additions
and
1 deletion.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductCustomAttributes.php
This file contains 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,155 @@ | ||
<?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\Catalog\Api\Data\ProductAttributeInterface; | ||
use Magento\Catalog\Model\FilterProductCustomAttribute; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\CatalogGraphQl\Model\ProductDataProvider; | ||
use Magento\Eav\Api\Data\AttributeInterface; | ||
use Magento\Eav\Model\AttributeRepository; | ||
use Magento\EavGraphQl\Model\Output\Value\GetAttributeValueInterface; | ||
use Magento\EavGraphQl\Model\Resolver\AttributeFilter; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\GraphQl\Model\Query\ContextInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
|
||
/** | ||
* | ||
* Format a product's custom attribute information to conform to GraphQL schema representation | ||
*/ | ||
class ProductCustomAttributes implements ResolverInterface | ||
{ | ||
/** | ||
* @var AttributeRepository | ||
*/ | ||
private AttributeRepository $attributeRepository; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private SearchCriteriaBuilder $searchCriteriaBuilder; | ||
|
||
/** | ||
* @var GetAttributeValueInterface | ||
*/ | ||
private GetAttributeValueInterface $getAttributeValue; | ||
|
||
/** | ||
* @var ProductDataProvider | ||
*/ | ||
private ProductDataProvider $productDataProvider; | ||
|
||
/** | ||
* @var AttributeFilter | ||
*/ | ||
private AttributeFilter $attributeFilter; | ||
|
||
/** | ||
* @var FilterProductCustomAttribute | ||
*/ | ||
private FilterProductCustomAttribute $filterCustomAttribute; | ||
|
||
/** | ||
* @param AttributeRepository $attributeRepository | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
* @param GetAttributeValueInterface $getAttributeValue | ||
* @param ProductDataProvider $productDataProvider | ||
* @param AttributeFilter $attributeFilter | ||
* @param FilterProductCustomAttribute $filterCustomAttribute | ||
*/ | ||
public function __construct( | ||
AttributeRepository $attributeRepository, | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
GetAttributeValueInterface $getAttributeValue, | ||
ProductDataProvider $productDataProvider, | ||
AttributeFilter $attributeFilter, | ||
FilterProductCustomAttribute $filterCustomAttribute | ||
) { | ||
$this->attributeRepository = $attributeRepository; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
$this->getAttributeValue = $getAttributeValue; | ||
$this->productDataProvider = $productDataProvider; | ||
$this->attributeFilter = $attributeFilter; | ||
$this->filterCustomAttribute = $filterCustomAttribute; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @throws \Exception | ||
* @return array | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
$filterArgs = $args['filter'] ?? []; | ||
|
||
$searchCriteriaBuilder = $this->attributeFilter->execute($filterArgs, $this->searchCriteriaBuilder); | ||
|
||
$searchCriteriaBuilder = $searchCriteriaBuilder | ||
->addFilter('is_visible', true) | ||
->addFilter('backend_type', 'static', 'neq') | ||
->create(); | ||
|
||
$productCustomAttributes = $this->attributeRepository->getList( | ||
ProductAttributeInterface::ENTITY_TYPE_CODE, | ||
$searchCriteriaBuilder | ||
)->getItems(); | ||
|
||
$attributeCodes = array_map( | ||
function (AttributeInterface $customAttribute) { | ||
return $customAttribute->getAttributeCode(); | ||
}, | ||
$productCustomAttributes | ||
); | ||
|
||
$filteredAttributeCodes = $this->filterCustomAttribute->execute(array_flip($attributeCodes)); | ||
|
||
/** @var Product $product */ | ||
$product = $value['model']; | ||
$productData = $this->productDataProvider->getProductDataById((int)$product->getId()); | ||
|
||
$customAttributes = []; | ||
foreach ($filteredAttributeCodes as $attributeCode => $value) { | ||
if (!array_key_exists($attributeCode, $productData)) { | ||
continue; | ||
} | ||
$attributeValue = $productData[$attributeCode]; | ||
if (is_array($attributeValue)) { | ||
$attributeValue = implode(',', $attributeValue); | ||
} | ||
$customAttributes[] = [ | ||
'attribute_code' => $attributeCode, | ||
'value' => $attributeValue | ||
]; | ||
} | ||
|
||
return array_map( | ||
function (array $customAttribute) { | ||
return $this->getAttributeValue->execute( | ||
ProductAttributeInterface::ENTITY_TYPE_CODE, | ||
$customAttribute['attribute_code'], | ||
$customAttribute['value'] | ||
); | ||
}, | ||
$customAttributes | ||
); | ||
} | ||
} |
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
app/code/Magento/EavGraphQl/Model/Resolver/AttributeFilter.php
This file contains 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,32 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\EavGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
|
||
/** | ||
* Creates a SearchCriteriaBuilder object from the provided arguments | ||
*/ | ||
class AttributeFilter | ||
{ | ||
/** | ||
* Returns a SearchCriteriaBuilder object with filters from the passed args | ||
* | ||
* @param array $filterArgs | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
* @return SearchCriteriaBuilder SearchCriteriaBuilder | ||
*/ | ||
public function execute(array $filterArgs, $searchCriteriaBuilder): SearchCriteriaBuilder | ||
{ | ||
foreach ($filterArgs as $key => $value) { | ||
$searchCriteriaBuilder->addFilter($key, $value); | ||
} | ||
|
||
return $searchCriteriaBuilder; | ||
} | ||
} |
Oops, something went wrong.