Question

I having an API that has to call the /V1/carts/mine/items, no point calling it via curl, can I call the same directly ?

the webapi.xml says :

<route url="/V1/carts/mine/items" method="POST">
    <service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>

di.xml says:

<preference for="Magento\Quote\Api\CartItemRepositoryInterface" type="Magento\Quote\Model\Quote\Item\Repository" />

Now, the Magento\Quote\Model\Quote\Item\Repository says :

public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)

So you need to pass object the implements : \Magento\Quote\Api\Data\CartItemInterface

I have an array which the above call needs but obviously the call won't work.

and will say

Fatal Error: 'Uncaught TypeError: Argument 1 passed to Magento\Quote\Model\Quote\Item\Repository::save() must implement interface Magento\Quote\Api\Data\CartItemInterface, array given

What are my options ?

Was it helpful?

Solution

Take a look at a good example here: https://github.com/magento/magento2/blob/develop/dev/tests/integration/testsuite/Magento/Sales/_files/order_info.php#L58

You have to use Magento\Quote\Api\Data\CartItemInterfaceFactory to create a CartItem, then set data from the array you have and finally pass it to Magento\Quote\Api\CartItemRepositoryInterface::save

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top