Skip to content

Commit

Permalink
Merge pull request #8798 from magento-lynx/multicoupon
Browse files Browse the repository at this point in the history
[LYNX] Multicoupon Admin UI
  • Loading branch information
sivaschenko authored Mar 1, 2024
2 parents 015b8dd + 7828f8b commit c26aca7
Show file tree
Hide file tree
Showing 27 changed files with 733 additions and 261 deletions.
64 changes: 64 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/CartItem/GetItemsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained from
* Adobe.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\CartItem;

use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\Quote\Api\Data\CartItemInterface;

class GetItemsData
{
/**
* @param Uid $uidEncoder
*/
public function __construct(
private readonly Uid $uidEncoder,
) {
}

/**
* Retrieve cart items data
*
* @param CartItemInterface[] $cartItems
* @return array
*/
public function execute(array $cartItems): array
{
$itemsData = [];
foreach ($cartItems as $cartItem) {
$product = $cartItem->getProduct();
if ($product === null) {
$itemsData[] = new GraphQlNoSuchEntityException(
__("The product that was requested doesn't exist. Verify the product and try again.")
);
continue;
}
$productData = $product->getData();
$productData['model'] = $product;
$productData['uid'] = $this->uidEncoder->encode((string) $product->getId());

$itemsData[] = [
'id' => $cartItem->getItemId(),
'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()),
'quantity' => $cartItem->getQty(),
'product' => $productData,
'model' => $cartItem,
];
}
return $itemsData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace Magento\QuoteGraphQl\Model\CartItem;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory as ItemCollectionFactory;

Expand All @@ -27,35 +25,13 @@
class GetPaginatedCartItems
{
/**
* @param ProductCollectionFactory $productCollectionFactory
* @param ItemCollectionFactory $itemCollectionFactory
*/
public function __construct(
private readonly ProductCollectionFactory $productCollectionFactory,
private readonly ItemCollectionFactory $itemCollectionFactory
) {
}

/**
* Get product models based on items in cart
*
* @param array $cartProductsIds
* @return ProductInterface[]
*/
private function getCartProduct(array $cartProductsIds): array
{
if (empty($cartProductsIds)) {
return [];
}
/** @var \Magento\Framework\Data\Collection $productCollection */
$productCollection = $this->productCollectionFactory->create()
->addAttributeToSelect('*')
->addIdFilter($cartProductsIds)
->setFlag('has_stock_status_filter', true);

return $productCollection->getItems();
}

/**
* Get visible cart items and product data for cart items
*
Expand All @@ -68,9 +44,11 @@ private function getCartProduct(array $cartProductsIds): array
*/
public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy, string $order): array
{
$result = [];
if (!$cart->getId()) {
return $result;
return [
'total' => 0,
'items' => []
];
}
/** @var \Magento\Framework\Data\Collection $itemCollection */
$itemCollection = $this->itemCollectionFactory->create()
Expand All @@ -81,20 +59,19 @@ public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy
->setPageSize($pageSize);

$items = [];
$cartProductsIds = [];
$itemDeletedCount = 0;
/** @var \Magento\Quote\Model\Quote\Item $item */
foreach ($itemCollection->getItems() as $item) {
if (!$item->isDeleted()) {
$items[] = $item;
$cartProductsIds[] = $item->getProduct()->getId();
} else {
$itemDeletedCount++;
}
}
$result['total'] = $itemCollection->getSize() - $itemDeletedCount;
$result['items'] = $items;
$result['products'] = $this->getCartProduct($cartProductsIds);
return $result;

return [
'total' => $itemCollection->getSize() - $itemDeletedCount,
'items' => $items
];
}
}
55 changes: 55 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/GetDiscounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained from
* Adobe.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Quote;

class GetDiscounts
{
/**
* Get Discount Values
*
* @param Quote $quote
* @param array $discounts
* @return array|null
* @throws LocalizedException
*/
public function execute(Quote $quote, array $discounts): ?array
{
if (empty($discounts)) {
return null;
}

$discountValues = [];
foreach ($discounts as $value) {
$discountData = $value->getDiscountData();
$discountValues[] = [
'label' => $value->getRuleLabel() ?: __('Discount'),
'applied_to' => $discountData->getAppliedTo(),
'amount' => [
'value' => $discountData->getAmount(),
'currency' => $quote->getQuoteCurrencyCode()
],
'discount_model' => $value,
'quote_model' => $quote
];
}

return $discountValues;
}
}
53 changes: 11 additions & 42 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,26 @@
use Magento\Quote\Model\Cart\Totals;
use Magento\Quote\Model\Quote\Item;
use Magento\QuoteGraphQl\Model\Cart\TotalsCollector;
use Magento\QuoteGraphQl\Model\GetDiscounts;

/**
* @inheritdoc
*/
class CartItemPrices implements ResolverInterface, ResetAfterRequestInterface
{
/**
* @var TotalsCollector
*/
private $totalsCollector;

/**
* @var Totals|null
*/
private $totals;

/**
* CartItemPrices constructor
*
* @param TotalsCollector $totalsCollector
* @param GetDiscounts $getDiscounts
*/
public function __construct(
TotalsCollector $totalsCollector
private readonly TotalsCollector $totalsCollector,
private readonly GetDiscounts $getDiscounts
) {
$this->totalsCollector = $totalsCollector;
}

/**
Expand Down Expand Up @@ -69,10 +64,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

/** calculate bundle product discount */
if ($cartItem->getProductType() == 'bundle') {
$discountValues = $this->getDiscountValues($cartItem, $currencyCode);
$discounts = $cartItem->getExtensionAttributes()->getDiscounts() ?? [];
$discountAmount = 0;
foreach ((array) $discountValues as $discountValue) {
$discountAmount += $discountValue['amount']['value'];
foreach ($discounts as $discount) {
$discountAmount += $discount->getDiscountData()->getAmount();
}
} else {
$discountAmount = $cartItem->getDiscountAmount();
Expand All @@ -99,36 +94,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
'currency' => $currencyCode,
'value' => $discountAmount,
],
'discounts' => $this->getDiscountValues($cartItem, $currencyCode)
'discounts' => $this->getDiscounts->execute(
$cartItem->getQuote(),
$cartItem->getExtensionAttributes()->getDiscounts() ?? []
)
];
}

/**
* Get Discount Values
*
* @param Item $cartItem
* @param string $currencyCode
* @return array|null
*/
private function getDiscountValues($cartItem, $currencyCode)
{
$itemDiscounts = $cartItem->getExtensionAttributes()->getDiscounts();
if ($itemDiscounts) {
$discountValues = [];
foreach ($itemDiscounts as $value) {
$discount = [];
$amount = [];
/* @var \Magento\SalesRule\Api\Data\DiscountDataInterface $discountData */
$discountData = $value->getDiscountData();
$discountAmount = $discountData->getAmount();
$discount['label'] = $value->getRuleLabel() ?: __('Discount');
$amount['value'] = $discountAmount;
$amount['currency'] = $currencyCode;
$discount['amount'] = $amount;
$discountValues[] = $discount;
}
return $discountValues;
}
return null;
}
}
30 changes: 6 additions & 24 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\QuoteGraphQl\Model\CartItem\GetItemsData;

/**
* @inheritdoc
Expand All @@ -22,9 +24,11 @@ class CartItems implements ResolverInterface
{
/**
* @param Uid $uidEncoder
* @param GetItemsData $getItemsData
*/
public function __construct(
private readonly Uid $uidEncoder,
private readonly GetItemsData $getItemsData
) {
}

Expand All @@ -36,32 +40,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var CartInterface $cart */
$cart = $value['model'];

$itemsData = [];
$cartItems = $cart->getAllVisibleItems();

/** @var QuoteItem $cartItem */
foreach ($cartItems as $cartItem) {
$product = $cartItem->getProduct();
if ($product === null) {
$itemsData[] = new GraphQlNoSuchEntityException(
__("The product that was requested doesn't exist. Verify the product and try again.")
);
continue;
}
$productData = $product->getData();
$productData['model'] = $product;
$productData['uid'] = $this->uidEncoder->encode((string) $product->getId());

$itemsData[] = [
'id' => $cartItem->getItemId(),
'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()),
'quantity' => $cartItem->getQty(),
'product' => $productData,
'model' => $cartItem,
];
}
return $itemsData;
return $this->getItemsData->execute($cartItems);
}
}
Loading

0 comments on commit c26aca7

Please sign in to comment.