cancel
Showing results for 
Search instead for 
Did you mean: 

Bundle product in rest API add to cart carts/mine/items

Bundle product in rest API add to cart carts/mine/items

Magento version 2.3.4

I am referring this document for adding bundle products in REST API: https://devdocs.magento.com/guides/v2.3/rest/tutorials/orders/order-add-items.html

 

I am searching for the "option_id" and "option_selections" in the back-end. I dont seems to be getting that. Do I need to write a function to get those details and pass it in array?

2 REPLIES 2

Re: Bundle product in rest API add to cart carts/mine/items

Hi @sathyashrayan 

 

Yes those option_id and option_selections - you will not find in the backend as that is specifically used for API and database purposes only.

 

So let's say if you would like to add bundle product into the cart - first, you need to get that product using get Method then you will get to know option_id about the single item from the bundle product.

 

So when you add the same into the cart you need to add bundle product SKU and then the options ID which you would like to add into the cart that way it will work.

 

For programmatically and dynamically yes you will require to customize to get all those option_ids and option_selections.

 

I hope it helps!

if issue solved,Click Kudos & Accept as Solution

Re: Bundle product in rest API add to cart carts/mine/items

Hi @Manthan Dave 

 

Thanks for the reply. Below is my code for getting the option Id. I am getting option_id as 1. I am not sure if that is correct. Also I need to know how to get the option_selections details.

 

namespace <vendor>\<module>\Model;

use <vendor>\<module>\Api\CartItemRepositoryInterface;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Bundle\Api\Data\OptionInterface;
use Magento\Bundle\Api\ProductOptionRepositoryInterface;

class CartItemRepository implements CartItemRepositoryInterface
{
    private $productOptionRepo;
    
    public function __construct(
        LoggerInterface $logger,
        ProductOptionRepositoryInterface $productOptionRepo,
        OptionInterface $productOptionType
        ) {
            $this->logger = $logger;
            $this->productOptionRepo = $productOptionRepo;
            $this->productOptionType = $productOptionType;
    }
    
    /**
     * Save Cart
     * @param <vendor>\<module>\Api\Data\CartItemRepository $cartItem
     * @return <vendor>\<module>\Api\Data\CartItemRepository[]
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function getOptions(
       <vendor>\<module>\Api\Data\CartItemRepository $cartItem
        )
    {
        $itemData = [];
        $sku = $cartItem->getSku();
        
        try {
            $items = $this->productOptionRepo->getList($sku);
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
            throw new Exception($exception->getMessage());
        }
        foreach ($items as $item) {
            $itemData[] = $item->getData();
        }
        
        return $itemData;
    }
    
}