-
Notifications
You must be signed in to change notification settings - Fork 3
/
Export.php
161 lines (135 loc) · 5.12 KB
/
Export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace RealtimeDespatch\OrderFlow\Controller\Adminhtml\Product;
use Magento\Backend\App\Action\Context;
class Export extends \Magento\Backend\App\Action
{
/**
* @var \RealtimeDespatch\OrderFlow\Helper\Export\Product
*/
protected $_exportHelper;
/**
* @var \RealtimeDespatch\OrderFlow\Api\RequestBuilderInterface
*/
protected $_builder;
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $_repository;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @param Context $context
* @param \RealtimeDespatch\OrderFlow\Helper\Export\Product $helper
* @param \RealtimeDespatch\OrderFlow\Api\RequestBuilderInterface $builder
* @param \Magento\Catalog\Model\ProductRepository $repository
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
\RealtimeDespatch\OrderFlow\Helper\Export\Product $helper,
\RealtimeDespatch\OrderFlow\Api\RequestBuilderInterface $builder,
\Magento\Catalog\Model\ProductRepository $repository,
\Magento\Store\Model\StoreManagerInterface $storeManager)
{
$this->_exportHelper = $helper;
$this->_builder = $builder;
$this->_repository = $repository;
$this->_storeManager = $storeManager;
parent::__construct($context);
}
/**
* Export Action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
// Check whether product exports are enabled.
if ( ! $this->_exportHelper->isEnabled()) {
$this->messageManager->addError(__('Product exports are currently disabled. Please review the OrderFlow module configuration.'));
return $resultRedirect->setRefererUrl();
}
try {
$product = $this->_getProduct();
if ( ! $product) {
return $resultRedirect->setRefererUrl();
}
if ($product->getTypeId() !== 'simple') {
$this->messageManager->addError(__('This product cannot be exported. OrderFlow only supports simple product types.'));
return $resultRedirect->setRefererUrl();
}
$request = $this->_buildRequest($product);
$export = $this->_getRequestProcessor()->process($request);
if ($export->getFailures() || $export->getDuplicates()) {
$this->messageManager->addError(__('Product '.$product->getSku().' has failed to be queued for export to OrderFlow.'));
} else {
$this->messageManager->addSuccess(__('Product '.$product->getSku().' has been queued for export to OrderFlow.'));
}
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
return $resultRedirect->setRefererUrl();
}
/**
* Retrieves a product from the current request
*
* @return \Magento\Sales\Api\Data\ProductInterface
*/
protected function _getProduct()
{
$id = $this->getRequest()->getParam('id');
try {
$product = $this->_repository->getById($id);
} catch (NoSuchEntityException $e) {
$this->messageManager->addError(__('Product Not Found.'));
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
return false;
} catch (InputException $e) {
$this->messageManager->addError(__('Product Not Found.'));
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
return false;
}
return $product;
}
/**
* Retrieves a product from the current request
*
* @return \RealtimeDespatch\OrderFlow\Api\Data\RequestInterface
*/
protected function _buildRequest($product)
{
$operation = \RealtimeDespatch\OrderFlow\Api\Data\RequestInterface::OP_UPDATE;
if ( ! $product->getOrderflowExportDate()) {
$operation = \RealtimeDespatch\OrderFlow\Api\Data\RequestInterface::OP_CREATE;
}
$this->_builder->setRequestData(
\RealtimeDespatch\OrderFlow\Api\Data\RequestInterface::TYPE_EXPORT,
\RealtimeDespatch\OrderFlow\Api\Data\RequestInterface::ENTITY_PRODUCT,
$operation
);
$this->_builder->setScopeId($this->_getWebsiteId());
$this->_builder->addRequestLine(json_encode(array('sku' => $product->getSku())));
return $this->_builder->saveRequest();
}
/**
* Retrieve the request processor instance.
*
* @return RealtimeDespatch\OrderFlow\Model\Service\Request\RequestProcessor
*/
protected function _getRequestProcessor()
{
return $this->_objectManager->create('ProductCreateRequestProcessor');
}
/**
* Returns the current website ID.
*
* @return integer
*/
protected function _getWebsiteId()
{
return $this->_storeManager->getStore($this->_request->getParam('store'))->getWebsiteId();
}
}