0

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
2
  • Please specify cartManagementInterface and cartRepositoryInterface er class name
    – Amit Bera
    Commented Mar 27, 2019 at 11:22
  • Please check updated edits , I mentioned total code in my post.
    – Dinesh
    Commented Mar 28, 2019 at 5:30

2 Answers 2

1

Try this:

$customer   = $this->_customerRepository->getById($customerId);
$quote      = $this->_quoteFactory->create();
$quote->setStoreId($storeId);
$quote->assignCustomer($customer);
3
  • Tried this code , But $quote->getItems() showing nothing.
    – Dinesh
    Commented Mar 27, 2019 at 10:37
  • you will also need to add products to quote programatically. then you will be able to use $quote->getItems(). Above code just creates a quote for particular customer Commented Mar 27, 2019 at 10:41
  • Yes i added products programmatically by calling $quote->addproduct($product,$qty);
    – Dinesh
    Commented Mar 27, 2019 at 12:18
0

I tried below code , Now i am able to add product to customer cart finely but it is showing price as 0.

 $store=$this->_storeManager->getStore();
                    $cartId = $this->cartManagementInterface->createEmptyCartForCustomer($poData->getPoCustomerId()); //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($poData->getPoCustomerId());
                    $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 = $this->_objectManager->create ( 'Magento\Catalog\Model\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";

I am getting the below response from post man when i access V1/carts/mine/items

[
    {
        "item_id": 815,
        "sku": "004444000033",
        "qty": 15,
        "name": "Women's Cotton Dress (PINK)",
        "price": 0,
        "product_type": "simple",
        "quote_id": "324",
        "extension_attributes": {
            "image_url": "/B/0/B07JDYS8VX_1.jpg"
        }
    }
]

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.