Skip to content

Commit 5b3c8c7

Browse files
committed
add get all attribute and options
1 parent b4fb084 commit 5b3c8c7

File tree

5 files changed

+312
-74
lines changed

5 files changed

+312
-74
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Lof\ProductAttributesGraphQl\Model\Resolver;
3+
4+
use Lof\ProductAttributesGraphQl\Model\Resolver\DataProvider\Attributes;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
7+
use Magento\Framework\GraphQl\Query\ResolverInterface;
8+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
9+
use Magento\Catalog\Model\Product;
10+
11+
class AdditionalAttributes implements ResolverInterface
12+
{
13+
/**
14+
* @var Attributes
15+
*/
16+
protected $attributeRepository;
17+
18+
/**
19+
* @param Attributes $attributeRepository
20+
*/
21+
public function __construct(
22+
Attributes $attributeRepository,
23+
){
24+
$this->attributeRepository = $attributeRepository;
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
31+
{
32+
if (!isset($value['model'])) {
33+
throw new GraphQlInputException(__('Value must contain "model" property.'));
34+
}
35+
/** @var Product $product */
36+
$product = $value['model'];
37+
$productSku = $product->getSku();
38+
if (empty($productSku)) {
39+
throw new GraphQlInputException(__('Value must contain "product_sku" property.'));
40+
}
41+
42+
$data = $this->attributeRepository->getAdditionalAttributesBySku($productSku);
43+
return [
44+
'total_count' => count($data),
45+
'items' => $data
46+
];
47+
}
48+
}
Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
<?php
2+
23
namespace Lof\ProductAttributesGraphQl\Model\Resolver\DataProvider;
34

5+
use Magento\Catalog\Model\Product\Attribute\Repository;
46
use Magento\Catalog\Model\ProductRepository;
7+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
8+
use Magento\Framework\Api\AttributeInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
510

611
class Attributes
712
{
813
/**
914
* @var ProductRepository
1015
*/
11-
protected $productRepository;
16+
private $productRepository;
17+
18+
/**
19+
* @var CollectionFactory
20+
*/
21+
private $attributeFactory;
22+
23+
/**
24+
* @var Repository
25+
*/
26+
private $attributeRepository;
1227

1328
/**
1429
* @param ProductRepository $productRepository
30+
* @param CollectionFactory $attributeFactory
31+
* @param Repository $attributeRepository
1532
*/
1633
public function __construct(
1734
ProductRepository $productRepository,
18-
){
35+
CollectionFactory $attributeFactory,
36+
Repository $attributeRepository
37+
)
38+
{
1939
$this->productRepository = $productRepository;
40+
$this->attributeFactory = $attributeFactory;
41+
$this->attributeRepository = $attributeRepository;
2042
}
2143

2244
/**
45+
* Get additional attributes by sku
46+
*
2347
* @param $sku
24-
* @return \Magento\Framework\Api\AttributeInterface[]|null
25-
* @throws \Magento\Framework\Exception\NoSuchEntityException
48+
* @return AttributeInterface[]|null
49+
* @throws NoSuchEntityException
2650
*/
27-
public function getBySku($sku)
51+
public function getAdditionalAttributesBySku($sku)
2852
{
2953
if (!$sku) {
3054
return null;
@@ -33,7 +57,7 @@ public function getBySku($sku)
3357
$attributes = $product->getAttributes();
3458
$additionalAttributes = [];
3559
foreach ($attributes as $attribute) {
36-
if($attribute->getIsUserDefined() && $attribute->getIsVisibleOnFront()) {
60+
if ($attribute->getIsUserDefined() && $attribute->getIsVisibleOnFront()) {
3761
$data['id'] = $attribute->getId();
3862
$data['code'] = $attribute->getAttributeCode();
3963
$data['label'] = $attribute->getStoreLabel();
@@ -45,4 +69,39 @@ public function getBySku($sku)
4569
}
4670
return $additionalAttributes;
4771
}
72+
73+
/**
74+
* Get visible and filterable attributes and options
75+
*
76+
* @return array
77+
* @throws NoSuchEntityException
78+
*/
79+
public function getVisibleAttributes()
80+
{
81+
$attributeData = [];
82+
$attributeInfo = $this->attributeFactory->create();
83+
foreach ($attributeInfo as $items) {
84+
if ($items->getIsVisibleOnFront() && $items->getIsFilterable()) {
85+
$options = $this->attributeRepository->get($items->getAttributeCode())->getOptions();
86+
$optionData = [];
87+
foreach ($options as $option) {
88+
if (!($option->getValue() == null) || !($option->getLabel() == " ")) {
89+
$optionData[] = [
90+
'label' => $option->getLabel(),
91+
'value' => $option->getValue(),
92+
];
93+
}
94+
}
95+
if (!empty($optionData)) {
96+
$attributeData[] = [
97+
'attribute_code' => $items->getAttributeCode(),
98+
'attribute_label' => $items->getStoreLabel(),
99+
'total_count' => count($optionData),
100+
'options' => $optionData,
101+
];
102+
}
103+
}
104+
}
105+
return $attributeData;
106+
}
48107
}

Model/Resolver/AttributeBySku.php renamed to Model/Resolver/VisibleAttributes.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
77
use Magento\Framework\GraphQl\Query\ResolverInterface;
88
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
9+
use Magento\Catalog\Model\Product;
910

10-
class AttributeBySku implements ResolverInterface
11+
class VisibleAttributes implements ResolverInterface
1112
{
1213
/**
1314
* @var Attributes
@@ -28,11 +29,7 @@ public function __construct(
2829
*/
2930
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
3031
{
31-
if (!isset($args['sku']) || (isset($args['sku']) && !$args['sku'])) {
32-
throw new GraphQlInputException(__('sku is required.'));
33-
}
34-
35-
$data = $this->attributeRepository->getBySku($args['sku']);
32+
$data = $this->attributeRepository->getVisibleAttributes();
3633
return [
3734
'total_count' => count($data),
3835
'items' => $data

0 commit comments

Comments
 (0)