0

I'm creating rest api to add product to cart, it works okay for some users and doesn't for others.

what is reason for that?

here is the code:

$customer = null;
    $isGuest = true;
    $quote = null;
    $a = array();
    $minSaleQty = 0;

    if($this->isLoggedIn()){
        $customerId = $this->userContext->getUserId();
        $quote = $this->cartObj->getQuote()->loadByCustomer($customerId);



        $_product = $this->productRepository->getById($productId);
        $quoteId = $quote->getId();





        $stockItem = $this->stockRegistry->getStockItem($productId);
        $minQty = $stockItem->getMinSaleQty();
        if(!$minQty){
            $minQty = 1;
        }
        $qty = $this->product->getQty($_product);


        $quote->addProduct($_product,$minQty);  
        $quote->collectTotals()->save();
        $items = $quote->getAllItems();
        foreach($items as $item){
            $product = $this->productRepository->getById($item->getData()["product_id"]);
            $itemsData = array(
                "item_id" => $item->getData()["item_id"],
                "quote_id" => $item->getData()["quote_id"],
                "product_id" => (int)$item->getData()["product_id"],
                "sku" => $item->getData()["sku"],
                "name" => $item->getData()["name"],
                "qty" => $item->getData()["qty"],
                "price" => $item->getPrice() * $item->getQty(), 
                "image" => $this->getMediaUrl()."catalog/product/".$product->getImage() 
            );
        }

    }
    else{
        throw new AuthenticationException(__("not logged in user"));
    }

    return $a;

Your Answer

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

Browse other questions tagged or ask your own question.