1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Api\Plugin; |
4
|
|
|
|
5
|
|
|
use Magento\Catalog\Model\Product; |
6
|
|
|
use \Magento\Framework\ObjectManagerInterface; |
7
|
|
|
use \Magento\Framework\Event\ManagerInterface; |
8
|
|
|
use \Richdynamix\PersonalisedProducts\Model\Export; |
9
|
|
|
use \Richdynamix\PersonalisedProducts\Model\ExportFactory; |
10
|
|
|
use \Richdynamix\PersonalisedProducts\Helper\Config; |
11
|
|
|
use \Richdynamix\PersonalisedProducts\Logger\PersonalisedProductsLogger; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ExportSaveProducts |
15
|
|
|
* |
16
|
|
|
* @category Richdynamix |
17
|
|
|
* @package PersonalisedProducts |
18
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
19
|
|
|
*/ |
20
|
|
|
class ExportSaveProducts |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var PersonalisedProductsLogger |
24
|
|
|
*/ |
25
|
|
|
private $_logger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Config |
29
|
|
|
*/ |
30
|
|
|
private $_config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Export |
34
|
|
|
*/ |
35
|
|
|
private $_export; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ExportFactory |
39
|
|
|
*/ |
40
|
|
|
private $_exportFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* ExportSaveProducts constructor. |
44
|
|
|
* @param PersonalisedProductsLogger $logger |
45
|
|
|
* @param Config $config |
46
|
|
|
* @param Export $export |
47
|
|
|
* @param ObjectManagerInterface $objectManager |
48
|
|
|
* @param ManagerInterface $eventManager |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
PersonalisedProductsLogger $logger, |
52
|
|
|
Config $config, |
53
|
|
|
Export $export, |
54
|
|
|
ExportFactory $exportFactory, |
55
|
|
|
ObjectManagerInterface $objectManager, |
56
|
|
|
ManagerInterface $eventManager |
57
|
|
|
) { |
58
|
|
|
$this->_logger = $logger; |
59
|
|
|
$this->_config = $config; |
60
|
|
|
$this->_export = $export; |
61
|
|
|
$this->_exportFactory = $exportFactory; |
62
|
|
|
$this->_objectManager = $objectManager; |
|
|
|
|
63
|
|
|
$this->_eventManager = $eventManager; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Plugin into the afterSave method on a product |
68
|
|
|
* |
69
|
|
|
* @param Product $product |
70
|
|
|
* @param $result |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function afterAfterSave(Product $product, $result) |
74
|
|
|
{ |
75
|
|
|
$this->_saveProductForExport($product->getId()); |
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Save the new product ready for export |
81
|
|
|
* |
82
|
|
|
* @param $productId |
83
|
|
|
*/ |
84
|
|
|
private function _saveProductForExport($productId) |
85
|
|
|
{ |
86
|
|
|
if (!$this->_isReadyForExport($productId)) { |
87
|
|
|
$exportItem = $this->_export->saveProductForExport($productId); |
88
|
|
|
|
89
|
|
|
$this->_eventManager->dispatch( |
90
|
|
|
'personalised_products_export_after_save', |
91
|
|
|
['exportItem' => $exportItem] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check the product exists in the export table |
98
|
|
|
* |
99
|
|
|
* @param $productId |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
private function _isReadyForExport($productId) |
103
|
|
|
{ |
104
|
|
|
$product = $this->_export->getCollection() |
105
|
|
|
->addFieldToFilter('product_id', $productId) |
106
|
|
|
->getFirstItem(); |
107
|
|
|
|
108
|
|
|
return ($product->getData("product_id")) ? true : false; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: