|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use \Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use \Magento\Sales\Model\OrderFactory; |
|
7
|
|
|
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EventClient\Client; |
|
8
|
|
|
use \Symfony\Component\Config\Definition\Exception\Exception; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AbstractOrdersCommand |
|
12
|
|
|
* |
|
13
|
|
|
* @category Richdynamix |
|
14
|
|
|
* @package PersonalisedProducts |
|
15
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractOrdersCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var OrderFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
private $_orderFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Client |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_eventClient; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var |
|
32
|
|
|
*/ |
|
33
|
|
|
private $_productCollection; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* AbstractOrdersCommand constructor. |
|
37
|
|
|
* @param OrderFactory $orderFactory |
|
38
|
|
|
* @param Client $eventClient |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(OrderFactory $orderFactory, Client $eventClient) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->_orderFactory = $orderFactory; |
|
43
|
|
|
$this->_eventClient = $eventClient; |
|
44
|
|
|
parent::__construct(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Prepare each order for customer and product data to send to PredictionIO |
|
49
|
|
|
* |
|
50
|
|
|
* @param $collection |
|
51
|
|
|
* @return int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function _sendCustomerBuyProductData($collection) |
|
54
|
|
|
{ |
|
55
|
|
|
$collectionCount = count($collection); |
|
56
|
|
|
$sentEventCount = 0; |
|
57
|
|
|
|
|
58
|
|
|
foreach ($collection as $customerId => $products) { |
|
59
|
|
|
$sentEvent = $this->_sendPurchaseEvent($customerId, $products); |
|
60
|
|
|
if ($sentEvent) { |
|
61
|
|
|
++$sentEventCount; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($collectionCount != $sentEventCount) { |
|
66
|
|
|
throw new Exception('There was a problem sending the event data, check the log file for more information'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $sentEventCount; |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get a collection of all completed orders from logged in customers |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function _getOrderCollection() |
|
79
|
|
|
{ |
|
80
|
|
|
$order = $this->_orderFactory->create(); |
|
81
|
|
|
$ordersCollection = $order->getCollection() |
|
82
|
|
|
->addFieldToSelect(['entity_id', 'customer_id']) |
|
83
|
|
|
->addFieldToFilter('state', ['eq' => 'complete']) |
|
84
|
|
|
->addFieldToFilter('customer_id', array('neq' => 'NULL' )); |
|
85
|
|
|
|
|
86
|
|
|
return $ordersCollection->getData(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get a collection of all products in the orders |
|
91
|
|
|
* |
|
92
|
|
|
* @param $ordersCollection |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function _getCustomerProductCollection($ordersCollection) |
|
96
|
|
|
{ |
|
97
|
|
|
$purchasedProducts = []; |
|
98
|
|
|
foreach ($ordersCollection as $order) { |
|
99
|
|
|
$order = $this->_orderFactory->create()->load($order['entity_id']); |
|
100
|
|
|
$itemCollection = $order->getItems(); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($itemCollection as $item) { |
|
103
|
|
|
$purchasedProducts[$order['customer_id']][] = $item->getProductId(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->_productCollection = $purchasedProducts; |
|
108
|
|
|
|
|
109
|
|
|
return $purchasedProducts; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Count the number of customers in all the orders |
|
114
|
|
|
* |
|
115
|
|
|
* @return int |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function _getCustomerCount() |
|
118
|
|
|
{ |
|
119
|
|
|
return count($this->_productCollection); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Count all the products across all the orders |
|
124
|
|
|
* |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function _getProductCount() |
|
128
|
|
|
{ |
|
129
|
|
|
$productCount = 0; |
|
130
|
|
|
foreach ($this->_productCollection as $products) { |
|
131
|
|
|
$productCount += count($products); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $productCount; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Send customer-buys-item events to PredictionIO for all existing orders |
|
139
|
|
|
* |
|
140
|
|
|
* @param $customerId |
|
141
|
|
|
* @param $products |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function _sendPurchaseEvent($customerId, $products) |
|
145
|
|
|
{ |
|
146
|
|
|
foreach ($products as $productId) { |
|
147
|
|
|
if (!$this->_eventClient->saveCustomerBuyProduct($customerId, $productId)) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|