Skip to content

Magento 2.4.6 Custom API end point add to users basket Invalid state change requested

I have created a custom API endpoint for adding products to a specific customers cart.
When running it through postman I get this error:

Invalid state change requested

This is my code:

declare(strict_types=1);
namespace SulmanTransferBasketModel;

use MagentoCatalogModelProductFactory;
use MagentoCustomerModelSession;
use MagentoQuoteApiCartManagementInterface;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoStoreModelStoreManagerInterface;
use MagentoCustomerModelCustomerFactory;
use SulmanTransferBasketApiTransferBasketInterface;

class TransferBasket implements TransferBasketInterface
{
    protected $productFactory;
    protected $quoteManagement;
    protected $quoteRepository;
    protected $_customerSession;
    protected $storemanager;
    protected $customerFactory;

    public function __construct(
        ProductFactory $productFactory,
        Session $customerSession,
        CartManagementInterface $quoteManagement,
        CartRepositoryInterface $quoteRepository,
        StoreManagerInterface $storemanager,
        CustomerFactory $customerFactory
    ) {
        $this->productFactory = $productFactory;
        $this->quoteManagement = $quoteManagement;
        $this->quoteRepository = $quoteRepository;
        $this->_customerSession = $customerSession;
        $this->storemanager = $storemanager;
        $this->customerFactory = $customerFactory;
    }

    public function addItemsToBasket($params)
    {
        
        $customerEmail = $params['customerEmail'];
        $websiteID = $this->storemanager->getStore()->getWebsiteId();
        $customer = $this->customerFactory->create()->setWebsiteId($websiteID)->loadByEmail($customerEmail);
        if (!$customer->getId()) {
            throw new MagentoFrameworkExceptionNoSuchEntityException(__('Customer not found.'));
        }
        $customerId = $customer->getId();
        $productRepository = MagentoFrameworkAppObjectManager::getInstance()->get(MagentoCatalogApiProductRepositoryInterface::class);

        $quoteId = $this->quoteManagement->createEmptyCartForCustomer($customerId);
        $quote = $this->quoteRepository->get($quoteId);
        $request = new MagentoFrameworkDataObject();
        
        foreach ($params['items'] as $item) {

            $product = $productRepository->get($item['sku']);
            $quote->addProduct($product, (int)$item['qty']);

            $params = [
                "product" => $product->getId(),
                "qty" => (int)$item['qty']
            ];

            $request->setData($params);
            $quote->addProduct($product, $request);
        }

        $this->quoteRepository->save($quote);
        $quote->collectTotals();

        return __('Items added to basket successfully.');
    }
}

If I run the same code in a standalone PHP file (boilerplate) it works correctly.

I have also applied the ACSD-48694 patch (which mentions Invalid state change requested)

I think it has to do with using the area code “webapi_rest” (which I can’t change as I am using the API).

Any ideas? Thanks.