0

Actually while adding product to the cart with product id and customer id, i am facing one error "We found an invalid request for adding product to quote." If anyone have any idea please guide me.

<?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;

    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)
    {
        $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;
    }

    public function customerservice($params) {


        $customerId = 13;
        $productId = 81;     
        try {
            $product = $this->ProductFactory->create()->load($productId);
            $params = array(
                        "product" => $productId,
                        'qty' => 1,
                        "price" => 160
                    ); 

            $request = new \Magento\Framework\DataObject();
            $request->setData($params); 
            $quoteId = $this->_cartManagementInterface->createEmptyCartForCustomer($customerId);
            $quote = $this->_cartRepositoryInterface->get($quoteId);
            $customer = $this->_customerRepositoryInterface->getById($customerId);
            $quote->assignCustomer($customer);
            $productAddToCart = $this->_productRepository->getById($productId);
            $quote->addProduct($productAddToCart, $request);
            $this->_cartRepositoryInterface->save($quote);
            $quote->collectTotals();
            //$quote->collectTotals()->save();


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

    }


}
3

1 Answer 1

0

use Magento\Framework\App\State as AppState; protected $_appState; public function __construct(AppState $appState){ $this->_appState = $appState;
}

$this->_appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL); Missing set Area

Hope help.

Your Answer

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

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