Skip to content

Commit

Permalink
Merge pull request #6656 from magento-honey-badgers/PWA-1303
Browse files Browse the repository at this point in the history
[honey] PWA-1303: [M2 Commerce] Gift card incorrectly applied to multiple orders using "free" payment method via graphql
  • Loading branch information
cpartica authored Mar 5, 2021
2 parents adc70c4 + 3530ceb commit 1b4ecb5
Show file tree
Hide file tree
Showing 6 changed files with 559 additions and 24 deletions.
34 changes: 32 additions & 2 deletions app/code/Magento/Bundle/Model/Product/BundleOptionDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Bundle\Model\Option;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\Framework\Pricing\Helper\Data;
use Magento\Framework\Serialize\SerializerInterface;

Expand All @@ -19,6 +20,11 @@
*/
class BundleOptionDataProvider
{
/**
* Option type name
*/
private const OPTION_TYPE = 'bundle';

/**
* @var Data
*/
Expand All @@ -34,19 +40,27 @@ class BundleOptionDataProvider
*/
private $configuration;

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

/**
* @param Data $pricingHelper
* @param SerializerInterface $serializer
* @param Configuration $configuration
* @param Uid $uidEncoder
*/
public function __construct(
Data $pricingHelper,
SerializerInterface $serializer,
Configuration $configuration
Configuration $configuration,
Uid $uidEncoder
) {
$this->pricingHelper = $pricingHelper;
$this->serializer = $serializer;
$this->configuration = $configuration;
$this->uidEncoder = $uidEncoder;
}

/**
Expand Down Expand Up @@ -100,8 +114,15 @@ private function buildBundleOptions(array $bundleOptions, ItemInterface $item):
continue;
}

$optionDetails = [
self::OPTION_TYPE,
$bundleOption->getOptionId()
];
$uidString = implode('/', $optionDetails);

$options[] = [
'id' => $bundleOption->getId(),
'uid' => $this->uidEncoder->encode($uidString),
'label' => $bundleOption->getTitle(),
'type' => $bundleOption->getType(),
'values' => $this->buildBundleOptionValues($bundleOption->getSelections(), $item),
Expand Down Expand Up @@ -130,10 +151,19 @@ private function buildBundleOptionValues(array $selections, ItemInterface $item)
continue;
}

$optionValueDetails = [
self::OPTION_TYPE,
$selection->getOptionId(),
$selection->getSelectionId(),
(int) $selection->getSelectionQty()
];
$uidString = implode('/', $optionValueDetails);

$selectionPrice = $this->configuration->getSelectionFinalPrice($item, $selection);
$values[] = [
'label' => $selection->getName(),
'id' => $selection->getSelectionId(),
'uid' => $this->uidEncoder->encode($uidString),
'label' => $selection->getName(),
'quantity' => $qty,
'price' => $this->pricingHelper->currency($selectionPrice, false, false),
];
Expand Down
14 changes: 12 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\PaymentMethodManagementInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
Expand Down Expand Up @@ -44,22 +45,30 @@ class PlaceOrder implements ResolverInterface
*/
private $checkCartCheckoutAllowance;

/**
* @var PaymentMethodManagementInterface
*/
private $paymentMethodManagement;

/**
* @param GetCartForUser $getCartForUser
* @param CartManagementInterface $cartManagement
* @param OrderRepositoryInterface $orderRepository
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
* @param PaymentMethodManagementInterface $paymentMethodManagement
*/
public function __construct(
GetCartForUser $getCartForUser,
CartManagementInterface $cartManagement,
OrderRepositoryInterface $orderRepository,
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
CheckCartCheckoutAllowance $checkCartCheckoutAllowance,
PaymentMethodManagementInterface $paymentMethodManagement
) {
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
$this->orderRepository = $orderRepository;
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
$this->paymentMethodManagement = $paymentMethodManagement;
}

/**
Expand All @@ -84,7 +93,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
}

try {
$orderId = $this->cartManagement->placeOrder($cart->getId());
$cartId = $cart->getId();
$orderId = $this->cartManagement->placeOrder($cartId, $this->paymentMethodManagement->get($cartId));
$order = $this->orderRepository->get($orderId);

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
use Magento\Wishlist\Model\Wishlist;
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
use Magento\Wishlist\Model\Wishlist\Data\Error;
use Magento\Wishlist\Model\Wishlist\Data\WishlistItemFactory;
use Magento\Wishlist\Model\Wishlist\UpdateProductsInWishlist as UpdateProductsInWishlistModel;
use Magento\Wishlist\Model\WishlistFactory;
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;
use Magento\WishlistGraphQl\Model\UpdateWishlistItem;

/**
* Update wishlist items resolver
*/
class UpdateProductsInWishlist implements ResolverInterface
{
/**
* @var UpdateProductsInWishlistModel
* @var UpdateWishlistItem
*/
private $updateProductsInWishlist;
private $updateWishlistItem;

/**
* @var WishlistDataMapper
Expand All @@ -55,20 +56,20 @@ class UpdateProductsInWishlist implements ResolverInterface
* @param WishlistResourceModel $wishlistResource
* @param WishlistFactory $wishlistFactory
* @param WishlistConfig $wishlistConfig
* @param UpdateProductsInWishlistModel $updateProductsInWishlist
* @param UpdateWishlistItem $updateWishlistItem
* @param WishlistDataMapper $wishlistDataMapper
*/
public function __construct(
WishlistResourceModel $wishlistResource,
WishlistFactory $wishlistFactory,
WishlistConfig $wishlistConfig,
UpdateProductsInWishlistModel $updateProductsInWishlist,
UpdateWishlistItem $updateWishlistItem,
WishlistDataMapper $wishlistDataMapper
) {
$this->wishlistResource = $wishlistResource;
$this->wishlistFactory = $wishlistFactory;
$this->wishlistConfig = $wishlistConfig;
$this->updateProductsInWishlist = $updateProductsInWishlist;
$this->updateWishlistItem = $updateWishlistItem;
$this->wishlistDataMapper = $wishlistDataMapper;
}

Expand All @@ -83,34 +84,32 @@ public function resolve(
array $args = null
) {
if (!$this->wishlistConfig->isEnabled()) {
throw new GraphQlInputException(__('The wishlist configuration is currently disabled.'));
throw new GraphQlInputException(__('The wishlist configuration is currently disabled'));
}

$customerId = $context->getUserId();

/* Guest checking */
if (null === $customerId || $customerId === 0) {
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
}

$wishlistId = ((int) $args['wishlistId']) ?: null;
$wishlist = $this->getWishlist($wishlistId, $customerId);
$wishlist = $this->getWishlist((int) $args['wishlistId'], $customerId);

if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) {
throw new GraphQlInputException(__('The wishlist was not found.'));
throw new GraphQlInputException(__('Could not find the specified wishlist'));
}

$wishlistItems = $args['wishlistItems'];
$wishlistItems = $this->getWishlistItems($wishlistItems, $wishlist);
$wishlistOutput = $this->updateProductsInWishlist->execute($wishlist, $wishlistItems);
$wishlistItems = $this->getWishlistItems($args['wishlistItems'], $wishlist);

if (count($wishlistOutput->getErrors()) !== count($wishlistItems)) {
$this->wishlistResource->save($wishlist);
foreach ($wishlistItems as $wishlistItem) {
$this->updateWishlistItem->execute($wishlistItem, $wishlist);
}

$wishlistOutput = $this->updateWishlistItem->prepareOutput($wishlist);

return [
'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist()),
'user_errors' => \array_map(
'user_errors' => array_map(
function (Error $error) {
return [
'code' => $error->getCode(),
Expand All @@ -133,7 +132,6 @@ function (Error $error) {
private function getWishlistItems(array $wishlistItemsData, Wishlist $wishlist): array
{
$wishlistItems = [];

foreach ($wishlistItemsData as $wishlistItemData) {
if (!isset($wishlistItemData['quantity'])) {
$wishlistItem = $wishlist->getItem($wishlistItemData['wishlist_item_id']);
Expand All @@ -149,7 +147,6 @@ private function getWishlistItems(array $wishlistItemsData, Wishlist $wishlist):
}
$wishlistItems[] = (new WishlistItemFactory())->create($wishlistItemData);
}

return $wishlistItems;
}

Expand Down
Loading

0 comments on commit 1b4ecb5

Please sign in to comment.