I want to set custom pricing for magento2 product through API . I googled for the same and tried to implement the solutions one by one. But no solution is worked out.
$cartId = $this->cartManagementInterface->createEmptyCartForCustomer($customer_id); //Create empty cart
$quote = $this->cartRepositoryInterface->get($cartId);
I am getting error on creating empty cart for customer.
"Warning: Illegal offset type in isset or empty in /var/www/html/mypro/vendor/magento/module-quote/Model/QuoteRepository.php on line 133"
Can any one help me on this? I am adding complete code that i am trying. Please help me on this.
class PoApi implements PosApiInterface
{
public function __construct(
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\ObjectManagerInterface $objectManager,
\My\RequestToQuote\Model\Quote $quote,
\My\RequestToQuote\Model\QuoteDetail $qdetails,
\My\RequestToQuote\Model\Po $po,
\My\Vincrequest\Model\Vpo $vpo,
\My\RequestToQuote\Model\PoDetail $podetails,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
\Magento\Framework\Registry $registry,
\Magento\Framework\UrlInterface $urlinterface,
\Magento\Customer\Api\CustomerRepositoryInterfaceFactory $customerRepositoryFactory
) {
$this->_request = $request;
$this->_objectManager = $objectManager;
$this->_quote = $quote;
$this->_quotedetails = $qdetails;
$this->_po = $po;
$this->_vpo = $vpo;
$this->_podetails = $podetails;
$this->_coreRegistry = $registry;
$this->_urlBuilder = $urlinterface;
$this->_customerRepository = $customerRepositoryFactory;
$this->cartRepositoryInterface=$cartRepositoryInterface;
$this->cartManagementInterface=$cartManagementInterface;
}
public function poToCart($poid)
{
try {
if (!isset($poid) || $poid == '' || empty($poid)) {
return "Po Id Is Required";
}
$po_incId = $poid;
$poData= $this->_po->load($po_incId, 'po_increment_id');
$status = $poData->getStatus();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->load($poData->getPoCustomerId());
$quote_id = $poData->getQquoteId();
// $quote = $this->quoteModel->loadByCustomer($customer_id);
if ($status == '1' || $status == '0') {
$poData->setData('status', '1');
$poData->save();
$podetail = $poData->getCollection()->addFieldToFilter('po_increment_id', $po_incId)->addFieldToFilter('status', '1')->getData();
if (sizeof($podetail)>0) {
$poProd=$this->_objectManager->create('My\Crequest\Model\PoDetail')->getCollection()->addFieldToFilter('po_id', $po_incId)->getData();
$cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart
$quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote
$quote->setStore($store);
// if you have allready buyer id then you can load customer directly
$customer= $this->customerRepository->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer); //Assign quote to customer
//add items in quote
foreach ($poProd as $item) {
$product=$this->_product->load($item['product_id']);
$product->setPrice($item['po_price']);
$quote->addProduct($product, intval($item['product_qty']));
}
$quote->save(); //Now Save quote and your quote is ready
return "Added Cart";
}
}
} catch (\Exception $ex) {
return $ex->getMessage();
}
}
}
When i tried to call this API. I am getting below error message.
Magento\Framework\Exception\CouldNotSaveException: Cannot create quote in /var/www/html/myproj/vendor/magento/module-quote/Model/QuoteManagement.php:239
Stack trace:
#0 /var/www/html/myproj/app/code/My/Crequest/Model/PoApi.php(316): Magento\Quote\Model\QuoteManagement->createEmptyCart()
#1 [internal function]: My\Crequest\Model\PoApi->poToCart('PO000101')
I tried another code as well by hitting API programmatically.
$store=$this->_storeManager->getStore();
$repository = $this->_objectManager->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
/** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
$customer = $repository->getById(2);
$customerId = $customerObj->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/customers/' . $customerId . '/carts',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
]
];
echo $quoteId = $this->_webApiCall($serviceInfo, ['customerId' => $customerId]);
But this is also giving an error like below.
Uncaught Error: Call to undefined method My\Crequest\Model\PoApi::_webApiCall() in
Best Answer
Try this: