55 */
66namespace Magento \Sales \Model ;
77
8+ use Magento \Framework \App \Config \ValueFactory ;
9+ use Magento \Framework \App \Config \ValueInterface ;
10+ use Magento \Framework \App \ObjectManager ;
811use Magento \Sales \Model \Order \Email \Container \IdentityInterface ;
12+ use Magento \Sales \Model \ResourceModel \Collection \AbstractCollection ;
913
1014/**
1115 * Sales emails sending
@@ -32,7 +36,7 @@ class EmailSenderHandler
3236 /**
3337 * Entity collection model.
3438 *
35- * @var \Magento\Sales\Model\ResourceModel\Collection\ AbstractCollection
39+ * @var AbstractCollection
3640 */
3741 protected $ entityCollection ;
3842
@@ -53,32 +57,50 @@ class EmailSenderHandler
5357 */
5458 private $ storeManager ;
5559
60+ /**
61+ * Config data factory
62+ *
63+ * @var ValueFactory
64+ */
65+ private $ configValueFactory ;
66+
67+ /**
68+ * @var string
69+ */
70+ private $ modifyStartFromDate ;
71+
5672 /**
5773 * @param \Magento\Sales\Model\Order\Email\Sender $emailSender
5874 * @param \Magento\Sales\Model\ResourceModel\EntityAbstract $entityResource
59- * @param \Magento\Sales\Model\ResourceModel\Collection\ AbstractCollection $entityCollection
75+ * @param AbstractCollection $entityCollection
6076 * @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
6177 * @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
6481 */
6582 public function __construct (
6683 \Magento \Sales \Model \Order \Email \Sender $ emailSender ,
6784 \Magento \Sales \Model \ResourceModel \EntityAbstract $ entityResource ,
68- \ Magento \ Sales \ Model \ ResourceModel \ Collection \ AbstractCollection $ entityCollection ,
85+ AbstractCollection $ entityCollection ,
6986 \Magento \Framework \App \Config \ScopeConfigInterface $ globalConfig ,
7087 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
7291 ) {
7392 $ this ->emailSender = $ emailSender ;
7493 $ this ->entityResource = $ entityResource ;
7594 $ this ->entityCollection = $ entityCollection ;
7695 $ this ->globalConfig = $ globalConfig ;
7796
78- $ this ->identityContainer = $ identityContainer ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
97+ $ this ->identityContainer = $ identityContainer ?: ObjectManager::getInstance ()
7998 ->get (\Magento \Sales \Model \Order \Email \Container \NullIdentity::class);
80- $ this ->storeManager = $ storeManager ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
99+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()
81100 ->get (\Magento \Store \Model \StoreManagerInterface::class);
101+
102+ $ this ->configValueFactory = $ configValueFactory ?: ObjectManager::getInstance ()->get (ValueFactory::class);
103+ $ this ->modifyStartFromDate = $ modifyStartFromDate ?: $ this ->modifyStartFromDate ;
82104 }
83105
84106 /**
@@ -90,6 +112,7 @@ public function sendEmails()
90112 if ($ this ->globalConfig ->getValue ('sales_email/general/async_sending ' )) {
91113 $ this ->entityCollection ->addFieldToFilter ('send_email ' , ['eq ' => 1 ]);
92114 $ this ->entityCollection ->addFieldToFilter ('email_sent ' , ['null ' => true ]);
115+ $ this ->filterCollectionByStartFromDate ($ this ->entityCollection );
93116 $ this ->entityCollection ->setPageSize (
94117 $ this ->globalConfig ->getValue ('sales_email/general/sending_limit ' )
95118 );
@@ -126,7 +149,7 @@ public function sendEmails()
126149 * @throws \Magento\Framework\Exception\NoSuchEntityException
127150 */
128151 private function getStores (
129- \ Magento \ Sales \ Model \ ResourceModel \ Collection \ AbstractCollection $ entityCollection
152+ AbstractCollection $ entityCollection
130153 ): array {
131154 $ stores = [];
132155
@@ -140,4 +163,26 @@ private function getStores(
140163
141164 return $ stores ;
142165 }
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+ }
143188}
0 commit comments