pageFactory = $pageFactory; $this->messageManager = $messageManager; $this->resultFactory = $context->getResultFactory(); $this->inlineTranslation = $inlineTranslation; $this->escaper = $escaper; $this->transportBuilder = $transportBuilder; $this->logger = $helperContext->getLogger(); $this->carttHistoryFactory = $carttHistoryFactory; $this->carttDetailFactory = $carttDetailFactory; $this->cartItemFactory = $cartItemFactory; parent::__construct($context); } public function addCartHistory($itemsTotal, $customerId) { $cartHistory = $this->carttHistoryFactory->create()->addData([ 'items_total' => $itemsTotal, 'customer_id' => $customerId ])->save(); return $cartHistory->getId(); } private function clearCartItem($customerId) { $collection = $this->cartItemFactory->create()->getCollection(); $count = $collection->addFieldToSelect('id') ->addFieldToFilter('customer_id', ['eq' => $customerId]) ->walk('delete'); } public function addCartDetail($quoteHistoryId, $items) { foreach ($items as $item) { $data = $this->carttDetailFactory->create()->addData([ 'product_id' => $item['product_id'], 'name' => $item['name'], 'url' => $item['url'], 'image' => $item['image'], 'sku' => $item['sku'], 'qty' => $item['qty'], 'price' => $item['price'], 'customer_id' => $item['customer_id'], 'quote_history_id' => $quoteHistoryId ])->save(); } } public function getCartItems($customerId) { $collection = $this->cartItemFactory->create()->getCollection(); $data = $collection->addFieldToFilter('customer_id', ['eq' => $customerId]) ->setOrder('name','ASC') ->getData(); return $data; } public function execute() { $page = $this->resultFactory->create(ResultFactory::TYPE_PAGE); $block = $page->getLayout()->getBlock('ip_supply_cart_to_quote_request_quote'); $data = $this->getRequest()->getPostValue(); if (!empty($data)) { if (!array_key_exists("name", $data)) { $this->messageManager->addError(__("The field name is invalid")); } else if (!array_key_exists("company_name", $data)) { $this->messageManager->addError(__("The field company name is invalid")); } else if (!array_key_exists("email", $data)) { $this->messageManager->addError(__("The field email is invalid")); } else if (!array_key_exists("phone", $data)) { $this->messageManager->addError(__("The field phone is invalid")); } else if (!array_key_exists("inquiry", $data)) { $this->messageManager->addError(__("The field inquiry is invalid")); } else if (!array_key_exists("note", $data)) { $this->messageManager->addError(__("The field note is invalid")); }else { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface'); $email_contact = $scopeConfig->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE); $customerSession = $objectManager->create('Magento\Customer\Model\Session'); if ($customerSession->isLoggedIn()) { $items = $this->getCartItems($customerSession->getCustomer()->getId()); if (count($items) > 0) { $cartHistoryId = $this->addCartHistory(count($items), $customerSession->getCustomer()->getId()); if ($cartHistoryId > 0) { $this->addCartDetail($cartHistoryId, $items); } } $inquirys = array(); foreach ($items as $item) { array_push($inquirys, $item["sku"] . " : " . $item["qty"]); } $data["inquiry"] = implode(".",$inquirys); $this->clearCartItem($customerSession->getCustomer()->getId()); } try { $this->inlineTranslation->suspend(); $sender = [ 'name' => $this->escaper->escapeHtml($data["name"]), 'email' => $this->escaper->escapeHtml($data["email"]), ]; $transport = $this->transportBuilder ->setTemplateIdentifier('email_request_quote_template') ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, ] ) ->setTemplateVars([ 'name' => $data["name"], 'email' => $data["email"], 'company_name' => $data["company_name"], 'phone' => $data["phone"], 'inquiry' => str_replace(".","
",$data["inquiry"]), 'note' => $data["note"], ]) ->setFrom($sender) ->addTo($email_contact) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccess(__("Thank you. We'll get back to you very soon.")); } catch (\Exception $e) { $this->logger->debug($e->getMessage()); } } } return $page; } }