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;
}
6
  • Hi yomna how are u? please try with magento.stackexchange.com/questions/190369/… Dec 11, 2018 at 10:24
  • please let me know how you are going to use endpoint url? Dec 11, 2018 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 Dec 11, 2018 at 10:35
  • have u checked and tried default magento code for adding products to cart api ....etc? Dec 11, 2018 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} Dec 11, 2018 at 10:42

0

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.