init
This commit is contained in:
+148
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_QuickOrder
|
||||
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\QuickOrder\Controller\Index;
|
||||
|
||||
use \Magento\Framework\Exception\LocalizedException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Stemcell\Customer\Model\LabRepository;
|
||||
|
||||
class AddToCart extends \Magento\Framework\App\Action\Action
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Checkout\Model\Cart
|
||||
*/
|
||||
protected $cart;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Api\ProductRepositoryInterface
|
||||
*/
|
||||
protected $productRepository;
|
||||
protected LabRepository $labRepository;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Action\Context $context
|
||||
* @param \Magento\Checkout\Model\Cart $cart
|
||||
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Action\Context $context,
|
||||
\Magento\Checkout\Model\Cart $cart,
|
||||
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
|
||||
LabRepository $labRepository,
|
||||
protected LoggerInterface $logger
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->cart = $cart;
|
||||
$this->productRepository = $productRepository;
|
||||
$this->labRepository = $labRepository;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$result['status'] = false;
|
||||
$data = $this->getRequest()->getPostValue();
|
||||
// $this->logger->warning('$data', $data);
|
||||
$extensionAttributes = [];
|
||||
$storeId = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->getStore()->getId();
|
||||
if ($data && isset($data['items'])) {
|
||||
try {
|
||||
$valid = true;
|
||||
$shouldCollectTotals = false;
|
||||
foreach ($data['items'] as $index => $item) {
|
||||
if (!isset($item['product'])) {
|
||||
if (isset($item['name']) && $item['name']) {
|
||||
$this->messageManager->addError(__('Product %1 does not exist. Please remove and add it again.', $item['name']));
|
||||
} else {
|
||||
$this->messageManager->addError(__('Something went wrong while processing the request.'));
|
||||
}
|
||||
$valid = false;
|
||||
break;
|
||||
}
|
||||
$product = $this->productRepository->getById($item['product'], false, $storeId, true);
|
||||
$params = [];
|
||||
if (isset($item['options'])) {
|
||||
parse_str($item['options'], $params);
|
||||
}
|
||||
$extensionAttributes[$product->getId()] = $item['others'];
|
||||
|
||||
$params['qty'] = $item['qty'];
|
||||
$params['item_sort'] = $index + 1;
|
||||
try {
|
||||
$this->cart->addProduct($product, $params);
|
||||
} catch (\Exception $e) {
|
||||
$valid = false;
|
||||
$this->logger->warning('货号直购将产品加入购物车时异常,原因:'.$e->getMessage(), $e->getTrace());
|
||||
$this->messageManager->addError(__('Something went wrong while processing the request.'));
|
||||
}
|
||||
}
|
||||
$cart = $this->cart->save();
|
||||
if ($valid) {
|
||||
|
||||
$firstLab = false;
|
||||
$valid = false;
|
||||
foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
|
||||
$productId = $item->getProduct()->getId();
|
||||
if (isset($extensionAttributes[$productId]) && is_array($extensionAttributes[$productId])) {
|
||||
try {
|
||||
$lab = $this->labRepository->getByLabCode($extensionAttributes[$productId]['lab_code']);
|
||||
if ($lab->getLabId()) {
|
||||
$extensionAttributes[$productId]['lab_id'] = $lab->getLabId();
|
||||
if ($lab->getSalespersonId()) {
|
||||
$firstLab = $lab;
|
||||
}
|
||||
}
|
||||
} catch (LocalizedException $e) {
|
||||
$this->logger->warning(
|
||||
sprintf(
|
||||
'货号直购设置lab时异常,lab_code: %s, 原因:%s',
|
||||
$extensionAttributes[$productId]['lab_code'],
|
||||
$e->getMessage()
|
||||
),
|
||||
$e->getTrace()
|
||||
);
|
||||
}
|
||||
$percent = $extensionAttributes[$productId]['customer_discount_percent'] ?? 0;
|
||||
if (intval($percent) != 0) {
|
||||
$shouldCollectTotals = true;
|
||||
}
|
||||
$cart->updateItems([$item->getId()=>['extension_attributes'=>$extensionAttributes[$item->getProduct()->getId()]]]);
|
||||
$valid = true;
|
||||
}
|
||||
}
|
||||
if ($firstLab) {
|
||||
$cart->getQuote()->setData('salesperson_id', $firstLab->getSalespersonId());
|
||||
}
|
||||
// if ($valid) {
|
||||
// $this->_eventManager->dispatch('checkout_cart_save_after', ['cart' => $cart]);
|
||||
// $this->cart->save();
|
||||
// }
|
||||
if ($shouldCollectTotals) {
|
||||
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals();
|
||||
}
|
||||
$result['status'] = true;
|
||||
$result['redirectUrl'] = $this->_url->getUrl('checkout/cart');
|
||||
}
|
||||
} catch (LocalizedException $e) {
|
||||
$result['message'] = $e->getMessage();
|
||||
} catch (\Exception $e) {
|
||||
$result['message'] = __('Something went wrong while processing the request.');
|
||||
}
|
||||
}
|
||||
$this->getResponse()->representJson(
|
||||
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
namespace Magezon\QuickOrder\Controller\Index;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Checkout\Model\Cart as CustomerCart;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Filesystem\Directory\WriteInterface;
|
||||
use Magento\MediaStorage\Model\File\Uploader;
|
||||
use Magento\MediaStorage\Model\File\UploaderFactory;
|
||||
use Magezon\QuickOrder\Model\ProductManagement;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Stemcell\Customer\Model\LabRepository;
|
||||
|
||||
class BatchImport extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
|
||||
{
|
||||
private const UPLOAD_DIRECTORY = 'import/quickorder';
|
||||
private const MAX_UPLOAD_SIZE = 10485760;
|
||||
private const ALLOWED_MIME_TYPES = [
|
||||
'application/vnd.ms-excel',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'application/zip',
|
||||
'application/x-zip-compressed',
|
||||
'application/x-ole-storage',
|
||||
'application/octet-stream',
|
||||
];
|
||||
|
||||
private WriteInterface $mediaDirectory;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
protected ProductRepositoryInterface $productRepository,
|
||||
protected LabRepository $labRepository,
|
||||
protected ProductManagement $productManagement,
|
||||
protected LoggerInterface $logger,
|
||||
protected CustomerCart $cart,
|
||||
protected UploaderFactory $uploaderFactory,
|
||||
Filesystem $filesystem
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
try {
|
||||
$uploader = $this->getValidatedUploader();
|
||||
$uploadData = $uploader->validateFile();
|
||||
$spreadSheet = IOFactory::load($uploadData['tmp_name']);
|
||||
$data = $spreadSheet->getActiveSheet()->toArray('');
|
||||
|
||||
$quote = $this->cart->getQuote();
|
||||
if (!$quote->getId()) {
|
||||
// A persisted quote ID is required for the archive file name.
|
||||
$this->cart->save();
|
||||
$quote = $this->cart->getQuote();
|
||||
}
|
||||
|
||||
if (!$quote->getId()) {
|
||||
throw new LocalizedException(__('Unable to create a cart for the uploaded file.'));
|
||||
}
|
||||
|
||||
$this->saveUploadedFile(
|
||||
$spreadSheet,
|
||||
(int) $quote->getId(),
|
||||
strtolower($uploader->getFileExtension())
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->error('货号直购保存上传的 Excel 失败:' . $e->getMessage(), ['exception' => $e]);
|
||||
$message = $e instanceof LocalizedException
|
||||
? $e->getMessage()
|
||||
: __('The Excel file could not be uploaded. Please check the file and try again.');
|
||||
$this->messageManager->addErrorMessage($message);
|
||||
$this->getResponse()->representJson(json_encode(['result' => false]));
|
||||
return;
|
||||
}
|
||||
|
||||
array_shift($data);
|
||||
$salesperson = [];
|
||||
|
||||
$firstLab = $canSave = false;
|
||||
$orderRemark = '';
|
||||
$errors = [];
|
||||
foreach ($data as $index => $row) {
|
||||
|
||||
if (!$row['0']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$product = $this->productRepository->get($row['0'], false, null, true);
|
||||
$productData = $this->productManagement->getProductData($product);
|
||||
|
||||
$params = [];
|
||||
if ($row[8] && $orderRemark == '') {
|
||||
$orderRemark = $row[8];
|
||||
}
|
||||
if (!empty($productData['children'])) {
|
||||
parse_str($productData['options'], $params);
|
||||
}
|
||||
$qty = isset($row['1']) ? intval($row['1']) : 1;
|
||||
$other = ['qty' => $qty, 'cd' => $row[4], 'quote' => $row[5], 'customer_discount_percent' => $row[6], 'note' => $row[7]];
|
||||
$params = array_merge($params, $other);
|
||||
$product = $this->productRepository->getById($productData['id'], false, null, true);
|
||||
if (isset($row['3'])) {
|
||||
try {
|
||||
$lab = $this->labRepository->getByLabCode($row['3']);
|
||||
if ($lab->getLabId()) {
|
||||
$params['lab_id'] = $lab->getLabId();
|
||||
if ($lab->getSalespersonId()) {
|
||||
$firstLab = $lab;
|
||||
$salesperson[] = $lab->getSalespersonId();
|
||||
}
|
||||
}
|
||||
} catch (LocalizedException $e) {
|
||||
$this->logger->warning(
|
||||
sprintf('货号直购设置lab时异常,lab_code: %s, 原因:%s', $row['3'], $e->getMessage()),
|
||||
$e->getTrace()
|
||||
);
|
||||
}
|
||||
}
|
||||
$params['item_sort'] = $index+1;
|
||||
$this->cart->addProduct($product, $params);
|
||||
$canSave = true;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->critical(
|
||||
sprintf('货号直购通过上传excel加入购物车失败,原因是:%s请求数据:', $e->getMessage()), $row
|
||||
);
|
||||
$fields = ['产品号'=>0, 'Lab Id'=>3, 'CD No.' => 4];
|
||||
$string = '';
|
||||
foreach ($fields as $label => $index) {
|
||||
$string .= $label . ':' . $row[$index] . ', ';
|
||||
}
|
||||
$errors[] = $string .' 加入购物车失败, 原因:'.$e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
if (count(array_unique($salesperson)) > 1) {
|
||||
$errors[] = '请注意,您使用错误的销售信息,请检查您的数据';
|
||||
}
|
||||
|
||||
if ($firstLab && count(array_unique($salesperson)) == 1) {
|
||||
$this->cart->getQuote()->setData('salesperson_id', $firstLab->getSalespersonId());
|
||||
}
|
||||
|
||||
if ($orderRemark) {
|
||||
$this->cart->getQuote()->setData('customer_note', $orderRemark);
|
||||
}
|
||||
foreach ($errors as $error) {
|
||||
$this->messageManager->addError($error);
|
||||
}
|
||||
if ($canSave) {
|
||||
$this->cart->save();
|
||||
$this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals();
|
||||
}
|
||||
$this->getResponse()->representJson(
|
||||
json_encode(['result' => $canSave])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the upload before PhpSpreadsheet opens it.
|
||||
*/
|
||||
private function getValidatedUploader(): Uploader
|
||||
{
|
||||
$uploader = $this->uploaderFactory->create(['fileId' => 'file']);
|
||||
$uploader->setAllowedExtensions(['xls', 'xlsx']);
|
||||
$uploader->setAllowCreateFolders(true);
|
||||
$uploader->setAllowRenameFiles(false);
|
||||
$uploader->setFilesDispersion(false);
|
||||
$uploader->validateFile();
|
||||
|
||||
if ($uploader->getFileSize() > self::MAX_UPLOAD_SIZE) {
|
||||
throw new LocalizedException(__('The Excel file must not exceed 10 MB.'));
|
||||
}
|
||||
|
||||
if (!$uploader->checkMimeType(self::ALLOWED_MIME_TYPES)) {
|
||||
throw new LocalizedException(__('The uploaded file is not a valid Excel file.'));
|
||||
}
|
||||
|
||||
return $uploader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save as {quote_id}.{extension}, then {quote_id}-{n}.{extension}.
|
||||
*/
|
||||
private function saveUploadedFile(Spreadsheet $spreadSheet, int $quoteId, string $extension): string
|
||||
{
|
||||
$this->mediaDirectory->create(self::UPLOAD_DIRECTORY);
|
||||
|
||||
$lock = $this->mediaDirectory->openFile(self::UPLOAD_DIRECTORY . '/.upload.lock', 'a+');
|
||||
if (!$lock->lock()) {
|
||||
$lock->close();
|
||||
throw new LocalizedException(__('The uploaded Excel file could not be locked for saving.'));
|
||||
}
|
||||
|
||||
try {
|
||||
$sequence = 0;
|
||||
do {
|
||||
$stem = (string) $quoteId . ($sequence ? '-' . $sequence : '');
|
||||
$fileName = $stem . '.' . $extension;
|
||||
$sequence++;
|
||||
} while ($this->archiveNameExists($stem));
|
||||
|
||||
$spreadSheet->getProperties()->setCreated(time());
|
||||
$writer = IOFactory::createWriter($spreadSheet, $extension === 'xlsx' ? 'Xlsx' : 'Xls');
|
||||
$relativeFilePath = self::UPLOAD_DIRECTORY . '/' . $fileName;
|
||||
try {
|
||||
$writer->save($this->mediaDirectory->getAbsolutePath($relativeFilePath));
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->mediaDirectory->isExist($relativeFilePath)) {
|
||||
$this->mediaDirectory->delete($relativeFilePath);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->mediaDirectory->changePermissions(
|
||||
$relativeFilePath,
|
||||
0640
|
||||
);
|
||||
} finally {
|
||||
$lock->unlock();
|
||||
$lock->close();
|
||||
}
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Treat .xls and .xlsx files with the same stem as the same upload sequence.
|
||||
*/
|
||||
private function archiveNameExists(string $stem): bool
|
||||
{
|
||||
foreach (['xls', 'xlsx'] as $extension) {
|
||||
if ($this->mediaDirectory->isExist(self::UPLOAD_DIRECTORY . '/' . $stem . '.' . $extension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_QuickOrder
|
||||
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\QuickOrder\Controller\Index;
|
||||
|
||||
use Magento\Checkout\Model\Session as CheckoutSession;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Index extends \Magento\Framework\App\Action\Action
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\View\Result\PageFactory
|
||||
*/
|
||||
protected $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Controller\Result\ForwardFactory
|
||||
*/
|
||||
protected $resultForwardFactory;
|
||||
|
||||
/**
|
||||
* @var \Magezon\QuickOrder\Helper\Data
|
||||
*/
|
||||
protected $dataHelper;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Action\Context $context
|
||||
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
|
||||
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
|
||||
* @param \Magezon\QuickOrder\Helper\Data $dataHelper
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Action\Context $context,
|
||||
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
|
||||
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
|
||||
\Magezon\QuickOrder\Helper\Data $dataHelper,
|
||||
protected \Stemcell\Catalog\Helper\Product\View $productViewHelper,
|
||||
protected CheckoutSession $checkoutSession,
|
||||
protected LoggerInterface $logger,
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
$this->dataHelper = $dataHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\View\Result\Page
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
try {
|
||||
if (!$this->checkoutSession->getQuote()->getId()) {
|
||||
$newQuote = $this->checkoutSession->getQuote();
|
||||
$newQuote->setIsActive(true);
|
||||
$newQuote->save();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->critical('货号直购页面发现没有新的购物车, 尝试初始化新购物车时失败,原因:'. $e->getMessage());
|
||||
}
|
||||
|
||||
/** @var \Magento\Framework\View\Result\Page $resultPage */
|
||||
$resultPage = $this->resultPageFactory->create();
|
||||
$resultPage->getConfig()->getTitle()->set($this->dataHelper->getTitle());
|
||||
if (!$this->dataHelper->isEnabled() || !$this->dataHelper->isGroupValid() || !$this->productViewHelper->notStockCheckGroup()) {
|
||||
$resultForward = $this->resultForwardFactory->create();
|
||||
return $resultForward->forward('noroute');
|
||||
}
|
||||
return $resultPage;
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_QuickOrder
|
||||
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\QuickOrder\Controller\Index;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
|
||||
class Item extends \Magento\Framework\App\Action\Action
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\DataObject\Factory
|
||||
*/
|
||||
protected $objectFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Helper\Product\ConfigurationPool
|
||||
*/
|
||||
protected $configurationPool;
|
||||
|
||||
/**
|
||||
* @var \Magento\Quote\Model\Quote\Item\Processor
|
||||
*/
|
||||
protected $itemProcessor;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\LayoutFactory
|
||||
*/
|
||||
protected $layoutFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\ProductFactory
|
||||
*/
|
||||
protected $productFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Pricing\Helper\Data
|
||||
*/
|
||||
protected $pricingHelper;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Action\Context $context
|
||||
* @param \Magento\Framework\DataObject\Factory $objectFactory
|
||||
* @param \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool
|
||||
* @param \Magento\Quote\Model\Quote\Item\Processor $itemProcessor
|
||||
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
|
||||
* @param \Magento\Catalog\Model\ProductFactory $productFactory
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Action\Context $context,
|
||||
\Magento\Framework\DataObject\Factory $objectFactory,
|
||||
\Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool,
|
||||
\Magento\Quote\Model\Quote\Item\Processor $itemProcessor,
|
||||
\Magento\Framework\View\LayoutFactory $layoutFactory,
|
||||
\Magento\Catalog\Model\ProductFactory $productFactory,
|
||||
\Magento\Framework\Pricing\Helper\Data $pricingHelper
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->objectFactory = $objectFactory;
|
||||
$this->configurationPool = $configurationPool;
|
||||
$this->itemProcessor = $itemProcessor;
|
||||
$this->layoutFactory = $layoutFactory;
|
||||
$this->productFactory = $productFactory;
|
||||
$this->pricingHelper = $pricingHelper;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$result['status'] = false;
|
||||
$data = $this->getRequest()->getParams();
|
||||
$resultRedirect = $this->resultRedirectFactory->create();
|
||||
if ($data) {
|
||||
try {
|
||||
$product = $this->productFactory->create();
|
||||
$product->load($data['product']);
|
||||
$options = [];
|
||||
parse_str($data['options'], $options);
|
||||
if (isset($options['super_group']) && $options['super_group']) {
|
||||
$price = 0;
|
||||
$html = '<dl class="item-options">';
|
||||
foreach ($options['super_group'] as $productId => $qty) {
|
||||
if ($qty) {
|
||||
$product1 = $this->productFactory->create();
|
||||
$product1->load($productId);
|
||||
$buyeRequest = $this->objectFactory->create([
|
||||
'product' => $productId,
|
||||
'qty' => $qty
|
||||
]);
|
||||
$item = $this->getItem($product1, $buyeRequest);
|
||||
$html .= '<dt>' . $qty . ' x ' . $product1->getName() . '</dt>';
|
||||
$html .= '<dd>' . $this->pricingHelper->currency($item->getProduct()->getFinalPrice(), true, false) . '</dd>';
|
||||
$price += $item->getProduct()->getFinalPrice() * $qty;
|
||||
}
|
||||
}
|
||||
$html .= '<dl>';
|
||||
$result['price'] = $price;
|
||||
$result['options'] = $html;
|
||||
} else {
|
||||
$options = $this->objectFactory->create($options);
|
||||
$item = $this->getItem($product, $options);
|
||||
$result['price'] = $item->getProduct()->getFinalPrice();
|
||||
$optionList = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
|
||||
$layout = $this->layoutFactory->create();
|
||||
$block = $layout->createBlock('\Magento\Framework\View\Element\Template');
|
||||
$block->setTemplate('Magezon_QuickOrder::product/options.phtml')->setOptionList($optionList);
|
||||
$result['options'] = $block->toHtml();
|
||||
}
|
||||
$result['status'] = true;
|
||||
} catch (LocalizedException $e) {
|
||||
$result['message'] = $e->getMessage();
|
||||
} catch (\Exception $e) {
|
||||
$result['message'] = __('Something went wrong while processing the request.');
|
||||
}
|
||||
}
|
||||
$this->getResponse()->representJson(
|
||||
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Quote\Model\Quote\Item
|
||||
*/
|
||||
public function getItem($product, $buyRequest)
|
||||
{
|
||||
$candidate = $this->getCandidate($product, $buyRequest);
|
||||
$item = $this->itemProcessor->init($candidate, $buyRequest);
|
||||
$item->setOptions($candidate->getCustomOptions());
|
||||
$item->setProduct($product);
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getCandidate($product, $buyRequest)
|
||||
{
|
||||
$cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced($buyRequest, $product, \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL);
|
||||
|
||||
/**
|
||||
* Error message
|
||||
*/
|
||||
if (is_string($cartCandidates) || $cartCandidates instanceof \Magento\Framework\Phrase) {
|
||||
return strval($cartCandidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* If prepare process return one object
|
||||
*/
|
||||
if (!is_array($cartCandidates)) {
|
||||
$cartCandidates = [$cartCandidates];
|
||||
}
|
||||
|
||||
return $cartCandidates[0];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_QuickOrder
|
||||
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\QuickOrder\Controller\Index;
|
||||
|
||||
use Magento\Catalog\Helper\ImageFactory as HelperFactory;
|
||||
|
||||
class LoadChildrens extends \Magento\Framework\App\Action\Action
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
protected $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var HelperFactory
|
||||
*/
|
||||
protected $helperFactory;
|
||||
|
||||
/**
|
||||
* @var \Magezon\QuickOrder\Model\ProductManagement
|
||||
*/
|
||||
protected $productManagement;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\ProductFactory
|
||||
*/
|
||||
protected $productFactory;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Action\Context $context
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param HelperFactory $helperFactory
|
||||
* @param \Magezon\QuickOrder\Model\ProductManagement $productManagement
|
||||
* @param \Magento\Catalog\Model\ProductFactory $productFactory
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Action\Context $context,
|
||||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
|
||||
HelperFactory $helperFactory,
|
||||
\Magezon\QuickOrder\Model\ProductManagement $productManagement,
|
||||
\Magento\Catalog\Model\ProductFactory $productFactory
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->helperFactory = $helperFactory;
|
||||
$this->productManagement = $productManagement;
|
||||
$this->productFactory = $productFactory;
|
||||
}
|
||||
|
||||
private function usedGroupedImage()
|
||||
{
|
||||
return $this->scopeConfig->getValue(
|
||||
\Magento\GroupedProduct\Block\Cart\Item\Renderer\Grouped::CONFIG_THUMBNAIL_SOURCE,
|
||||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\Controller\Result\Raw
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$result['status'] = false;
|
||||
try {
|
||||
$post = $this->getRequest()->getPostValue();
|
||||
|
||||
if (isset($post['type']) && isset($post['id'])) {
|
||||
$childrens = [];
|
||||
$product = $this->productFactory->create();
|
||||
$product->load($post['id']);
|
||||
$helper = $this->helperFactory->create()->init($product, 'product_thumbnail_image');
|
||||
|
||||
switch ($post['type']) {
|
||||
case 'grouped':
|
||||
$associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
|
||||
foreach ($associatedProducts as $product) {
|
||||
$item = $this->productManagement->getProductData($product);
|
||||
if ($this->usedGroupedImage()) {
|
||||
$item['img'] = $helper->getUrl();
|
||||
}
|
||||
$childrens[] = $item;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$result['childrens'] = $childrens;
|
||||
$result['status'] = true;
|
||||
}
|
||||
} catch (\Magento\Framework\Exception\LocalizedException $e) {
|
||||
$result['message'] = $e->getMessage();
|
||||
} catch (\Exception $e) {
|
||||
$result['message'] = __('An error occurred while processing your request. Please try again later.');
|
||||
}
|
||||
$this->getResponse()->representJson(
|
||||
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_QuickOrder
|
||||
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\QuickOrder\Controller\Index;
|
||||
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
|
||||
class LoadProducts extends \Magento\Framework\App\Action\Action
|
||||
{
|
||||
/**
|
||||
* @var \Magezon\QuickOrder\Model\ProductManagement
|
||||
*/
|
||||
protected $productManagement;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Action\Context $context
|
||||
* @param \Magezon\QuickOrder\Model\ProductManagement $productManagement
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Action\Context $context,
|
||||
\Magezon\QuickOrder\Model\ProductManagement $productManagement,
|
||||
protected Session $customerSession,
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->productManagement = $productManagement;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\Controller\Result\Raw
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$result['status'] = false;
|
||||
$result['isLoggedIn'] = $this->customerSession->isLoggedIn();
|
||||
try {
|
||||
$params = $this->getRequest()->getParams();
|
||||
$result['products'] = $this->productManagement->getProductList($params);
|
||||
$result['status'] = true;
|
||||
} catch (\Magento\Framework\Exception\LocalizedException $e) {
|
||||
$result['message'] = $e->getMessage();
|
||||
} catch (\Exception $e) {
|
||||
$result['message'] = $e->getMessage();
|
||||
}
|
||||
$this->getResponse()->representJson(
|
||||
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license/.
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_QuickOrder
|
||||
* @copyright Copyright (C) 2018 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\QuickOrder\Controller;
|
||||
|
||||
use Magento\Framework\App\ActionFactory;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\App\RouterInterface;
|
||||
use Magento\Framework\DataObject;
|
||||
|
||||
class Router implements RouterInterface
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $dispatched;
|
||||
|
||||
/**
|
||||
* @var ActionFactory
|
||||
*/
|
||||
protected $actionFactory;
|
||||
|
||||
/**
|
||||
* @var \Magezon\QuickOrder\Helper\Data
|
||||
*/
|
||||
protected $dataHelper;
|
||||
|
||||
/**
|
||||
* @param ActionFactory $actionFactory
|
||||
* @param \Magezon\QuickOrder\Helper\Data $dataHelper
|
||||
*/
|
||||
public function __construct(
|
||||
ActionFactory $actionFactory,
|
||||
\Magezon\QuickOrder\Helper\Data $dataHelper
|
||||
) {
|
||||
$this->actionFactory = $actionFactory;
|
||||
$this->dataHelper = $dataHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function match(RequestInterface $request)
|
||||
{
|
||||
if (!$this->dispatched && $this->dataHelper->isEnabled()) {
|
||||
$pathInfo = trim($request->getPathInfo(), '/');
|
||||
$result = $this->processUrlKey($pathInfo);
|
||||
|
||||
if ($result) {
|
||||
$request->setModuleName($result->getModuleName())
|
||||
->setControllerName($result->getControllerName())
|
||||
->setActionName($result->getActionName());
|
||||
if ($params = $result->getParams()) {
|
||||
$request->setParams($params);
|
||||
}
|
||||
|
||||
$request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $pathInfo);
|
||||
$request->setDispatched(true);
|
||||
$this->dispatched = true;
|
||||
return $this->actionFactory->create(
|
||||
'Magento\Framework\App\Action\Forward',
|
||||
['request' => $request]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $identifier
|
||||
* @return boolean
|
||||
*/
|
||||
protected function processUrlKey($identifier)
|
||||
{
|
||||
$result = false;
|
||||
$route = $this->dataHelper->getConfig('general/route');
|
||||
if ($route) {
|
||||
$paths = explode("/", $identifier);
|
||||
if (count($paths) == 1 && ($identifier == $route)) {
|
||||
$result = new DataObject([
|
||||
'module_name' => 'quickorder',
|
||||
'controller_name' => 'index',
|
||||
'action_name' => 'index'
|
||||
]);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user