5
5
*/
6
6
namespace Magento \Sales \Model ;
7
7
8
+ use Magento \Framework \App \Config \ValueFactory ;
9
+ use Magento \Framework \App \Config \ValueInterface ;
10
+ use Magento \Framework \App \ObjectManager ;
8
11
use Magento \Sales \Model \Order \Email \Container \IdentityInterface ;
12
+ use Magento \Sales \Model \ResourceModel \Collection \AbstractCollection ;
9
13
10
14
/**
11
15
* Sales emails sending
@@ -32,7 +36,7 @@ class EmailSenderHandler
32
36
/**
33
37
* Entity collection model.
34
38
*
35
- * @var \Magento\Sales\Model\ResourceModel\Collection\ AbstractCollection
39
+ * @var AbstractCollection
36
40
*/
37
41
protected $ entityCollection ;
38
42
@@ -53,32 +57,50 @@ class EmailSenderHandler
53
57
*/
54
58
private $ storeManager ;
55
59
60
+ /**
61
+ * Config data factory
62
+ *
63
+ * @var ValueFactory
64
+ */
65
+ private $ configValueFactory ;
66
+
67
+ /**
68
+ * @var string
69
+ */
70
+ private $ modifyStartFromDate ;
71
+
56
72
/**
57
73
* @param \Magento\Sales\Model\Order\Email\Sender $emailSender
58
74
* @param \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource
59
- * @param \Magento\Sales\Model\ResourceModel\Collection\ AbstractCollection $entityCollection
75
+ * @param AbstractCollection $entityCollection
60
76
* @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
61
77
* @param IdentityInterface|null $identityContainer
62
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
63
- * @throws \InvalidArgumentException
78
+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
79
+ * @param ValueFactory|null $configValueFactory
80
+ * @param string|null $modifyStartFromDate
64
81
*/
65
82
public function __construct (
66
83
\Magento \Sales \Model \Order \Email \Sender $ emailSender ,
67
84
\Magento \Sales \Model \ResourceModel \EntityAbstract $ entityResource ,
68
- \ Magento \ Sales \ Model \ ResourceModel \ Collection \ AbstractCollection $ entityCollection ,
85
+ AbstractCollection $ entityCollection ,
69
86
\Magento \Framework \App \Config \ScopeConfigInterface $ globalConfig ,
70
87
IdentityInterface $ identityContainer = null ,
71
- \Magento \Store \Model \StoreManagerInterface $ storeManager = null
88
+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null ,
89
+ ?ValueFactory $ configValueFactory = null ,
90
+ ?string $ modifyStartFromDate = null
72
91
) {
73
92
$ this ->emailSender = $ emailSender ;
74
93
$ this ->entityResource = $ entityResource ;
75
94
$ this ->entityCollection = $ entityCollection ;
76
95
$ this ->globalConfig = $ globalConfig ;
77
96
78
- $ this ->identityContainer = $ identityContainer ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
97
+ $ this ->identityContainer = $ identityContainer ?: ObjectManager::getInstance ()
79
98
->get (\Magento \Sales \Model \Order \Email \Container \NullIdentity::class);
80
- $ this ->storeManager = $ storeManager ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
99
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()
81
100
->get (\Magento \Store \Model \StoreManagerInterface::class);
101
+
102
+ $ this ->configValueFactory = $ configValueFactory ?: ObjectManager::getInstance ()->get (ValueFactory::class);
103
+ $ this ->modifyStartFromDate = $ modifyStartFromDate ?: $ this ->modifyStartFromDate ;
82
104
}
83
105
84
106
/**
@@ -90,6 +112,7 @@ public function sendEmails()
90
112
if ($ this ->globalConfig ->getValue ('sales_email/general/async_sending ' )) {
91
113
$ this ->entityCollection ->addFieldToFilter ('send_email ' , ['eq ' => 1 ]);
92
114
$ this ->entityCollection ->addFieldToFilter ('email_sent ' , ['null ' => true ]);
115
+ $ this ->filterCollectionByStartFromDate ($ this ->entityCollection );
93
116
$ this ->entityCollection ->setPageSize (
94
117
$ this ->globalConfig ->getValue ('sales_email/general/sending_limit ' )
95
118
);
@@ -126,7 +149,7 @@ public function sendEmails()
126
149
* @throws \Magento\Framework\Exception\NoSuchEntityException
127
150
*/
128
151
private function getStores (
129
- \ Magento \ Sales \ Model \ ResourceModel \ Collection \ AbstractCollection $ entityCollection
152
+ AbstractCollection $ entityCollection
130
153
): array {
131
154
$ stores = [];
132
155
@@ -140,4 +163,26 @@ private function getStores(
140
163
141
164
return $ stores ;
142
165
}
166
+
167
+ /**
168
+ * Filter collection by start from date
169
+ *
170
+ * @param AbstractCollection $collection
171
+ * @return void
172
+ */
173
+ private function filterCollectionByStartFromDate (AbstractCollection $ collection ): void
174
+ {
175
+ /** @var $configValue ValueInterface */
176
+ $ configValue = $ this ->configValueFactory ->create ();
177
+ $ configValue ->load ('sales_email/general/async_sending ' , 'path ' );
178
+
179
+ if ($ configValue ->getId ()) {
180
+ $ startFromDate = date (
181
+ 'Y-m-d H:i:s ' ,
182
+ strtotime ($ configValue ->getUpdatedAt () . ' ' . $ this ->modifyStartFromDate )
183
+ );
184
+
185
+ $ collection ->addFieldToFilter ('created_at ' , ['from ' => $ startFromDate ]);
186
+ }
187
+ }
143
188
}
0 commit comments