Skip to content

Commit 3e3646a

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'origin/PWA-1379' into 31660_Config_addToCart
2 parents 96da9c6 + 665421f commit 3e3646a

File tree

16 files changed

+1095
-10
lines changed

16 files changed

+1095
-10
lines changed

app/code/Magento/WishlistGraphQl/Mapper/WishlistDataMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ private function getMappedVisibility(int $visibility): ?string
6666

6767
return isset($visibilityEnums[$visibility]) ? strtoupper($visibilityEnums[$visibility]) : null;
6868
}
69-
}
69+
}
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\WishlistGraphQl\Model\CartItems;
9+
10+
use Magento\Wishlist\Model\Item;
11+
use Magento\Framework\GraphQl\Query\Uid;
12+
13+
/**
14+
* Data provider for bundlue product cart item request
15+
*/
16+
class BundleDataProvider implements CartItemsRequestDataProviderInterface
17+
{
18+
/**
19+
* @var Uid
20+
*/
21+
private $uidEncoder;
22+
23+
/**
24+
* @param Uid $uidEncoder
25+
*/
26+
public function __construct(
27+
Uid $uidEncoder
28+
) {
29+
$this->uidEncoder = $uidEncoder;
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function execute(Item $wishlistItem, ?string $sku): array
36+
{
37+
$buyRequest = $wishlistItem->getBuyRequest();
38+
$selected_options = [];
39+
if (isset($buyRequest['bundle_option'])) {
40+
$bundleOptions = $buyRequest['bundle_option'];
41+
$bundleOptionQty = $buyRequest['bundle_option_qty'];
42+
foreach ($bundleOptions as $option => $value) {
43+
$qty = $bundleOptionQty[$option];
44+
$selected_options[] = $this->uidEncoder->encode("bundle/$option/$value/$qty");
45+
}
46+
}
47+
48+
$cartItems['selected_options'] = $selected_options;
49+
return $cartItems;
50+
}
51+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\WishlistGraphQl\Model\CartItems;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Wishlist\Model\Item;
12+
13+
/**
14+
* Building cart items request for add to cart form wishlist buy request
15+
*/
16+
class CartItemsRequestBuilder
17+
{
18+
/**
19+
* @var CartItemsRequestDataProviderInterface[]
20+
*/
21+
private $providers;
22+
23+
/**
24+
* @var ProductRepositoryInterface
25+
*/
26+
private $productRepository;
27+
28+
/**
29+
* @param ProductRepositoryInterface $productRepository
30+
* @param array $providers
31+
*/
32+
public function __construct(
33+
ProductRepositoryInterface $productRepository,
34+
array $providers = []
35+
) {
36+
$this->productRepository = $productRepository;
37+
$this->providers = $providers;
38+
}
39+
40+
/**
41+
* Build wishlist cart item request for adding to cart
42+
*
43+
* @param Item $wishlistItem
44+
* @return array
45+
*/
46+
public function build(Item $wishlistItem): array
47+
{
48+
$product = $this->productRepository->getById($wishlistItem->getProductId());
49+
$parentsku = $product->getSku();
50+
$cartItems['quantity'] = floatval($wishlistItem->getQty());
51+
$cartItems['sku'] = $parentsku;
52+
53+
foreach ($this->providers as $provider) {
54+
$cartItems = array_merge_recursive($cartItems, $provider->execute($wishlistItem, $parentsku));
55+
}
56+
return $cartItems;
57+
}
58+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\WishlistGraphQl\Model\CartItems;
9+
10+
use Magento\Wishlist\Model\Item;
11+
12+
/**
13+
* Build cart item request for adding products to cart
14+
*/
15+
interface CartItemsRequestDataProviderInterface
16+
{
17+
/**
18+
* Provide cart item request from buy request to add wishlist items to cart
19+
*
20+
* @param Item $wishlistItem
21+
* @param string $sku
22+
*
23+
* @return array
24+
*/
25+
public function execute(Item $wishlistItem, ?string $sku): array;
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\WishlistGraphQl\Model\CartItems;
9+
10+
use Magento\Wishlist\Model\Item;
11+
use Magento\Framework\GraphQl\Query\Uid;
12+
13+
/**
14+
* Data provider for configurable product cart item request
15+
*/
16+
class ConfigurableDataProvider implements CartItemsRequestDataProviderInterface
17+
{
18+
/**
19+
* @var Uid
20+
*/
21+
private $uidEncoder;
22+
23+
/**
24+
* @param Uid $uidEncoder
25+
*/
26+
public function __construct(
27+
Uid $uidEncoder
28+
) {
29+
$this->uidEncoder = $uidEncoder;
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function execute(Item $wishlistItem, ?string $sku): array
36+
{
37+
$buyRequest = $wishlistItem->getBuyRequest();
38+
$selected_options = [];
39+
if (isset($buyRequest['super_attribute'])) {
40+
$superAttributes = $buyRequest['super_attribute'];
41+
foreach ($superAttributes as $attributeId => $value) {
42+
$selected_options[] = $this->uidEncoder->encode("configurable/$attributeId/$value");
43+
}
44+
}
45+
$cartItems['selected_options'] = $selected_options;
46+
return $cartItems;
47+
}
48+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\WishlistGraphQl\Model\CartItems;
9+
10+
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
11+
use Magento\Wishlist\Model\Item;
12+
use Magento\Framework\GraphQl\Query\Uid;
13+
14+
/**
15+
* Data provider for custom options for cart item request
16+
*/
17+
class CustomizableOptionDataProvider implements CartItemsRequestDataProviderInterface
18+
{
19+
/**
20+
* @var ProductCustomOptionRepositoryInterface
21+
*/
22+
private $productCustomOptionRepository;
23+
24+
/**
25+
* @var Uid
26+
*/
27+
private $uidEncoder;
28+
29+
/**
30+
* @param ProductCustomOptionRepositoryInterface $productCustomOptionRepository
31+
* @param Uid $uidEncoder
32+
*/
33+
public function __construct(
34+
ProductCustomOptionRepositoryInterface $productCustomOptionRepository,
35+
Uid $uidEncoder
36+
) {
37+
$this->productCustomOptionRepository = $productCustomOptionRepository;
38+
$this->uidEncoder = $uidEncoder;
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function execute(Item $wishlistItem, ?string $sku): array
45+
{
46+
$buyRequest = $wishlistItem->getBuyRequest();
47+
$options = isset($buyRequest['options'])?$buyRequest['options']:[];
48+
$customOptions = $this->productCustomOptionRepository->getList($sku);
49+
$selectedOptions = [];
50+
$enteredOptions = [];
51+
foreach ($customOptions as $customOption) {
52+
$optionId = $customOption->getOptionId();
53+
54+
if (isset($options[$optionId])) {
55+
$optionType = $customOption->getType();
56+
if ($optionType === 'field' || $optionType === 'area' || $optionType === 'date') {
57+
$enteredOptions[] = [
58+
'uid' => $this->uidEncoder->encode("custom-option/$optionId"),
59+
'value' => $options[$optionId],
60+
];
61+
} elseif ($optionType === 'drop_down') {
62+
$optionSelectValues = $customOption->getValues();
63+
$selectedOptions[] = $this->encodeSelectedOption(
64+
(int) $customOption->getOptionId(),
65+
(int) $options[$optionId]
66+
);
67+
68+
} elseif ($optionType === 'multiple') {
69+
foreach ($options[$optionId] as $multipleOption) {
70+
$selectedOptions[] = $this->encodeSelectedOption(
71+
(int) $customOption->getOptionId(),
72+
(int) $multipleOption
73+
);
74+
}
75+
}
76+
}
77+
}
78+
79+
$cartItems['selected_options'] = $selectedOptions;
80+
$cartItems['entered_options'] = $enteredOptions;
81+
return $cartItems;
82+
}
83+
84+
/**
85+
* Returns uid of the selected custom option
86+
*
87+
* @param int $optionId
88+
* @param int $optionValueId
89+
*
90+
* @return string
91+
*/
92+
private function encodeSelectedOption(int $optionId, int $optionValueId): string
93+
{
94+
return $this->uidEncoder->encode("custom-option/$optionId/$optionValueId");
95+
}
96+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\WishlistGraphQl\Model\CartItems;
9+
10+
use Magento\Wishlist\Model\Item;
11+
use Magento\Framework\GraphQl\Query\Uid;
12+
13+
/**
14+
* Data provider for downloadable product links cart item request
15+
*/
16+
class DownloadableLinkDataProvider implements CartItemsRequestDataProviderInterface
17+
{
18+
/**
19+
* @var Uid
20+
*/
21+
private $uidEncoder;
22+
23+
/**
24+
* @param Uid $uidEncoder
25+
*/
26+
public function __construct(
27+
Uid $uidEncoder
28+
) {
29+
$this->uidEncoder = $uidEncoder;
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function execute(Item $wishlistItem, ?string $sku): array
36+
{
37+
$buyRequest = $wishlistItem->getBuyRequest();
38+
$links = isset($buyRequest['links']) ? $buyRequest['links'] : [];
39+
$selectedOptions = [];
40+
$cartItems = [];
41+
foreach ($links as $linkId) {
42+
$selectedOptions[] = $this->uidEncoder->encode("downloadable/$linkId");
43+
}
44+
$cartItems['selected_options'] = $selectedOptions;
45+
return $cartItems;
46+
}
47+
}

0 commit comments

Comments
 (0)