CustomizableOption.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\CartItem\DataProvider;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Quote\Model\Quote\Item as QuoteItem;
  10. /**
  11. * Custom Option Data provider
  12. */
  13. class CustomizableOption
  14. {
  15. /**
  16. * @var CustomizableOptionValueInterface
  17. */
  18. private $customizableOptionValue;
  19. /**
  20. * @param CustomizableOptionValueInterface $customOptionValueDataProvider
  21. */
  22. public function __construct(
  23. CustomizableOptionValueInterface $customOptionValueDataProvider
  24. ) {
  25. $this->customizableOptionValue = $customOptionValueDataProvider;
  26. }
  27. /**
  28. * Retrieve custom option data
  29. *
  30. * @param QuoteItem $cartItem
  31. * @param int $optionId
  32. * @return array
  33. * @throws LocalizedException
  34. */
  35. public function getData(QuoteItem $cartItem, int $optionId): array
  36. {
  37. $product = $cartItem->getProduct();
  38. $option = $product->getOptionById($optionId);
  39. if (!$option) {
  40. return [];
  41. }
  42. $selectedOption = $cartItem->getOptionByCode('option_' . $option->getId());
  43. $selectedOptionValueData = $this->customizableOptionValue->getData(
  44. $cartItem,
  45. $option,
  46. $selectedOption
  47. );
  48. return [
  49. 'id' => $option->getId(),
  50. 'label' => $option->getTitle(),
  51. 'type' => $option->getType(),
  52. 'values' => $selectedOptionValueData,
  53. 'sort_order' => $option->getSortOrder(),
  54. 'is_required' => $option->getIsRequire(),
  55. ];
  56. }
  57. }