Dropdown.php 2.0 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\CartItem\DataProvider\CustomizableOptionValue;
  8. use Magento\Catalog\Model\Product\Option;
  9. use Magento\Catalog\Model\Product\Option\Type\Select as SelectOptionType;
  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. * @inheritdoc
  15. */
  16. class Dropdown 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. /** @var SelectOptionType $optionTypeRenderer */
  39. $optionTypeRenderer = $option->groupFactory($option->getType())
  40. ->setOption($option)
  41. ->setConfigurationItemOption($selectedOption);
  42. $selectedValue = $selectedOption->getValue();
  43. $optionValue = $option->getValueById($selectedValue);
  44. $optionPriceType = (string)$optionValue->getPriceType();
  45. $priceValueUnits = $this->priceUnitLabel->getData($optionPriceType);
  46. $selectedOptionValueData = [
  47. 'id' => $selectedOption->getId(),
  48. 'label' => $optionTypeRenderer->getFormattedOptionValue($selectedValue),
  49. 'value' => $selectedValue,
  50. 'price' => [
  51. 'type' => strtoupper($optionPriceType),
  52. 'units' => $priceValueUnits,
  53. 'value' => $optionValue->getPrice(),
  54. ]
  55. ];
  56. return [$selectedOptionValueData];
  57. }
  58. }