Magento2 – Fixing ‘Uncaught Error: Call to a Member Function setCustomPrice() on Boolean’ Error

addtocartmagento2productquoteitemshopping-cart

Here i am trying to add product to the cart using customerId and
productId without customer logged in, it's working fine but its not
adding custom Price to that product,please guide me if you have any
idea related it.

Error "Fatal Error: 'Uncaught Error: Call to a member function
setCustomPrice() on boolean" because function $this->getProductQuote($product); returning false.

 <?php
    namespace Vendor\Module\Model;

    use Vendor\Module\Api\CustomerServiceInterface;
    use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollection;
    use Magento\Catalog\Model\ProductFactory as ProductFactory;
    use Magento\Store\Model\StoreManagerInterface as StoreManager;

    class Customerservice implements CustomerServiceInterface
    {

        private $productCollection;

        private $ProductFactory;

        protected $_storeManager;

        protected $_product;

        protected $_customerserviceFactory;


        protected $_customerRepositoryInterface;

        protected $_quoteModel;


        protected $_productRepository;

        protected $_cartManagementInterface;

        protected $_cartRepositoryInterface;

        protected $_cart;

        protected $_customer;
        protected $checkoutSession;
        protected $customerSession;

        protected $formKey;   

        public function __construct(ProductCollection $productCollection,
        ProductFactory $ProductFactory,
        \Magento\Catalog\Model\Product $product,
        \Vendor\Module\Model\CustomerserviceFactory $customerserviceFactory,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
        \Magento\Quote\Model\Quote $quoteModel,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
        \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
        StoreManager $_storeManager,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Data\Form\FormKey $formKey)
        {

            $this->productCollection = $productCollection;
            $this->ProductFactory = $ProductFactory;
            $this->_storeManager = $_storeManager;
            $this->_customerserviceFactory = $customerserviceFactory;
            $this->_product = $product;
            $this->_customerRepositoryInterface = $customerRepositoryInterface;
            $this->_quoteModel                   = $quoteModel;
            $this->_productRepository            = $productRepository;
            $this->_cartManagementInterface = $cartManagementInterface;
            $this->_cartRepositoryInterface = $cartRepositoryInterface;
            $this->_cart = $cart;
            $this->_customer = $customer;
            $this->checkoutSession = $checkoutSession;
            $this->customerSession = $customerSession;

            $this->formKey = $formKey;
        }

        public function customerservice($params) {

            $customerId = 13;
            $productId = 82;   
            $customprice = 160;
            try {

                    $params = array(
                    'form_key' => $this->formKey->getFormKey(),
                    "product" => $productId,
                    "qty" => 1,
                    "price" => $customprice
                    ); 

                    $request = new \Magento\Framework\DataObject();
                    $request->setData($params); 
                    $customerData = $this->_customerRepositoryInterface->getById($customerId);
                    $quote_data    = $this->_quoteModel->loadByCustomer($customerData);

                    if (!$quote_data->getId()) {
                        $quote_data->setCustomer($customerData); //error line here
                        //$quote_data->setIsActive(1);
                        $quote_data->setStoreId($this->_storeManager->getStore()->getId());
                    }
                    $quote_data->setCustomer($customerData);
                    //$product = $this->_productRepository->getById($productId);
                    $product = $this->ProductFactory->create()->load($productId);
                    $quote_data->addProduct($product, $request);
                    $customerEmail = $customerData->getEmail();

                    $this->_customer->setWebsiteId(1); 
                    $customer = $this->_customer->loadByEmail($customerEmail); 
                    $this->customerSession->setCustomerAsLoggedIn($customer);

                    $product = $this->ProductFactory->create()->load($productId);

                    if ($this->customerSession->isLoggedIn()) {
                        $item = $this->getProductQuote($product);
                        $item->setCustomPrice($customPrice); // setting custom price
                        $item->setOriginalCustomPrice($customPrice);
                        $item->getProduct()->setIsSuperMode(true);
                        $this->_cart->save();
                    }
                        $quote_data->collectTotals()->save(); 
                    if($this->customerSession->getId()) {
                        $this->customerSession->logout();

                    }  
                    $quote_data->collectTotals()->save(); 
                    echo json_encode("product added");
                    } catch (\Exception $ex) {
                print_r($ex->getMessage()); exit;
            }

        }

        public function getProductQuote($product) {
            $quote = $this->checkoutSession->getQuote();        
            $cartItems = $quote->getItemByProduct($product);        
            return $cartItems;
        }


    }   

Best Answer

I guess you need to check if the addProduct() is adding the product to cart.

You can also try the following way

$quoteItem = $quote_data->addProduct($product, $request);
$quoteItem->setCustomPrice($customPrice);
$quoteItem->setOriginalCustomPrice($customPrice);