-
Notifications
You must be signed in to change notification settings - Fork 153
Products: Access to related up sell cross sell product fields #249
Changes from all commits
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,43 @@ | ||
<?php | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class Related implements ResolverInterface | ||
{ | ||
|
||
/** | ||
* Fetches the data from persistence models and format it according to the GraphQL schema. | ||
* | ||
* @param \Magento\Framework\GraphQl\Config\Element\Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @throws \Exception | ||
* @return mixed|Value | ||
*/ | ||
|
||
|
||
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')); | ||
} | ||
|
||
$product = $value['model']; | ||
$productListType = $field->getName(); | ||
|
||
return [ | ||
'model' => $product, | ||
'product_list_type' => $productListType | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related; | ||
|
||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\CrossSellDataProvider; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
|
||
class CrossSellProducts implements ResolverInterface | ||
{ | ||
/** | ||
* Attribute to select fields | ||
*/ | ||
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key']; | ||
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. We should take this data from request |
||
/** | ||
* @var CrossSellDataProvider | ||
*/ | ||
private $dataProvider; | ||
|
||
public function __construct(CrossSellDataProvider $dataProvider) | ||
{ | ||
$this->dataProvider = $dataProvider; | ||
} | ||
|
||
/** | ||
* Fetches the data from persistence models and format it according to the GraphQL schema. | ||
* | ||
* @param \Magento\Framework\GraphQl\Config\Element\Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @throws \Exception | ||
* @return mixed|Value | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
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. We can use |
||
{ | ||
$product = $value['model']; | ||
$this->dataProvider->addFieldToSelect(self::FIELDS); | ||
$collection = $this->dataProvider->getData($product); | ||
|
||
$count = 0; | ||
foreach ($collection as $item) { | ||
$data[$count] = $item->getData(); | ||
$data[$count]['model'] = $item; | ||
$count++; | ||
} | ||
return $data; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related; | ||
|
||
|
||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\RelatedDataProvider; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
|
||
/** | ||
* Class RelatedProducts | ||
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related | ||
*/ | ||
class RelatedProducts implements ResolverInterface | ||
{ | ||
|
||
/** | ||
* Attribute to select fields | ||
*/ | ||
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key']; | ||
/** | ||
* @var RelatedDataProvider | ||
*/ | ||
private $dataProvider; | ||
|
||
/** | ||
* RelatedProducts constructor. | ||
* @param RelatedDataProvider $dataProvider | ||
*/ | ||
public function __construct(RelatedDataProvider $dataProvider) | ||
{ | ||
$this->dataProvider = $dataProvider; | ||
} | ||
|
||
/** | ||
* Fetches the data from persistence models and format it according to the GraphQL schema. | ||
* | ||
* @param \Magento\Framework\GraphQl\Config\Element\Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @throws \Exception | ||
* @return mixed|Value | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$product = $value['model']; | ||
$this->dataProvider->addFieldToSelect(self::FIELDS); | ||
$collection = $this->dataProvider->getData($product); | ||
|
||
$count = 0; | ||
$data = []; | ||
foreach ($collection as $item) { | ||
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[$count] = $item->getData(); | ||
$data[$count]['model'] = $item; | ||
$count++; | ||
} | ||
return $data; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related; | ||
|
||
|
||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\UpSellDataProvider; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
|
||
/** | ||
* Class UpSellProducts | ||
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related | ||
*/ | ||
class UpSellProducts implements ResolverInterface | ||
{ | ||
/** | ||
* Attribute to select fields | ||
*/ | ||
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key']; | ||
/** | ||
* @var UpSellDataProvider | ||
*/ | ||
private $dataProvider; | ||
|
||
/** | ||
* UpSellProducts constructor. | ||
* @param UpSellDataProvider $dataProvider | ||
*/ | ||
public function __construct(UpSellDataProvider $dataProvider) | ||
{ | ||
$this->dataProvider = $dataProvider; | ||
} | ||
|
||
/** | ||
* Fetches the data from persistence models and format it according to the GraphQL schema. | ||
* | ||
* @param \Magento\Framework\GraphQl\Config\Element\Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @throws \Exception | ||
* @return mixed|Value | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$product = $value['model']; | ||
$this->dataProvider->addFieldToSelect(self::FIELDS); | ||
$collection = $this->dataProvider->getData($product); | ||
|
||
$count = 0; | ||
$data = []; | ||
foreach ($collection as $item) { | ||
$data[$count] = $item->getData(); | ||
$data[$count]['model'] = $item; | ||
$count++; | ||
} | ||
return $data; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related; | ||
|
||
abstract class AbstractDataProvider | ||
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. Pls, don't use supertypes only for code reusing (if a relation is not like |
||
{ | ||
|
||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $fields = []; | ||
/** | ||
* @var $collection | ||
*/ | ||
protected $collection; | ||
|
||
|
||
/** | ||
* @param $field | ||
*/ | ||
public function addFieldToSelect($field) | ||
{ | ||
$this->fields = $field; | ||
} | ||
|
||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getFields(): array | ||
{ | ||
return $this->fields; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getCollection() | ||
{ | ||
return $this->collection; | ||
} | ||
|
||
/** | ||
* @param $product | ||
* @return mixed | ||
*/ | ||
public function getData($product) | ||
{ | ||
$this->prepareCollection($product); | ||
return $this->collection; | ||
|
||
} | ||
|
||
/** | ||
* @param $product | ||
*/ | ||
protected function prepareCollection($product): void | ||
{ | ||
$this->collection = $product->getRelatedProducts(); | ||
$this->collection->addAttributeToSelect($this->getFields()); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related; | ||
|
||
|
||
/** | ||
* Class CrossSellDataProvider | ||
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related | ||
*/ | ||
class CrossSellDataProvider extends AbstractDataProvider | ||
{ | ||
/** | ||
* @param $product | ||
*/ | ||
protected function prepareCollection($product): void | ||
{ | ||
$this->collection = $product->getCrossSellProductCollection(); | ||
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. Looks like proxy on another method |
||
$this->collection->addAttributeToSelect($this->getFields()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related; | ||
|
||
|
||
class RelatedDataProvider extends AbstractDataProvider | ||
{ | ||
|
||
|
||
protected function prepareCollection($product): void | ||
{ | ||
$this->collection = $product->getRelatedProductCollection(); | ||
$this->collection->addAttributeToSelect($this->getFields()); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related; | ||
|
||
/** | ||
* Class UpSellDataProvider | ||
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related | ||
*/ | ||
class UpSellDataProvider extends AbstractDataProvider | ||
{ | ||
|
||
/** | ||
* @param $product | ||
*/ | ||
protected function prepareCollection($product): void | ||
{ | ||
$this->collection = $product->getUpSellProductCollection(); | ||
$this->collection->addAttributeToSelect($this->getFields()); | ||
} | ||
} |
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.
Pls, add
declare(strict_types=1);
to all of new filesAnd check code style of all of files