2

When adding product first time to the cart is not setting the product price, But when we add another product to the same cart (second time) the price of both product is set. my code is below:

public function createCustomerCar($customerId,$authorization,$sku,$qty)
{
    $result_arr = $this->_masterobj->getResultarray();
    $storeId = $this->storeManager->getStore()->getStoreId();

    try {
        $quote = $this->createCustomerCart($customerId, $storeId,$sku,$qty);
    } catch (\Exception $e) {
        $result_arr[$this->_masterobj->_json_keys["DATA_KEY"]]["message"] = $e->getMessage();
    }

}

/**
 * Creates a cart for the currently logged-in customer.
 *
 * @param int $customerId
 * @param int $storeId
 * @param string $sku
 * @param int $qty
 * @return \Magento\Quote\Model\Quote Cart object.
 * @throws CouldNotSaveException The cart could not be created.
 */
protected function createCustomerCart($customerId, $storeId,$sku,$qty)
{
    $result_arr = $this->_masterobj->getResultarray();
    $customer = $this->customerRepository->getById($customerId);

    try {
        $quote = $this->cartRepositoryInterface->getActiveForCustomer($customerId);
        // echo "err12";
        // exit;
    } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->QuoteFactory->create(); 
        $quote->setStoreId($storeId);
        $quote->setCustomer($customer);
        $quote->setCustomerIsGuest(0);
    }

    $this->cartItem->setQuoteId($quote->getId());
    $this->cartItem->setQty($qty);
    $this->cartItem->setSku($sku);

    $quoteItems = $quote->getItems();
    $quoteItems[] = $this->cartItem;
    $quote->setItems($quoteItems);

    $this->cartRepositoryInterface->save($quote);

    $quote->collectTotals();
}

0

Your Answer

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