-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logic for setting shipping method is moved to a separate class
- Loading branch information
Showing
3 changed files
with
119 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingMethodsOnCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\Stdlib\ArrayManager; | ||
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; | ||
use Magento\QuoteGraphQl\Model\Cart\SetShippingMethodOnCart; | ||
|
||
/** | ||
* Class SetShippingMethodsOnCart | ||
* | ||
* Mutation resolver for setting shipping methods for shopping cart | ||
*/ | ||
class SetShippingMethodsOnCart implements ResolverInterface | ||
{ | ||
/** | ||
* @var SetShippingMethodOnCart | ||
*/ | ||
private $setShippingMethodOnCart; | ||
|
||
/** | ||
* @var ArrayManager | ||
*/ | ||
private $arrayManager; | ||
|
||
/** | ||
* @var GetCartForUser | ||
*/ | ||
private $getCartForUser; | ||
|
||
/** | ||
* @param ArrayManager $arrayManager | ||
* @param GetCartForUser $getCartForUser | ||
* @param SetShippingMethodOnCart $setShippingMethodOnCart | ||
*/ | ||
public function __construct( | ||
ArrayManager $arrayManager, | ||
GetCartForUser $getCartForUser, | ||
SetShippingMethodOnCart $setShippingMethodOnCart | ||
) { | ||
$this->arrayManager = $arrayManager; | ||
$this->getCartForUser = $getCartForUser; | ||
$this->setShippingMethodOnCart = $setShippingMethodOnCart; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$shippingMethods = $this->arrayManager->get('input/shipping_methods', $args); | ||
$maskedCartId = $this->arrayManager->get('input/cart_id', $args); | ||
|
||
if (!$maskedCartId) { | ||
throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); | ||
} | ||
if (!$shippingMethods) { | ||
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing')); | ||
} | ||
|
||
$shippingMethod = reset($shippingMethods); // This point can be extended for multishipping | ||
|
||
if (!$shippingMethod['cart_address_id']) { | ||
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing')); | ||
} | ||
if (!$shippingMethod['shipping_carrier_code']) { | ||
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing')); | ||
} | ||
if (!$shippingMethod['shipping_method_code']) { | ||
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing')); | ||
} | ||
|
||
$userId = $context->getUserId(); | ||
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId); | ||
|
||
$this->setShippingMethodOnCart->execute( | ||
$cart, | ||
$shippingMethod['cart_address_id'], | ||
$shippingMethod['shipping_carrier_code'], | ||
$shippingMethod['shipping_method_code'] | ||
); | ||
|
||
return [ | ||
'cart' => [ | ||
'cart_id' => $maskedCartId, | ||
'model' => $cart | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters