Multiple.php 1.9 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\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue;
  8. use Magento\Catalog\Model\Product\Option;
  9. use Magento\Catalog\Model\Product\Option\Type\DefaultType;
  10. use Magento\Quote\Model\Quote\Item as QuoteItem;
  11. use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
  12. use Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValueInterface;
  13. /**
  14. * Multiple Option Value Data provider
  15. */
  16. class Multiple implements CustomizableOptionValueInterface
  17. {
  18. /**
  19. * @var PriceUnitLabel
  20. */
  21. private $priceUnitLabel;
  22. /**
  23. * @param PriceUnitLabel $priceUnitLabel
  24. */
  25. public function __construct(
  26. PriceUnitLabel $priceUnitLabel
  27. ) {
  28. $this->priceUnitLabel = $priceUnitLabel;
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function getData(
  34. QuoteItem $cartItem,
  35. Option $option,
  36. SelectedOption $selectedOption
  37. ): array {
  38. $selectedOptionValueData = [];
  39. $optionIds = explode(',', $selectedOption->getValue());
  40. if (0 === count($optionIds)) {
  41. return $selectedOptionValueData;
  42. }
  43. foreach ($optionIds as $optionId) {
  44. $optionValue = $option->getValueById($optionId);
  45. $priceValueUnits = $this->priceUnitLabel->getData($optionValue->getPriceType());
  46. $selectedOptionValueData[] = [
  47. 'id' => $selectedOption->getId(),
  48. 'label' => $optionValue->getTitle(),
  49. 'price' => [
  50. 'type' => strtoupper($optionValue->getPriceType()),
  51. 'units' => $priceValueUnits,
  52. 'value' => $optionValue->getPrice(),
  53. ],
  54. ];
  55. }
  56. return $selectedOptionValueData;
  57. }
  58. }