3

How to save quote items in Magento 2? I have quote id. I'm able to store quote table data but I want to store products name, sku which is going in quote_item table.

I am able to set Data in quote table . $quote->setItemsqty($this->qty);

But i want to set Sku ,Name etc on quote_item table

Below is my Function on that function i am able to create quote

public function save(\Magento\Quote\Api\Data\CartInterface $quote)
{


    if ($quote->getId()) {

       $currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);


     foreach ($currentQuote->getData() as $key => $value) {

          if (!$quote->hasData($key)) {
                $quote->setData($key, $value);
            }
        }


    $quote->setItemscount(count($this->sku)); 
    $quote->setItemsqty($this->qty);


    }


    $this->getSaveHandler()->save($quote);

    $quoteItem = $this->quoteItemFactory->create();
    $quoteItem->setProduct($product);

    $quoteObj->addItem($quoteItem);
    $quoteObj->collectTotals()->save();


    $this->customlog->info($quoteItem->getData());

    unset($this->quotesById[$quote->getId()]);
    unset($this->quotesByCustomerId[$quote->getCustomerId()]);
}

How to do it? Any idea?

  • Your question makes me confused. quote_item table has already name, sku. – Khoa TruongDinh Oct 4 '16 at 12:26
  • yes its already have but i have quote id i want to save quote item data – magento12345 Oct 4 '16 at 12:32
  • Ah! You have a quote id, now, you want to load quote object add more quote items to this object? – Khoa TruongDinh Oct 4 '16 at 12:35
  • yes i have quote id now i want to add items – magento12345 Oct 4 '16 at 12:36
  • i updated my question – magento12345 Oct 4 '16 at 12:42
1

We can use setProduct method of quote item object.

Inject \Magento\Quote\Model\Quote\ItemFactory - \Magento\Quote\Api\Data\CartItemInterfaceFactory in your constructor.

/**
 * @var \Magento\Quote\Api\Data\CartItemInterfaceFactory
 */
protected $cartItemFactory;

/**
 * @var \Magento\Catalog\Api\ProductRepositoryInterface
 */
protected $product;

public function __construct(
    \Magento\Quote\Api\Data\CartItemInterfaceFactory $cartItemFactory,
    \Magento\Catalog\Api\ProductRepositoryInterface $product,
    ....
) {
    $this->cartItemFactory = $cartItemFactory;
    $this->product = $product;

}

In your method:

$product = $this->product->get($productSku);
$quoteItem = $this->cartItemFactory->create();
$quoteItem->setProduct($product);
$quote->addItem($quoteItem);
$quote->collectTotals()->save();

EDIT:

I used \Magento\Quote\Api\Data\CartItemInterfaceFactory instead of using \Magento\Quote\Model\Quote\ItemFactory

  • getting error ItemFactory does not exist' in D:\xamppnew\htdocs\magento2sapient5\lib\internal\Magento\Framework\Webapi\ErrorProcessor.php:195 – magento12345 Oct 4 '16 at 13:01
  • its not creating quote item row – magento12345 Oct 4 '16 at 13:06
  • { "message": "Cannot create quote" } – magento12345 Oct 4 '16 at 13:09
  • i updated my functions – magento12345 Oct 4 '16 at 13:16
  • Still getting the same error quote_item table is not creating row with my quote id then how come data will save i corrected whatever changes you send me but still no luck can you correct me what is going wrong – magento12345 Oct 5 '16 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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