|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\BundleGraphQl\Model\Cart; |
| 9 | + |
| 10 | +use Magento\Bundle\Helper\Catalog\Product\Configuration; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Quote\Model\Quote\Item; |
| 13 | +use Magento\Framework\Pricing\Helper\Data; |
| 14 | +use Magento\Framework\Serialize\SerializerInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Data provider for bundled product options |
| 18 | + */ |
| 19 | +class BundleOptionDataProvider |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Data |
| 23 | + */ |
| 24 | + private $pricingHelper; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var SerializerInterface |
| 28 | + */ |
| 29 | + private $serializer; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Configuration |
| 33 | + */ |
| 34 | + private $configuration; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param Data $pricingHelper |
| 38 | + * @param SerializerInterface $serializer |
| 39 | + * @param Configuration $configuration |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + Data $pricingHelper, |
| 43 | + SerializerInterface $serializer, |
| 44 | + Configuration $configuration |
| 45 | + ) { |
| 46 | + $this->pricingHelper = $pricingHelper; |
| 47 | + $this->serializer = $serializer; |
| 48 | + $this->configuration = $configuration; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Extract data for a bundled cart item |
| 53 | + * |
| 54 | + * @param Item $item |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function getData(Item $item): array |
| 58 | + { |
| 59 | + $options = []; |
| 60 | + $product = $item->getProduct(); |
| 61 | + |
| 62 | + /** @var \Magento\Bundle\Model\Product\Type $typeInstance */ |
| 63 | + $typeInstance = $product->getTypeInstance(); |
| 64 | + |
| 65 | + $optionsQuoteItemOption = $item->getOptionByCode('bundle_option_ids'); |
| 66 | + $bundleOptionsIds = $optionsQuoteItemOption |
| 67 | + ? $this->serializer->unserialize($optionsQuoteItemOption->getValue()) |
| 68 | + : []; |
| 69 | + |
| 70 | + if ($bundleOptionsIds) { |
| 71 | + /** @var \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection */ |
| 72 | + $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $product); |
| 73 | + |
| 74 | + $selectionsQuoteItemOption = $item->getOptionByCode('bundle_selection_ids'); |
| 75 | + |
| 76 | + $bundleSelectionIds = $this->serializer->unserialize($selectionsQuoteItemOption->getValue()); |
| 77 | + |
| 78 | + if (!empty($bundleSelectionIds)) { |
| 79 | + $selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $product); |
| 80 | + $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true); |
| 81 | + |
| 82 | + $options = $this->buildBundleOptions($bundleOptions, $item); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return $options; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Build bundle product options based on current selection |
| 91 | + * |
| 92 | + * @param \Magento\Bundle\Model\Option[] $bundleOptions |
| 93 | + * @param Item $item |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + private function buildBundleOptions(array $bundleOptions, Item $item): array |
| 97 | + { |
| 98 | + $options = []; |
| 99 | + foreach ($bundleOptions as $bundleOption) { |
| 100 | + if (!$bundleOption->getSelections()) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + $options[] = [ |
| 105 | + 'id' => $bundleOption->getId(), |
| 106 | + 'label' => $bundleOption->getTitle(), |
| 107 | + 'type' => $bundleOption->getType(), |
| 108 | + 'values' => $this->buildBundleOptionValues($bundleOption->getSelections(), $item), |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + return $options; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Build bundle product option values based on current selection |
| 117 | + * |
| 118 | + * @param Product[] $selections |
| 119 | + * @param Item $item |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + private function buildBundleOptionValues(array $selections, Item $item): array |
| 123 | + { |
| 124 | + $values = []; |
| 125 | + |
| 126 | + $product = $item->getProduct(); |
| 127 | + foreach ($selections as $selection) { |
| 128 | + $qty = (float) $this->configuration->getSelectionQty($product, $selection->getSelectionId()); |
| 129 | + if (!$qty) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + $selectionPrice = $this->configuration->getSelectionFinalPrice($item, $selection); |
| 134 | + |
| 135 | + $values[] = [ |
| 136 | + 'id' => $selection->getSelectionId(), |
| 137 | + 'label' => $selection->getName(), |
| 138 | + 'quantity' => $qty, |
| 139 | + 'price' => $this->pricingHelper->currency($selectionPrice, false, false), |
| 140 | + ]; |
| 141 | + } |
| 142 | + |
| 143 | + return $values; |
| 144 | + } |
| 145 | +} |
0 commit comments