0

Scratching my head with this issue, and I just do not understand why this is happening in the first place. Made a custom REST API call to remove multiple items from the cart with one single API call from calling a similar REST API calls multiple time to prevent too much activity happening. I have the logged user cart working just fine, but somehow the guest cart is throwing me this error:

Class int[] does not exist

vendor\module\etc\webapi.xml

<!-- Works fine -->
<route url="/V1/custom/carts/mine/items/clear" method="PUT">
    <service class="Vendor\Module\Api\CartItemRepositoryInterface" method="clearAsUser" />

    <resources>
        <resource ref="self" />
    </resources>

    <data>
        <parameter name="cartId" force="true">%cart_id%</parameter>
    </data>
</route>

<!-- Does not work -->
<route url="/V1/custom/guest-carts/:cartId/items/clear" method="PUT">
    <service class="Vendor\Module\Api\CartItemRepositoryInterface" method="clearAsGuest" />

    <resources>
        <resource ref="anonymous" />
    </resources>
</route>

vendor\module\api\CartItemRepositoryInterface.php

/**
 * Removes an array of items from the specified cart.
 *
 * @param int $cartId
 * @param int[] $cartItems
 * @return bool
 */
public function clearAsUser($cartId, $cartItems);

/**
 * Removes an array of items from the specified cart.
 *
 * @param string $cartId
 * @param int[] $cartItems
 * @return bool
 */
public function clearAsGuest($cartId, $cartItems);

Request Data

{
    "cartItems": [
        0: 123456,
        1: 789012
    ]
}

EDIT

vendor\module\model\CartItemRepository.php

/**
 * @param \Magento\Quote\Api\CartItemRepositoryInterface $cartItemRepo
 * @param \Magento\Quote\Api\GuestCartItemRepositoryInterface $guestItemRepo
 */
public function __construct(
    \Magento\Quote\Api\CartItemRepositoryInterface $cartItemRepo,
    \Magento\Quote\Api\GuestCartItemRepositoryInterface $guestItemRepo
){
    $this->cartItemRepo = $cartItemRepo;
    $this->guestItemRepo = $guestItemRepo;

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logging.log');
    $this->logger = new \Zend\Log\Logger();
    $this->logger->addWriter($writer);
}

/**
 * {@inheritdoc}
 */
public function clearAsGuest($cartId, $cartItems)
{
    $this->logger->info('clearAsGuest: ' . $cartId);

    try{
        foreach($cartItems as $itemId){
            if(!$this->guestItemRepo->deleteById($cartId, $itemId)){
                throw new \Exception('Some items were unable to be deleted from your cart.');
            } else {
                $this->logger->info('Removed! ' . $itemId);
            }
        }   

        return true;
    } catch (\Exception $e){
        $this->logger->info('Error: ' . $e->getMessage());
        return false;
    }
}

I've tried the guest cart api without the array to see if it does recognize the guest-cart id, and it does recognize it without an issue, but when I try to send the array of integers it will give me the error.

  • It might be notation issue at your API code: Important! Here be timesinks! There are a few gotchas here that are hard to debug if you get them wrong: DO NOT use PHP7 scalar argument types or return types if you want to hook this into the REST API! Add PHPDoc annotations for all arguments and the return type to all methods! Use Fully Qualified Class Names in the PHPDoc block! .You can find an idea from vinai post magento.stackexchange.com/a/160617/4564 – Amit Bera Jun 12 '18 at 14:00
  • @AmitBera Thanks for the link to that question, but it still stands as to why is it that my guest version is not working while the logged version works just fine with almost identical code (the exception being the dependencies) and PHPDoc annotations. – Jeremy Trpka Jun 12 '18 at 14:19
0

in Webapi.xml

Pass method as POST instead of PUT and Its working fine in Postman.

Don't forget to Flush Magento Cache after doing this.

<route url="/V1/custom/guest-carts/:cartId/items/clear" method="POST">

enter image description here

  • That worked! But why? Why does it have to work as a POST call instead of a PUT call? There has to be something I am missing. Thanks again! – Jeremy Trpka Jun 13 '18 at 20:19
  • if we want to change, we need to use POST after sending the request in anonymous resource. PUT won't change the data after sending same request again – Vishnunath Jun 14 '18 at 13:55
0

Try to change and test

/**
* Removes an array of items from the specified cart.
*
* @param int $cartId
* @param int[] $cartItems
* @return bool
*/
public function clearAsGuest($cartId, $cartItems);

webapi.xml

  <!-- Does not work -->
  <route url="/V1/custom/guest-carts/:cartId/items/clear" method="PUT">
  <service class="Vendor\Module\Api\CartItemRepositoryInterface" method="clearAsGuest" />

<resources>
    <resource ref="anonymous" />
</resources>
<data>
    <parameter name="cartId" force="true">%cart_id%</parameter>
</data>
 </route>
  • Adding the data parameter did not make any changes as I got the same error. One thing to point out is that guest cart ids are string, not integers. – Jeremy Trpka Jun 12 '18 at 13:28
  • can you please provide the functionality of guest user? – Vishnunath Jun 12 '18 at 13:33
  • Added to main question body. – Jeremy Trpka Jun 12 '18 at 13:40

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.