5
5
*/
6
6
namespace Magento \Sales \Model ;
7
7
8
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
8
9
use Magento \Framework \App \Config \ValueFactory ;
9
10
use Magento \Framework \App \Config \ValueInterface ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Sales \Model \Order \Email \Container \IdentityInterface ;
13
+ use Magento \Sales \Model \Order \Email \Sender ;
12
14
use Magento \Sales \Model \ResourceModel \Collection \AbstractCollection ;
15
+ use Magento \Sales \Model \ResourceModel \EntityAbstract ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
13
17
14
18
/**
15
19
* Sales emails sending
@@ -68,16 +72,18 @@ class EmailSenderHandler
68
72
* @var string
69
73
*/
70
74
private $ modifyStartFromDate ;
75
+ private int $ maxSendAttempts ;
71
76
72
77
/**
73
- * @param \Magento\Sales\Model\Order\Email\ Sender $emailSender
74
- * @param \Magento\Sales\Model\ResourceModel\ EntityAbstract $entityResource
78
+ * @param Sender $emailSender
79
+ * @param EntityAbstract $entityResource
75
80
* @param AbstractCollection $entityCollection
76
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $globalConfig
81
+ * @param ScopeConfigInterface $globalConfig
77
82
* @param IdentityInterface|null $identityContainer
78
- * @param \Magento\Store\Model\ StoreManagerInterface|null $storeManager
83
+ * @param StoreManagerInterface|null $storeManager
79
84
* @param ValueFactory|null $configValueFactory
80
85
* @param string|null $modifyStartFromDate
86
+ * @param int $maxSendAttempts
81
87
*/
82
88
public function __construct (
83
89
\Magento \Sales \Model \Order \Email \Sender $ emailSender ,
@@ -87,7 +93,8 @@ public function __construct(
87
93
IdentityInterface $ identityContainer = null ,
88
94
\Magento \Store \Model \StoreManagerInterface $ storeManager = null ,
89
95
?ValueFactory $ configValueFactory = null ,
90
- ?string $ modifyStartFromDate = null
96
+ ?string $ modifyStartFromDate = null ,
97
+ int $ maxSendAttempts = null
91
98
) {
92
99
$ this ->emailSender = $ emailSender ;
93
100
$ this ->entityResource = $ entityResource ;
@@ -101,6 +108,7 @@ public function __construct(
101
108
102
109
$ this ->configValueFactory = $ configValueFactory ?: ObjectManager::getInstance ()->get (ValueFactory::class);
103
110
$ this ->modifyStartFromDate = $ modifyStartFromDate ?: $ this ->modifyStartFromDate ;
111
+ $ this ->maxSendAttempts = $ maxSendAttempts ?? 3 ;
104
112
}
105
113
106
114
/**
@@ -112,7 +120,13 @@ public function sendEmails()
112
120
{
113
121
if ($ this ->globalConfig ->getValue ('sales_email/general/async_sending ' )) {
114
122
$ this ->entityCollection ->addFieldToFilter ('send_email ' , ['eq ' => 1 ]);
115
- $ this ->entityCollection ->addFieldToFilter ('email_sent ' , ['null ' => true ]);
123
+ $ this ->entityCollection ->addFieldToFilter (
124
+ 'email_sent ' ,
125
+ [
126
+ ['null ' => true ],
127
+ ['lteq ' => -1 ]
128
+ ]
129
+ );
116
130
$ this ->filterCollectionByStartFromDate ($ this ->entityCollection );
117
131
$ this ->entityCollection ->setPageSize (
118
132
$ this ->globalConfig ->getValue ('sales_email/general/sending_limit ' )
@@ -132,9 +146,17 @@ public function sendEmails()
132
146
133
147
/** @var \Magento\Sales\Model\AbstractModel $item */
134
148
foreach ($ entityCollection ->getItems () as $ item ) {
149
+ $ sendAttempts = $ item ->getEmailSent () ?? -$ this ->maxSendAttempts ;
135
150
$ isEmailSent = $ this ->emailSender ->send ($ item , true );
151
+
152
+ if ($ isEmailSent ) {
153
+ $ sendAttempts = 1 ;
154
+ } else {
155
+ $ sendAttempts ++;
156
+ }
157
+
136
158
$ this ->entityResource ->saveAttribute (
137
- $ item ->setEmailSent ($ isEmailSent ),
159
+ $ item ->setEmailSent ($ sendAttempts ),
138
160
'email_sent '
139
161
);
140
162
}
0 commit comments