1

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.

0

1 Answer 1

2

Instead of Magento\Customer\Model\Customer try Magento\Customer\Api\CustomerRepositoryInterface with getById() and then pass $customerData in $quote_data->setCustomer($customerData);

3
  • You most welcome Nov 14, 2018 at 10:47
  • one more thing : When I go to checkout/shipping page at first time, second product price not getting update in order summary block. It's just generate issue only first time. Nov 14, 2018 at 10:47
  • @Rakesh Varma, this code is not working inside model.i am getting one error while execution i.e. "We found an invalid request for adding product to quote." May 1, 2019 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.