Skip to content

Commit

Permalink
Merge pull request #8766 from magento-lynx/multicoupon-delivery
Browse files Browse the repository at this point in the history
[LYNX] Multicoupon
  • Loading branch information
sivaschenko authored Feb 20, 2024
2 parents 3428ebc + b07a843 commit 7e0fc6b
Show file tree
Hide file tree
Showing 33 changed files with 1,631 additions and 606 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,25 @@
use Magento\OfflineShipping\Model\SalesRule\ExtendedCalculator;
use Magento\Quote\Model\Quote\Address\FreeShippingInterface;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\Store\Model\StoreManagerInterface;

class FreeShipping implements FreeShippingInterface
{
/**
* @var ExtendedCalculator
* @var Calculator
*/
protected $calculator;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @param StoreManagerInterface $storeManager
* @param Calculator $calculator
*/
public function __construct(
StoreManagerInterface $storeManager,
Calculator $calculator
) {
$this->storeManager = $storeManager;
$this->calculator = $calculator;
}

/**
* {@inheritDoc}
* @inheritdoc
*/
public function isFreeShipping(\Magento\Quote\Model\Quote $quote, $items)
{
Expand All @@ -49,12 +40,7 @@ public function isFreeShipping(\Magento\Quote\Model\Quote $quote, $items)

$result = false;
$addressFreeShipping = true;
$store = $this->storeManager->getStore($quote->getStoreId());
$this->calculator->init(
$store->getWebsiteId(),
$quote->getCustomerGroupId(),
$quote->getCouponCode()
);
$this->calculator->initFromQuote($quote);
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setFreeShipping(0);
/** @var \Magento\Quote\Api\Data\CartItemInterface $item */
Expand Down Expand Up @@ -88,6 +74,8 @@ public function isFreeShipping(\Magento\Quote\Model\Quote $quote, $items)
}

/**
* Apply free shipping to quote item child items
*
* @param AbstractItem $item
* @param bool $isFreeShipping
* @return void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function processFreeShipping(\Magento\Quote\Model\Quote\Item\AbstractItem
$address = $item->getAddress();
$item->setFreeShipping(false);

foreach ($this->_getRules($address) as $rule) {
foreach ($this->getRules($address) as $rule) {
/* @var $rule \Magento\SalesRule\Model\Rule */
if (!$this->validatorUtility->canProcessRule($rule, $address)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Item;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,36 +21,41 @@
*/
class FreeShippingTest extends TestCase
{
/**
* @var int
*/
private static $websiteId = 1;

/**
* @var int
*/
private static $customerGroupId = 2;

/**
* @var int
*/
private static $couponCode = 3;

/**
* @var int
*/
private static $storeId = 1;

/**
* @var FreeShipping
*/
private $model;

/**
* @var MockObject|StoreManagerInterface
*/
private $storeManager;

/**
* @var MockObject|Calculator
*/
private $calculator;

protected function setUp(): void
{
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
$this->calculator = $this->createMock(Calculator::class);

$this->model = new FreeShipping(
$this->storeManager,
$this->calculator
);
}
Expand All @@ -74,8 +78,8 @@ public function testIsFreeShipping(int $addressFree, int $fItemFree, int $sItemF
$sItem = $this->getItem($quote);
$items = [$fItem, $sItem];

$this->calculator->method('init')
->with(self::$websiteId, self::$customerGroupId, self::$couponCode);
$this->calculator->method('initFromQuote')
->with($this->getQuote($this->getShippingAddress()));
$this->calculator->method('processFreeShipping')
->withConsecutive(
[$fItem],
Expand Down Expand Up @@ -118,9 +122,6 @@ private function withStore()
$store = $this->getMockBuilder(StoreInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->storeManager->method('getStore')
->with(self::$storeId)
->willReturn($store);

$store->method('getWebsiteId')
->willReturn(self::$websiteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
{
$this->_model = $this->createPartialMock(
Calculator::class,
['_getRules', '__wakeup']
['getRules', '__wakeup']
);
}

Expand All @@ -40,7 +40,7 @@ public function testProcessFreeShipping()
$item->expects($this->once())->method('getAddress')->willReturn($addressMock);

$this->_model->expects($this->once())
->method('_getRules')
->method('getRules')
->with($addressMock)
->willReturn([]);

Expand Down
60 changes: 0 additions & 60 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetCartProducts.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ private function getCartProduct(array $cartProductsIds): array
* @param Quote $cart
* @param int $pageSize
* @param int $offset
* @param string $orderBy
* @param string $order
* @return array
*/
public function execute(Quote $cart, int $pageSize, int $offset): array
public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy, string $order): array
{
$result = [];
if (!$cart->getId()) {
Expand All @@ -74,6 +76,7 @@ public function execute(Quote $cart, int $pageSize, int $offset): array
$itemCollection = $this->itemCollectionFactory->create()
->addFieldToFilter('parent_item_id', ['null' => true])
->addFieldToFilter('quote_id', $cart->getId())
->setOrder($orderBy, $order)
->setCurPage($offset)
->setPageSize($pageSize);

Expand Down
46 changes: 7 additions & 39 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,23 @@

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Query\Uid;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\QuoteGraphQl\Model\Cart\GetCartProducts;

/**
* @inheritdoc
*/
class CartItems implements ResolverInterface
{
/**
* @var GetCartProducts
*/
private $getCartProducts;

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

/**
* @param GetCartProducts $getCartProducts
* @param Uid $uidEncoder
*/
public function __construct(
GetCartProducts $getCartProducts,
Uid $uidEncoder
private readonly Uid $uidEncoder,
) {
$this->getCartProducts = $getCartProducts;
$this->uidEncoder = $uidEncoder;
}

/**
Expand All @@ -54,18 +39,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$cart = $value['model'];

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

/** @var QuoteItem $cartItem */
foreach ($cartItems as $cartItem) {
$productId = $cartItem->getProduct()->getId();
if (!isset($cartProductsData[$productId])) {
$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 = $cartProductsData[$productId];
$productData = $product->getData();
$productData['model'] = $product;
$productData['uid'] = $this->uidEncoder->encode((string) $product->getId());

$itemsData[] = [
'id' => $cartItem->getItemId(),
Expand All @@ -77,23 +64,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
}
return $itemsData;
}

/**
* Get product data for cart items
*
* @param Quote $cart
* @return array
*/
private function getCartProductsData(Quote $cart): array
{
$products = $this->getCartProducts->execute($cart);
$productsData = [];
foreach ($products as $product) {
$productsData[$product->getId()] = $product->getData();
$productsData[$product->getId()]['model'] = $product;
$productsData[$product->getId()]['uid'] = $this->uidEncoder->encode((string) $product->getId());
}

return $productsData;
}
}
Loading

0 comments on commit 7e0fc6b

Please sign in to comment.