99
1010use Magento \Checkout \Api \Data \TotalsInformationInterface ;
1111use Magento \Checkout \Api \TotalsInformationManagementInterface ;
12+ use Magento \Framework \App \ObjectManager ;
1213use Magento \Framework \Exception \NoSuchEntityException ;
1314use Magento \Framework \GraphQl \Config \Element \Field ;
1415use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
2021use Magento \QuoteGraphQl \Model \ErrorMapper ;
2122use Magento \QuoteGraphQl \Model \TotalsBuilder ;
2223use Psr \Log \LoggerInterface ;
24+ use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
2325
2426/**
2527 * Apply address and shipping method to totals estimate and return the quote
28+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2629 */
2730class EstimateTotals implements ResolverInterface
2831{
32+ /**
33+ * @var GetCartForUser
34+ */
35+ private GetCartForUser $ getCartForUser ;
36+
2937 /**
3038 * EstimateTotals Constructor
3139 *
@@ -36,6 +44,7 @@ class EstimateTotals implements ResolverInterface
3644 * @param AssignShippingMethodToCart $assignShippingMethodToCart
3745 * @param LoggerInterface $logger
3846 * @param TotalsBuilder $totalsBuilder
47+ * @param GetCartForUser|null $getCartForUser
3948 */
4049 public function __construct (
4150 private readonly MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
@@ -44,8 +53,10 @@ public function __construct(
4453 private readonly ErrorMapper $ errorMapper ,
4554 private readonly AssignShippingMethodToCart $ assignShippingMethodToCart ,
4655 private readonly LoggerInterface $ logger ,
47- private readonly TotalsBuilder $ totalsBuilder
56+ private readonly TotalsBuilder $ totalsBuilder ,
57+ ?GetCartForUser $ getCartForUser = null
4858 ) {
59+ $ this ->getCartForUser = $ getCartForUser ?? ObjectManager::getInstance ()->get (GetCartForUser::class);
4960 }
5061
5162 /**
@@ -54,19 +65,20 @@ public function __construct(
5465 public function resolve (Field $ field , $ context , ResolveInfo $ info , ?array $ value = null , ?array $ args = null )
5566 {
5667 $ input = $ args ['input ' ] ?? [];
68+ $ maskedCartId = $ input ['cart_id ' ];
5769
58- if (empty ($ input [ ' cart_id ' ] )) {
70+ if (empty ($ maskedCartId )) {
5971 throw new GraphQlInputException (__ ('Required parameter "cart_id" is missing ' ));
6072 }
6173
6274 try {
63- $ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ input [ ' cart_id ' ] );
75+ $ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ maskedCartId );
6476 } catch (NoSuchEntityException $ exception ) {
6577 throw new GraphQlInputException (
6678 __ (
6779 'Could not find a cart with ID "%masked_id" ' ,
6880 [
69- 'masked_id ' => $ input [ ' cart_id ' ]
81+ 'masked_id ' => $ maskedCartId
7082 ]
7183 ),
7284 $ exception ,
@@ -79,6 +91,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
7991 throw new GraphQlInputException (__ ('Required parameter "country_code" is missing ' ));
8092 }
8193
94+ $ currentUserId = $ context ->getUserId ();
95+ $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
96+ $ this ->getCartForUser ->execute ($ maskedCartId , $ currentUserId , $ storeId );
97+
8298 $ totalsInfo = $ this ->totalsBuilder ->execute ($ addressData , $ input ['shipping_method ' ] ?? []);
8399 $ this ->totalsInformationManagement ->calculate ($ cartId , $ totalsInfo );
84100 $ this ->updateShippingMethod ($ totalsInfo , $ cartId );
0 commit comments