#StackBounty: #magento2 #cart Magento 2.4.3 Remove products from cart

Bounty: 50

The customer has 500+ items in their cart. The "Remove All" button does not remove items from the cart.

The below code in resolve function is used to remove cart items

<?php
declare (strict_types = 1);

namespace VendorShippingRestrictionModelResolver;

use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkGraphQlConfigElementField;
use MagentoFrameworkGraphQlExceptionGraphQlInputException;
use MagentoFrameworkGraphQlExceptionGraphQlNoSuchEntityException;
use MagentoFrameworkGraphQlQueryResolverInterface;
use MagentoFrameworkGraphQlQueryResolverArgumentsProcessorInterface;
use MagentoFrameworkGraphQlSchemaTypeResolveInfo;
use MagentoQuoteGraphQlModelCartGetCartForUser;
use MagentoQuoteApiCartItemRepositoryInterface;
use MagentoQuoteModelMaskedQuoteIdToQuoteId;

/**
 * Class for remove items from cart
 */
class RemoveItemsFromCart implements ResolverInterface {
    /**
     * @var GetCartForUser
     */
    private $getCartForUser;

    /**
     * @var CartItemRepositoryInterface
     */
    private $cartItemRepository;

    /**
     * @var MaskedQuoteIdToQuoteId
     */
    private $maskedQuoteIdToQuoteId;

    /**
     * @var ArgumentsProcessorInterface
     */
    private $argsSelection;

    /**
     * @param GetCartForUser $getCartForUser
     * @param CartItemRepositoryInterface $cartItemRepository
     * @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
     * @param ArgumentsProcessorInterface $argsSelection
     */
    public function __construct(
        GetCartForUser $getCartForUser,
        CartItemRepositoryInterface $cartItemRepository,
        MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
        ArgumentsProcessorInterface $argsSelection
    ) {
        $this->getCartForUser = $getCartForUser;
        $this->cartItemRepository = $cartItemRepository;
        $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
        $this->argsSelection = $argsSelection;
    }

    /**
     * Remove multiple items from cart
     */
    public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) {
        $processedArgs = $this->argsSelection->process($info->fieldName, $args);
        if (empty($processedArgs['input']['cart_id'])) {
            throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
        }
        $maskedCartId = $processedArgs['input']['cart_id'];
        try {
            $cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
        } catch (NoSuchEntityException $exception) {
            throw new GraphQlNoSuchEntityException(
                __('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
            );
        }
        if (empty($processedArgs['input']['cart_items']["item_id"])) {
            throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing.'));
        }
        $items = $processedArgs['input']['cart_items']["item_id"];
        $storeId = (int) $context->getExtensionAttributes()->getStore()->getId();
        try {
            foreach ($items as $item){
                $this->cartItemRepository->deleteById($cartId, $item);
            }
        } catch (NoSuchEntityException $e) {
            throw new GraphQlNoSuchEntityException(__('The cart doesn't contain the item'));
        } catch (LocalizedException $e) {
            throw new GraphQlInputException(__($e->getMessage()), $e);
        }
        $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
        return [
            'cart' => [
                'model' => $cart,
            ],
        ];
    }
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.