2

I've created custom Rest API for adding and removing products from cart.

I am able to add and delete products from my cart, but when I log in as a customer, I can't add or delete products from cart.

API throws message: Invalid state change requested.

How can I solve this?

3

Actually the custom REST API is throwing "Error message: Invalid state change requested" when customer does not pass the Authentication Token in headers. Magento 2 doesn't give access to Quote for users without authentication token.

| improve this answer | |
  • Good one. +1 for that :) – Shoaib Munir Sep 12 '19 at 7:47
  • @Prabhu M.: I have extended ShippingInformationManagement using preference and facing same error on normal checkout flow with new registered user. Can you plese help me. – DEEP JOSHI Feb 28 at 21:52
  • thanks one Up.. – 111 Apr 9 at 7:31
0
/**
 * Checks if change quote's customer id is allowed for current user.
 *
 * @param CartRepositoryInterface $subject
 * @param Quote $quote
 * @throws StateException if Guest has customer_id or Customer's customer_id not much with user_id
 * or unknown user's type
 * @return void
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */

Judging by the methods docs there may be issues with the Quote not matching or not allowed.

Checks if change quote's customer id is allowed for current user.

...

StateException if Guest has customer_id or Customer's customer_id not much with user_id or unknown user's type

| improve this answer | |
  • It is checking the below case in AccessChangeQuoteControl.php case UserContextInterface::USER_TYPE_GUEST: $isAllowed = ($quote->getCustomerId() === null); break; when i echo $quote->getCustomerId(), it returns me null, hence the above case breaks, $isAllowed variable is set to false and returns me message: Invalid state change requested. Why is my $quote->getCustomerId() is null? – Prabhu M. Mar 16 '18 at 7:37
  • If the context is hitting the USER_TYPE_GUEST then the customer hasn't been logged in properly over the API possibly. – B00MER Mar 19 '18 at 13:04
  • I checked it by creating new customer account too, but still the context is hitting USER_TYPE_GUEST when i try to add product using custom Rest Api . $params = array( 'form_key' => $this->_formkey->getFormKey(), 'product' => $productId, 'qty' =>$qty ); $this->_cart->addProduct($product_collection, $params); $this->_cart->save(); – Prabhu M. Mar 20 '18 at 12:02
-1

Please refer to the following https://github.com/myparcelnl/magento/issues/171#issuecomment-544443956 for a solution to this issue.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.