Skip to content

Commit 9bcd0fc

Browse files
author
Prabhu Ram
committed
MC-20637: MyAccount :: Order Details :: Invoice Details by Order Number
- added impl for Bundled options
1 parent dde845c commit 9bcd0fc

File tree

9 files changed

+354
-147
lines changed

9 files changed

+354
-147
lines changed

app/code/Magento/SalesGraphQl/Model/Resolver/BundleOptions.php

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77

88
namespace Magento\SalesGraphQl\Model\Resolver;
99

10+
use Magento\Framework\Api\ExtensibleDataInterface;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
1213
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1314
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Framework\Serialize\Serializer\Json;
1618
use Magento\GraphQl\Model\Query\ContextInterface;
17-
use Magento\SalesGraphQl\Model\Resolver\OrderItem\DataProvider as OrderItemProvider;
19+
use Magento\Sales\Api\Data\LineItemInterface;
1820
use Magento\Sales\Api\Data\OrderItemInterface;
19-
use Magento\Sales\Api\Data\InvoiceItemInterface;
20-
use Magento\Framework\Serialize\Serializer\Json;
21-
use Magento\Framework\Api\ExtensibleDataInterface;
21+
use Magento\Sales\Model\Order;
22+
use Magento\SalesGraphQl\Model\Resolver\OrderItem\DataProvider as OrderItemProvider;
2223

2324
/**
2425
* Resolve bundle options items for order item
@@ -55,7 +56,6 @@ public function __construct(
5556
$this->valueFactory = $valueFactory;
5657
$this->orderItemProvider = $orderItemProvider;
5758
$this->serializer = $serializer;
58-
5959
}
6060

6161
/**
@@ -69,37 +69,40 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
}
7070

7171
return $this->valueFactory->create(function () use ($value) {
72-
if (!isset($value['model']) || !($value['model'] instanceof OrderItemInterface)) {
72+
if (!isset($value['model'])) {
7373
throw new LocalizedException(__('"model" value should be specified'));
7474
}
75-
/** @var ExtensibleDataInterface $item */
76-
$item = $value['model'];
77-
return $this->getBundleOptions($item);
75+
if ($value['model'] instanceof OrderItemInterface) {
76+
/** @var ExtensibleDataInterface $item */
77+
$item = $value['model'];
78+
return $this->getBundleOptions($item, null, null);
79+
}
80+
if ($value['model'] instanceof LineItemInterface) {
81+
/** @var LineItemInterface $item */
82+
$item = $value['model'];
83+
$lineItemToOrderItemMap = $value['line_item_to_order_item_map'];
84+
$order = $value['order'];
85+
// Have to pass down order and item to map to avoid refetching all data
86+
return $this->getBundleOptions($item->getOrderItem(), $order, $lineItemToOrderItemMap);
87+
}
88+
return null;
7889
});
7990
}
8091

81-
8292
/**
8393
* Format bundle options and values from a parent bundle order item
8494
*
8595
* @param ExtensibleDataInterface $item
8696
* @return array
8797
*/
88-
private function getBundleOptions(ExtensibleDataInterface $item): array
89-
{
98+
private function getBundleOptions(
99+
OrderItemInterface $item,
100+
Order $order = null,
101+
array $lineItemToOrderItemMap = null
102+
): array {
90103
$bundleOptions = [];
91104
if ($item->getProductType() === 'bundle') {
92-
$options = [];
93-
if ($item instanceof OrderItemInterface) {
94-
$options = $item->getProductOptions();
95-
} elseif ($item instanceof InvoiceItemInterface) {
96-
$orderItemArray = $this->orderItemProvider
97-
->getOrderItemById((int)$item->getOrderItemId());
98-
/** @var OrderItemInterface $orderItem */
99-
$orderItem = $orderItemArray['model'];
100-
$options = $orderItem->getProductOptions();
101-
}
102-
105+
$options = $item->getProductOptions();
103106
if (isset($options['bundle_options'])) {
104107
//loop through options
105108
foreach ($options['bundle_options'] as $bundleOptionKey => $bundleOption) {
@@ -108,19 +111,23 @@ private function getBundleOptions(ExtensibleDataInterface $item): array
108111
base64_encode($bundleOption['option_id']) : null;
109112
$bundleOptions[$bundleOptionKey]['items'] = [];
110113
foreach ($bundleOption['value'] ?? [] as $bundleOptionValueKey => $bundleOptionValue) {
111-
// Find the item assign to the option
114+
// Find the item assign to the option
112115
/** @var OrderItemInterface $childrenOrderItem */
113116
foreach ($item->getChildrenItems() ?? [] as $childrenOrderItem) {
114-
$childOrderItemOptions = $childrenOrderItem->getProductOptions();
115-
$bundleChildAttributes = $this->serializer
116-
->unserialize($childOrderItemOptions['bundle_selection_attributes']);
117-
// Value Id is missing from parent, so we have to match the child to parent option
118-
if (isset($bundleChildAttributes['option_id'])
119-
&& $bundleChildAttributes['option_id'] == $bundleOption['option_id']) {
120-
$bundleOptions[$bundleOptionKey]['items'][] = $this->orderItemProvider
121-
->getOrderItemById((int)$childrenOrderItem->getItemId());
122-
}
123-
}
117+
$childOrderItemOptions = $childrenOrderItem->getProductOptions();
118+
$bundleChildAttributes = $this->serializer
119+
->unserialize($childOrderItemOptions['bundle_selection_attributes']);
120+
// Value Id is missing from parent, so we have to match the child to parent option
121+
if (isset($bundleChildAttributes['option_id'])
122+
&& $bundleChildAttributes['option_id'] == $bundleOption['option_id']) {
123+
$bundleOptions[$bundleOptionKey]['item_ids'][] = $childrenOrderItem->getItemId();
124+
if ($lineItemToOrderItemMap) {
125+
$bundleOptions[$bundleOptionKey]['items'][] =
126+
$lineItemToOrderItemMap[$childrenOrderItem->getItemId()];
127+
}
128+
}
129+
}
130+
$bundleOptions[$bundleOptionKey]['order'] = $order;
124131
}
125132
}
126133
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\BundleOptions;
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\Sales\Model\Order;
18+
use Magento\SalesGraphQl\Model\Resolver\OrderItem\DataProvider as OrderItemProvider;
19+
20+
/**
21+
* Resolve order items for Bundle Options
22+
*/
23+
class SelectedBundleOptionItems implements ResolverInterface
24+
{
25+
/**
26+
* @var ValueFactory
27+
*/
28+
private $valueFactory;
29+
30+
/**
31+
* @var OrderItemProvider
32+
*/
33+
private $orderItemProvider;
34+
35+
/**
36+
* @param ValueFactory $valueFactory
37+
* @param OrderItemProvider $orderItemProvider
38+
*/
39+
public function __construct(
40+
ValueFactory $valueFactory,
41+
OrderItemProvider $orderItemProvider
42+
) {
43+
$this->valueFactory = $valueFactory;
44+
$this->orderItemProvider = $orderItemProvider;
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
51+
{
52+
/** @var ContextInterface $context */
53+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
54+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
55+
}
56+
if (!isset($value['item_ids'])) {
57+
throw new LocalizedException(__('"item_ids" value should be specified'));
58+
}
59+
60+
$orderItemIds = $value['item_ids'];
61+
foreach ($orderItemIds as $orderItemId) {
62+
$this->orderItemProvider->addOrderItemId((int)$orderItemId);
63+
}
64+
$itemsList = [];
65+
foreach ($orderItemIds as $orderItemId) {
66+
$itemsList[] = $this->valueFactory->create(
67+
function () use ($orderItemId) {
68+
return $this->orderItemProvider->getOrderItemById((int)$orderItemId);
69+
}
70+
);
71+
}
72+
return $itemsList;
73+
}
74+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\BundleOptions;
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\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\GraphQl\Model\Query\ContextInterface;
16+
17+
/**
18+
* Resolve line items for Bundle Options
19+
*/
20+
class SelectedBundleOptionLineItems implements ResolverInterface
21+
{
22+
/**
23+
* @inheritDoc
24+
*/
25+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
26+
{
27+
/** @var ContextInterface $context */
28+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
29+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
30+
}
31+
if (!isset($value['items'])) {
32+
throw new LocalizedException(__('"items" value should be specified'));
33+
}
34+
35+
$lineItems = $value['items'];
36+
$order = $value['order'];
37+
$resolvedData = [];
38+
foreach ($lineItems as $lineItem) {
39+
$resolvedData[] = [
40+
'product_name' => $lineItem->getName(),
41+
'product_sku' => $lineItem->getSku(),
42+
'product_sale_price' => [
43+
'value' => $lineItem->getPrice(),
44+
'currency' => $order->getOrderCurrency()
45+
],
46+
'quantity_invoiced' => $lineItem->getQty(),
47+
];
48+
}
49+
return $resolvedData;
50+
}
51+
}

app/code/Magento/SalesGraphQl/Model/Resolver/InvoiceItem.php

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)