Skip to content

[GraphQl] Add wishlist item to cart Implementation #31584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ private function getMappedVisibility(int $visibility): ?string

return isset($visibilityEnums[$visibility]) ? strtoupper($visibilityEnums[$visibility]) : null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);

namespace Magento\WishlistGraphQl\Model\CartItems;

use Magento\Wishlist\Model\Item;
use Magento\Framework\GraphQl\Query\Uid;

/**
* Data provider for bundlue product cart item request
*/
class BundleDataProvider implements CartItemsRequestDataProviderInterface
{
/**
* @var Uid
*/
private $uidEncoder;

/**
* @param Uid $uidEncoder
*/
public function __construct(
Uid $uidEncoder
) {
$this->uidEncoder = $uidEncoder;
}

/**
* @inheritdoc
*/
public function execute(Item $wishlistItem, ?string $sku): array
{
$buyRequest = $wishlistItem->getBuyRequest();
$selected_options = [];
if (isset($buyRequest['bundle_option'])) {
$bundleOptions = $buyRequest['bundle_option'];
$bundleOptionQty = $buyRequest['bundle_option_qty'];
foreach ($bundleOptions as $option => $value) {
$qty = $bundleOptionQty[$option];
$selected_options[] = $this->uidEncoder->encode("bundle/$option/$value/$qty");
}
}

$cartItems['selected_options'] = $selected_options;
return $cartItems;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);

namespace Magento\WishlistGraphQl\Model\CartItems;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Wishlist\Model\Item;

/**
* Building cart items request for add to cart form wishlist buy request
*/
class CartItemsRequestBuilder
{
/**
* @var CartItemsRequestDataProviderInterface[]
*/
private $providers;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @param ProductRepositoryInterface $productRepository
* @param array $providers
*/
public function __construct(
ProductRepositoryInterface $productRepository,
array $providers = []
) {
$this->productRepository = $productRepository;
$this->providers = $providers;
}

/**
* Build wishlist cart item request for adding to cart
*
* @param Item $wishlistItem
* @return array
*/
public function build(Item $wishlistItem): array
{
$product = $this->productRepository->getById($wishlistItem->getProductId());
$parentsku = $product->getSku();
$cartItems['quantity'] = floatval($wishlistItem->getQty());
$cartItems['sku'] = $parentsku;

foreach ($this->providers as $provider) {
$cartItems = array_merge_recursive($cartItems, $provider->execute($wishlistItem, $parentsku));
}
return $cartItems;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);

namespace Magento\WishlistGraphQl\Model\CartItems;

use Magento\Wishlist\Model\Item;

/**
* Build cart item request for adding products to cart
*/
interface CartItemsRequestDataProviderInterface
{
/**
* Provide cart item request from buy request to add wishlist items to cart
*
* @param Item $wishlistItem
* @param string $sku
*
* @return array
*/
public function execute(Item $wishlistItem, ?string $sku): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);

namespace Magento\WishlistGraphQl\Model\CartItems;

use Magento\Wishlist\Model\Item;
use Magento\Framework\GraphQl\Query\Uid;

/**
* Data provider for configurable product cart item request
*/
class ConfigurableDataProvider implements CartItemsRequestDataProviderInterface
{
/**
* @var Uid
*/
private $uidEncoder;

/**
* @param Uid $uidEncoder
*/
public function __construct(
Uid $uidEncoder
) {
$this->uidEncoder = $uidEncoder;
}

/**
* @inheritdoc
*/
public function execute(Item $wishlistItem, ?string $sku): array
{
$buyRequest = $wishlistItem->getBuyRequest();
$selected_options = [];
if (isset($buyRequest['super_attribute'])) {
$superAttributes = $buyRequest['super_attribute'];
foreach ($superAttributes as $attributeId => $value) {
$selected_options[] = $this->uidEncoder->encode("configurable/$attributeId/$value");
}
}
$cartItems['selected_options'] = $selected_options;
return $cartItems;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);

namespace Magento\WishlistGraphQl\Model\CartItems;

use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
use Magento\Wishlist\Model\Item;
use Magento\Framework\GraphQl\Query\Uid;

/**
* Data provider for custom options for cart item request
*/
class CustomizableOptionDataProvider implements CartItemsRequestDataProviderInterface
{
/**
* @var ProductCustomOptionRepositoryInterface
*/
private $productCustomOptionRepository;

/**
* @var Uid
*/
private $uidEncoder;

/**
* @param ProductCustomOptionRepositoryInterface $productCustomOptionRepository
* @param Uid $uidEncoder
*/
public function __construct(
ProductCustomOptionRepositoryInterface $productCustomOptionRepository,
Uid $uidEncoder
) {
$this->productCustomOptionRepository = $productCustomOptionRepository;
$this->uidEncoder = $uidEncoder;
}

/**
* @inheritdoc
*/
public function execute(Item $wishlistItem, ?string $sku): array
{
$buyRequest = $wishlistItem->getBuyRequest();
$options = isset($buyRequest['options'])?$buyRequest['options']:[];
$customOptions = $this->productCustomOptionRepository->getList($sku);
$selectedOptions = [];
$enteredOptions = [];
foreach ($customOptions as $customOption) {
$optionId = $customOption->getOptionId();

if (isset($options[$optionId])) {
$optionType = $customOption->getType();
if ($optionType === 'field' || $optionType === 'area' || $optionType === 'date') {
$enteredOptions[] = [
'uid' => $this->uidEncoder->encode("custom-option/$optionId"),
'value' => $options[$optionId],
];
} elseif ($optionType === 'drop_down') {
$optionSelectValues = $customOption->getValues();
$selectedOptions[] = $this->encodeSelectedOption(
(int) $customOption->getOptionId(),
(int) $options[$optionId]
);

} elseif ($optionType === 'multiple') {
foreach ($options[$optionId] as $multipleOption) {
$selectedOptions[] = $this->encodeSelectedOption(
(int) $customOption->getOptionId(),
(int) $multipleOption
);
}
}
}
}

$cartItems['selected_options'] = $selectedOptions;
$cartItems['entered_options'] = $enteredOptions;
return $cartItems;
}

/**
* Returns uid of the selected custom option
*
* @param int $optionId
* @param int $optionValueId
*
* @return string
*/
private function encodeSelectedOption(int $optionId, int $optionValueId): string
{
return $this->uidEncoder->encode("custom-option/$optionId/$optionValueId");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);

namespace Magento\WishlistGraphQl\Model\CartItems;

use Magento\Wishlist\Model\Item;
use Magento\Framework\GraphQl\Query\Uid;

/**
* Data provider for downloadable product links cart item request
*/
class DownloadableLinkDataProvider implements CartItemsRequestDataProviderInterface
{
/**
* @var Uid
*/
private $uidEncoder;

/**
* @param Uid $uidEncoder
*/
public function __construct(
Uid $uidEncoder
) {
$this->uidEncoder = $uidEncoder;
}

/**
* @inheritdoc
*/
public function execute(Item $wishlistItem, ?string $sku): array
{
$buyRequest = $wishlistItem->getBuyRequest();
$links = isset($buyRequest['links']) ? $buyRequest['links'] : [];
$selectedOptions = [];
$cartItems = [];
foreach ($links as $linkId) {
$selectedOptions[] = $this->uidEncoder->encode("downloadable/$linkId");
}
$cartItems['selected_options'] = $selectedOptions;
return $cartItems;
}
}
Loading