I want add surprise functionality so when custom try to place an order, one simple product will get automatically added to their cart. I want to add that item after customer click on place order button. To achieve that, I have used Observer checkout_submit_before and loading the quote id.
Here is my observer code. I have loaded the qupte and added my surprise item to the quote. Issue is, it's adding item in quote_item table but it's not adding item into order. In quote_item table I can see the new SKU for that particular Quote. I'm missing something here. Can anyone please guide me? I also tried to convert Quote Item to Order item using ToOrderItem class but no luck. I tried so many things for so many hours but still at the same point.
<?php
namespace Jackson\Surprise\Observer;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\Quote\Item\ToOrderItem as ToOrderItemConverter;
class Surprise implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var CartRepositoryInterface
*/
public $cartRepository;
private $productRepository;
private $quote;
/**
* @var ToOrderItemConverter
*/
protected $quoteItemToOrderItem;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
CartRepositoryInterface $cartRepository,
ToOrderItemConverter $quoteItemToOrderItem,
QuoteFactory $quote
){
$this->cartRepository = $cartRepository;
$this->quote = $quote;
$this->productRepository = $productRepository;
$this->quoteItemToOrderItem = $quoteItemToOrderItem;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylogfile.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("=========start1============");
$order = $observer->getEvent()->getQuote();
$quoteId = $order->getEntityId();
$logger->info($quoteId);
if ($quoteId) {
$logger->info("=========start2============");
$sku = 'LYLP';
$product = $this->productRepository->get($sku);
$cart = $this->quote->create()->loadActive($quoteId);
$cart->addProduct($product, $this->makeAddRequest($product, $sku, 1));
$cart->setItemsCount($cart->getItemsCount() + 1);
$cart->setItemsQty((float)$cart->getItemsQty() + 1);
$orderItems = [];
foreach ($cart->getAllItems() as $quoteItem) {
$itemId = $quoteItem->getId();
if (!empty($orderItems[$itemId])) {
continue;
}
$parentItemId = $quoteItem->getParentItemId();
/** @var \Magento\Quote\Model\ResourceModel\Quote\Item $parentItem */
if ($parentItemId && !isset($orderItems[$parentItemId])) {
$orderItems[$parentItemId] = $this->quoteItemToOrderItem->convert(
$quoteItem->getParentItem(),
['parent_item' => null]
);
}
$parentItem = isset($orderItems[$parentItemId]) ? $orderItems[$parentItemId] : null;
$orderItems[$itemId] = $this->quoteItemToOrderItem->convert($quoteItem, ['parent_item' => $parentItem]);
}
$cart->collectTotals();
$cart->save();
/* $cart = $this->cartRepository->getActiveForCustomer(8);
$cart->addProduct($product, 1);
// $quote->addProduct($product, $this->makeAddRequest($product, $sku, 1));
$cart->collectTotals();
$this->cartRepository->save($cart);
//$cart->save();
$quote = $this->cartRepository->getForCustomer(8);
$quote->addProduct($product, 1);
$quote->collectTotals();
$quote->save($quote);*/
$logger->info("=========save2============");
}
}
private function makeAddRequest(\Magento\Catalog\Model\Product $product, $sku = null, $qty = 1)
{
$data = [
'product' => $product->getEntityId(),
'qty' => $qty
];
$request = new \Magento\Framework\DataObject();
$request->setData($data);
return $request;
}
}