I have created a custom module for create the customer programmatically and add some products to that newly created customer's cart.
my code is working and products are also added to that customer's cart.But somehow the product prices are set to zero.
please check my code!!!
<?php
/**
*
* Copyright © 2015 Detailcommerce. All rights reserved.
*/
namespace Mp\Accountmanager\Controller\Accountmanager;
class Amsave extends \Magento\Framework\App\Action\Action
{
protected $_messageManager;
protected $cart;
protected $product;
protected $resultPageFactory;
protected $customerRepositoryInterface;
protected $quoteModel;
protected $productRepository;
protected $storeManager;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Quote\Model\Quote $quoteModel,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Model\Product $product,
\Magento\Checkout\Model\Cart $cart) {
parent::__construct($context);
$this->_messageManager = $messageManager;
$this->storeManager = $storeManager;
$this->resultPageFactory = $resultPageFactory;
$this->cart = $cart;
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->quoteModel = $quoteModel;
$this->productRepository = $productRepository;
$this->product = $product;
}
public function execute()
{
$post = $this->getRequest()->getParams();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('\Magento\Framework\App\State');
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$websiteId = $storeManager->getWebsite()->getWebsiteId();
$store = $storeManager->getStore(); // Get Store ID
$storeId = $store->getStoreId();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customer_new = $customerFactory->create();
$customer_new->setWebsiteId($websiteId);
$customer_new->setEmail($post['customer_email']);
$customer_new->setFirstname($post['customer_contact_person']);
$customer_new->setLastname($post['customer_contact_person']);
$customer_new->setPassword($post['customer_email']);
$customer_new->save();
try {
$customer_id = $this->_customerRepositoryInterface->getById($customer_new->getId());
$quote = $this->quoteModel->loadByCustomer($customer_id);
if (!$quote->getId()) {
$quote->setCustomer($customer_id);
$quote->setIsActive(1);
$quote->setStoreId($this->storeManager->getStore()->getId());
}
if($customerSession->getProcuctIdsSm() != NULL)
{
$products_array = $customerSession->getProcuctIdsSm();
$products_array = explode(',', $products_array);
}
//I got product array like $products_array = array (0,1,2,3). and it is correct.
foreach ($products_array as $value)
{
// $product = $this->productRepository->getById($value);
//$quote->addProduct($product, 1);
$params = array(
'product' => $value,
'qty' => 2
);
$_product = $this->productRepository->getById($value);
$quote->addProduct($_product,1);
}
// $quote->save();
$quote->collectTotals()->save();
} catch (\Exception $e) {
echo $e->getMessage(); die;
}
$this->messageManager->addSuccessMessage(__("Thanks for filling form..."));
$this->_redirect('checkout/cart');
}
}
Please see this image!!
please help me!!!! your effort would be appreciated... Thanks
