I need to add product to cart as per customer vise with custom price.
I'm trying by below code. But, it returns error
Uncaught TypeError: Argument 1 passed to Magento\Quote\Model\Quote::setCustomer() must implement interface Magento\Customer\Api\Data\CustomerInterface, instance of Magento\Customer\Model\Customer\Interceptor given, called in /media/vendor/ssd_drive/mage2/abcd/app/code/vendor/module/Controller/Index/test.php on line 133 and defined in /media/vendor/ssd_drive/mage2/abcd/vendor/magento/module-quote/Model/Quote.php:974
How to solve it ?
Controller code :
protected $_customer;
protected $quote;
protected $_customerSession;
protected $cart;
public function __construct(
\Magento\Customer\Model\Customer $customer,
\Magento\Customer\Model\Session $customerSession,
\Magento\Quote\Model\Quote $quote,
\Magento\Checkout\Model\Cart $cart,
) {
$this->_customer = $customer;
$this->_customerSession = $customerSession;
$this->quote = $quote;
$this->cart = $cart;
parent::__construct($context);
}
-----------------------------------------------------------------
$customerid = $this->_customerSession->getCustomer()->getId();
$customerData = $this->_customer->load($customerid);
$quote_data = $this->quote->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->addProduct($_products, $params);
$quote_data->collectTotals()->save();
$cart = $this->cart->getQuote()->getAllItems();
$subtotal = 0;
if ($cart) {
foreach ($cart as $item) {
if ($item->getProductId() == $productID) {
$subprice = $this->priceCurrency->currency($customprice, false, false);
$finalprice = round($subprice, 2);
$item->setCustomPrice($finalprice);
$item->setOriginalCustomPrice($finalprice);
$item->getProduct()->setIsSuperMode(true);
$item->save();
}
$subtotal += $item->getCustomPrice();
}
$quote_data->setSubtotal($subtotal);
$quote_data->setGrandTotal($subtotal);
$quote_data->save();
}
Note : I do this inside controller not using observer.