Attributes.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\ConfigurableProductGraphQl\Model\Resolver\Variant;
  8. use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
  9. use Magento\Framework\GraphQl\Query\Resolver\Value;
  10. use Magento\Catalog\Model\Product;
  11. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  12. use Magento\Framework\GraphQl\Config\Element\Field;
  13. use Magento\Framework\GraphQl\Query\ResolverInterface;
  14. /**
  15. * Format a product's option information to conform to GraphQL schema representation
  16. */
  17. class Attributes implements ResolverInterface
  18. {
  19. /**
  20. * @inheritdoc
  21. *
  22. * Format product's option data to conform to GraphQL schema
  23. *
  24. * @param Field $field
  25. * @param ContextInterface $context
  26. * @param ResolveInfo $info
  27. * @param array|null $value
  28. * @param array|null $args
  29. * @throws \Exception
  30. * @return mixed|Value
  31. */
  32. public function resolve(
  33. Field $field,
  34. $context,
  35. ResolveInfo $info,
  36. array $value = null,
  37. array $args = null
  38. ) {
  39. if (!isset($value['options']) || !isset($value['product'])) {
  40. return null;
  41. }
  42. $data = [];
  43. foreach ($value['options'] as $option) {
  44. $code = $option['attribute_code'];
  45. /** @var Product|null $model */
  46. $model = $value['product']['model'] ?? null;
  47. if (!$model || !$model->getData($code)) {
  48. continue;
  49. }
  50. foreach ($option['values'] as $optionValue) {
  51. if ($optionValue['value_index'] != $model->getData($code)) {
  52. continue;
  53. }
  54. $data[] = [
  55. 'label' => $optionValue['label'],
  56. 'code' => $code,
  57. 'use_default_value' => $optionValue['use_default_value'],
  58. 'value_index' => $optionValue['value_index']
  59. ];
  60. }
  61. }
  62. return $data;
  63. }
  64. }