CustomizableOptions.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\QuoteGraphQl\Model\Resolver;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\GraphQl\Config\Element\Field;
  10. use Magento\Framework\GraphQl\Query\ResolverInterface;
  11. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  12. use Magento\Quote\Model\Quote\Item as QuoteItem;
  13. use Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOption;
  14. /**
  15. * @inheritdoc
  16. */
  17. class CustomizableOptions implements ResolverInterface
  18. {
  19. /**
  20. * @var CustomizableOption
  21. */
  22. private $customizableOption;
  23. /**
  24. * @param CustomizableOption $customizableOption
  25. */
  26. public function __construct(
  27. CustomizableOption $customizableOption
  28. ) {
  29. $this->customizableOption = $customizableOption;
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  35. {
  36. if (!isset($value['model'])) {
  37. throw new LocalizedException(__('"model" value should be specified'));
  38. }
  39. /** @var QuoteItem $cartItem */
  40. $cartItem = $value['model'];
  41. $quoteItemOption = $cartItem->getOptionByCode('option_ids');
  42. if (null === $quoteItemOption) {
  43. return [];
  44. }
  45. $customizableOptionsData = [];
  46. $customizableOptionIds = explode(',', $quoteItemOption->getValue());
  47. foreach ($customizableOptionIds as $customizableOptionId) {
  48. $customizableOption = $this->customizableOption->getData(
  49. $cartItem,
  50. (int)$customizableOptionId
  51. );
  52. $customizableOptionsData[] = $customizableOption;
  53. }
  54. return $customizableOptionsData;
  55. }
  56. }