|
| 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\SalesGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
| 13 | +use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; |
| 14 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 15 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 16 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 17 | +use Magento\SalesGraphQl\Model\Resolver\OrderItem\DataProvider as OrderItemProvider; |
| 18 | +use Magento\Sales\Api\Data\OrderItemInterface; |
| 19 | +use Magento\Framework\Serialize\Serializer\Json; |
| 20 | + |
| 21 | +/** |
| 22 | + * Resolve bundle options items for order item |
| 23 | + */ |
| 24 | +class BundleOptions implements ResolverInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Serializer |
| 28 | + * |
| 29 | + * @var Json |
| 30 | + */ |
| 31 | + private $serializer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ValueFactory |
| 35 | + */ |
| 36 | + private $valueFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var OrderItemProvider |
| 40 | + */ |
| 41 | + private $orderItemProvider; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param ValueFactory $valueFactory |
| 45 | + * @param OrderItemProvider $orderItemProvider |
| 46 | + * @param Json $serializer |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + ValueFactory $valueFactory, |
| 50 | + OrderItemProvider $orderItemProvider, |
| 51 | + Json $serializer |
| 52 | + ) { |
| 53 | + $this->valueFactory = $valueFactory; |
| 54 | + $this->orderItemProvider = $orderItemProvider; |
| 55 | + $this->serializer = $serializer; |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritDoc |
| 61 | + */ |
| 62 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 63 | + { |
| 64 | + /** @var ContextInterface $context */ |
| 65 | + if (false === $context->getExtensionAttributes()->getIsCustomer()) { |
| 66 | + throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->valueFactory->create(function () use ($value) { |
| 70 | + if (!isset($value['model']) || !($value['model'] instanceof OrderItemInterface)) { |
| 71 | + throw new LocalizedException(__('"model" value should be specified')); |
| 72 | + } |
| 73 | + /** @var OrderItemInterface $orderItem */ |
| 74 | + $orderItem = $value['model']; |
| 75 | + return $this->getBundleOptions($orderItem); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Format bundle options and values from a parent bundle order item |
| 82 | + * |
| 83 | + * @param OrderItemInterface $item |
| 84 | + * @return array |
| 85 | + */ |
| 86 | + private function getBundleOptions(OrderItemInterface $item): array |
| 87 | + { |
| 88 | + $bundleOptions = []; |
| 89 | + if ($item->getProductType() === 'bundle') { |
| 90 | + $options = $item->getProductOptions(); |
| 91 | + if (isset($options['bundle_options'])) { |
| 92 | + //loop through options |
| 93 | + foreach ($options['bundle_options'] as $bundleOptionKey => $bundleOption) { |
| 94 | + $bundleOptions[$bundleOptionKey]['label'] = $bundleOption['label'] ?? ''; |
| 95 | + $bundleOptions[$bundleOptionKey]['id'] = isset($bundleOption['option_id']) ? |
| 96 | + base64_encode($bundleOption['option_id']) : null; |
| 97 | + $bundleOptions[$bundleOptionKey]['items'] = []; |
| 98 | + foreach ($bundleOption['value'] ?? [] as $bundleOptionValueKey => $bundleOptionValue) { |
| 99 | + // Find the item assign to the option |
| 100 | + /** @var OrderItemInterface $childrenOrderItem */ |
| 101 | + foreach ($item->getChildrenItems() ?? [] as $childrenOrderItem) { |
| 102 | + $childOrderItemOptions = $childrenOrderItem->getProductOptions(); |
| 103 | + $bundleChildAttributes = $this->serializer |
| 104 | + ->unserialize($childOrderItemOptions['bundle_selection_attributes']); |
| 105 | + // Value Id is missing from parent, so we have to match the child to parent option |
| 106 | + if (isset($bundleChildAttributes['option_id']) |
| 107 | + && $bundleChildAttributes['option_id'] == $bundleOption['option_id']) { |
| 108 | + $bundleOptions[$bundleOptionKey]['items'][] = $this->orderItemProvider |
| 109 | + ->getOrderItemById((int)$childrenOrderItem->getItemId()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return $bundleOptions; |
| 117 | + } |
| 118 | +} |
0 commit comments