GroupedItems.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GroupedProductGraphQl\Model\Resolver;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  10. use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Deferred\Product;
  11. use Magento\Framework\GraphQl\Config\Element\Field;
  12. use Magento\Framework\GraphQl\Query\ResolverInterface;
  13. use Magento\GroupedProduct\Model\Product\Initialization\Helper\ProductLinks\Plugin\Grouped;
  14. /**
  15. * @inheritdoc
  16. */
  17. class GroupedItems implements ResolverInterface
  18. {
  19. /**
  20. * @var Product
  21. */
  22. private $productResolver;
  23. /**
  24. * @param Product $productResolver
  25. */
  26. public function __construct(
  27. Product $productResolver
  28. ) {
  29. $this->productResolver = $productResolver;
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function resolve(
  35. Field $field,
  36. $context,
  37. ResolveInfo $info,
  38. array $value = null,
  39. array $args = null
  40. ) {
  41. if (!isset($value['model'])) {
  42. throw new LocalizedException(__('"model" value should be specified'));
  43. }
  44. $data = [];
  45. $productModel = $value['model'];
  46. $links = $productModel->getProductLinks();
  47. foreach ($links as $link) {
  48. if ($link->getLinkType() !== Grouped::TYPE_NAME) {
  49. continue;
  50. }
  51. $data[] = [
  52. 'position' => (int)$link->getPosition(),
  53. 'qty' => $link->getExtensionAttributes()->getQty(),
  54. 'sku' => $link->getLinkedProductSku()
  55. ];
  56. }
  57. return $data;
  58. }
  59. }