0

I'm building rest api to dd product to cart but it doesn't work and returns quote = null.

here is my code

 /**
* @api
* @param int $productId
* @return \Magento\Quote\Model\Quote
*/
 public function addItem($productId){
    $customer = null;
    $isGuest = true;
    if($this->isLoggedIn()){
        $customerId = $this->userContext->getUserId();
        $isGuest = false;
    }             

    $_product = $this->productRepository->getById($productId); 
    $customerId =  $this->userContext->getUserId();
    $quote = $this->cartObj->getQuote()->loadByCustomer($customerId);
    $quote->setCustomerIsGuest($isGuest)
    $this->item->setProduct($_product);    
    $quote->addItem($this->item);  
    return $quote;
}


 /**
* @return bool
*/
public function isLoggedIn(){
    $customerId = $this->userContext->getUserId();
    $userType = $this->userContext->getUserType();
    if($userType == UserContextInterface::USER_TYPE_CUSTOMER){
        return true;
    }
    return false;
}
Yomna Mansour
  • 593
  • 7
  • 23
  • Hi yomna how are u? please try with https://magento.stackexchange.com/questions/190369/magento-2-programatically-add-product-to-cart/190371#190371 – Nagaraju Kasa Dec 11 '18 at 10:24
  • please let me know how you are going to use endpoint url? – Nagaraju Kasa Dec 11 '18 at 10:29
  • @NagarajuKasa I've tried the code in the link but it doesn't work also.

    regarding your question, I'll get product information and quote id

    – Yomna Mansour Dec 11 '18 at 10:35
  • have u checked and tried default magento code for adding products to cart api ....etc? – Nagaraju Kasa Dec 11 '18 at 10:38
  • yes, but it needs its parameter to be like this {"cartItem":{"sku":"pro","qty":1,"name":"pro","price":"200.0000","product_type":"simple","quote_id":7}} and I need to send them like this {"productId" : 8} – Yomna Mansour Dec 11 '18 at 10:42
  • Hi yomna mansour can u tell me what exact parameters you would like to pass from your custom REST API (or) please provide me demo end point url so that i will check and let u know – Nagaraju Kasa Dec 12 '18 at 06:43

0 Answers0