1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MagePrince |
5
|
|
|
* Copyright (C) 2020 Mageprince <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @package Mageprince_BuyNow |
8
|
|
|
* @copyright Copyright (c) 2020 Mageprince (http://www.mageprince.com/) |
9
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License,version 3 (GPL-3.0) |
10
|
|
|
* @author MagePrince <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Mageprince\Buynow\Controller\Cart; |
14
|
|
|
|
15
|
|
|
use Magento\Catalog\Api\ProductRepositoryInterface; |
|
|
|
|
16
|
|
|
use Magento\Checkout\Model\Cart as CustomerCart; |
|
|
|
|
17
|
|
|
use Magento\Checkout\Model\Cart\RequestQuantityProcessor; |
|
|
|
|
18
|
|
|
use Magento\Framework\App\ObjectManager; |
|
|
|
|
19
|
|
|
|
20
|
|
|
class Add extends \Magento\Checkout\Controller\Cart\Add |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Mageants\GiftCard\Model\Account |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
protected $productRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Magento\Quote\Model\QuoteRepository |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
protected $quoteRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RequestQuantityProcessor |
34
|
|
|
*/ |
35
|
|
|
private $quantityProcessor; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Magento\Framework\App\Action\Context $context |
39
|
|
|
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
40
|
|
|
* @param \Magento\Checkout\Model\Session $checkoutSession |
41
|
|
|
* @param \Magento\Store\Model\StoreManagerInterface $storeManager |
42
|
|
|
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator |
43
|
|
|
* @param CustomerCart $cart |
44
|
|
|
* @param ProductRepositoryInterface $productRepository |
45
|
|
|
* @param RequestQuantityProcessor $quantityProcessor |
46
|
|
|
* @param \Magento\Quote\Model\QuoteRepository $quoteRepository |
47
|
|
|
* @param \Magento\Quote\Model\QuoteFactory $quoteFactory |
48
|
|
|
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
\Magento\Framework\App\Action\Context $context, |
|
|
|
|
52
|
|
|
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
|
|
|
|
53
|
|
|
\Magento\Checkout\Model\Session $checkoutSession, |
|
|
|
|
54
|
|
|
\Magento\Store\Model\StoreManagerInterface $storeManager, |
|
|
|
|
55
|
|
|
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, |
|
|
|
|
56
|
|
|
CustomerCart $cart, |
57
|
|
|
ProductRepositoryInterface $productRepository, |
58
|
|
|
? RequestQuantityProcessor $quantityProcessor = null, |
59
|
|
|
\Magento\Quote\Model\QuoteRepository $quoteRepository, |
60
|
|
|
\Magento\Quote\Model\QuoteFactory $quoteFactory, |
|
|
|
|
61
|
|
|
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository |
|
|
|
|
62
|
|
|
) { |
63
|
|
|
$this->productRepository = $productRepository; |
64
|
|
|
$this->quantityProcessor = $quantityProcessor ?? ObjectManager::getInstance()->get(RequestQuantityProcessor::class); |
65
|
|
|
$this->quoteRepository = $quoteRepository; |
66
|
|
|
$this->storeManager = $storeManager; |
|
|
|
|
67
|
|
|
$this->quoteFactory = $quoteFactory; |
|
|
|
|
68
|
|
|
$this->customerRepository = $customerRepository; |
|
|
|
|
69
|
|
|
$this->scopeConfig = $scopeConfig; |
|
|
|
|
70
|
|
|
parent::__construct( |
71
|
|
|
$context, |
72
|
|
|
$scopeConfig, |
73
|
|
|
$checkoutSession, |
74
|
|
|
$storeManager, |
75
|
|
|
$formKeyValidator, |
76
|
|
|
$cart, |
77
|
|
|
$productRepository, |
78
|
|
|
$quantityProcessor |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add product to shopping cart action |
84
|
|
|
* |
85
|
|
|
* @return \Magento\Framework\Controller\Result\Redirect |
|
|
|
|
86
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
87
|
|
|
*/ |
88
|
|
|
public function execute() |
89
|
|
|
{ |
90
|
|
|
$mainQuoteId = $this->cart->getQuote()->getEntityId(); |
91
|
|
|
if (!$this->_formKeyValidator->validate($this->getRequest())) { |
92
|
|
|
return $this->resultRedirectFactory->create()->setPath('*/*/'); |
93
|
|
|
} |
94
|
|
|
$params = $this->getRequest()->getParams(); |
95
|
|
|
try { |
96
|
|
|
if (isset($params['qty'])) { |
97
|
|
|
$filter = new \Zend_Filter_LocalizedToNormalized( |
|
|
|
|
98
|
|
|
['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()] |
99
|
|
|
); |
100
|
|
|
$params['qty'] = $filter->filter($params['qty']); |
101
|
|
|
} |
102
|
|
|
$product = $this->_initProduct(); |
103
|
|
|
$related = $this->getRequest()->getParam('related_product'); |
104
|
|
|
if (!$product) { |
105
|
|
|
return $this->goBack(); |
106
|
|
|
} |
107
|
|
|
$cartProducts = $this->scopeConfig->getValue( |
108
|
|
|
'buynow/general/keep_cart_products', |
109
|
|
|
\Magento\Store\Model\ScopeInterface::SCOPE_STORE |
|
|
|
|
110
|
|
|
); |
111
|
|
|
if (!$cartProducts) { |
112
|
|
|
$cartRestore = $this->scopeConfig->getValue( |
113
|
|
|
'buynow/general/restore_cart_products', |
114
|
|
|
\Magento\Store\Model\ScopeInterface::SCOPE_STORE |
115
|
|
|
); |
116
|
|
|
if ($cartRestore) { |
117
|
|
|
if ($mainQuoteId) { |
118
|
|
|
$quoteRepo = $this->quoteRepository->get($mainQuoteId); |
119
|
|
|
$quoteRepo->setData('is_active', 0); |
120
|
|
|
$this->quoteRepository->save($quoteRepo); |
121
|
|
|
} |
122
|
|
|
$store = $this->storeManager->getStore(); |
123
|
|
|
$websiteId = $this->storeManager->getStore()->getWebsiteId(); |
|
|
|
|
124
|
|
|
$quoteData = $this->quoteFactory->create(); |
125
|
|
|
$quoteData->setStore($store); |
126
|
|
|
$quoteData->setCurrency(); |
127
|
|
|
if ($this->cart->getQuote()->getCustomerId()) { |
128
|
|
|
$customerId = $this->cart->getQuote()->getCustomerId(); |
129
|
|
|
$customerEmail = $this->cart->getQuote()->getCustomerEmail(); |
|
|
|
|
130
|
|
|
$customerRepoData = $this->customerRepository->getById($customerId); |
131
|
|
|
$quoteData->assignCustomer($customerRepoData); |
132
|
|
|
} |
133
|
|
|
if ($mainQuoteId) { |
134
|
|
|
$quoteData->setBuyNowId($mainQuoteId); |
135
|
|
|
} |
136
|
|
|
$quoteData->save(); |
137
|
|
|
$this->cart->setQuote($quoteData); |
138
|
|
|
} else { |
139
|
|
|
$this->cart->truncate(); //remove all products from cart |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
$this->cart->addProduct($product, $params); |
143
|
|
|
if (!empty($related)) { |
144
|
|
|
$this->cart->addProductsByIds(explode(',', $related)); |
145
|
|
|
} |
146
|
|
|
$this->cart->save(); |
147
|
|
|
|
148
|
|
|
// $this->_eventManager->dispatch( |
149
|
|
|
// 'checkout_cart_add_product_complete', |
150
|
|
|
// ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()] |
151
|
|
|
// ); |
152
|
|
|
|
153
|
|
|
if (!$this->_checkoutSession->getNoCartRedirect(true)) { |
154
|
|
|
$baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface') |
155
|
|
|
->getStore()->getBaseUrl(); |
156
|
|
|
return $this->goBack($baseUrl . 'checkout/', $product); |
157
|
|
|
} |
158
|
|
|
} catch (\Magento\Framework\Exception\LocalizedException $e) { |
|
|
|
|
159
|
|
|
if ($this->_checkoutSession->getUseNotice(true)) { |
160
|
|
|
$this->messageManager->addNotice( |
161
|
|
|
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($e->getMessage()) |
162
|
|
|
); |
163
|
|
|
} else { |
164
|
|
|
$messages = array_unique(explode("\n", $e->getMessage())); |
165
|
|
|
foreach ($messages as $message) { |
166
|
|
|
$this->messageManager->addError( |
167
|
|
|
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($message) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
$url = $this->_checkoutSession->getRedirectUrl(true); |
172
|
|
|
if (!$url) { |
173
|
|
|
$cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl(); |
174
|
|
|
$url = $this->_redirect->getRedirectUrl($cartUrl); |
175
|
|
|
} |
176
|
|
|
return $this->goBack($url); |
177
|
|
|
} catch (\Exception $e) { |
178
|
|
|
$this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.')); |
|
|
|
|
179
|
|
|
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e); |
180
|
|
|
return $this->goBack(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths