10
10
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
11
11
use Magento \Catalog \Model \ProductIdLocatorInterface ;
12
12
use Magento \Catalog \Model \ResourceModel \Attribute ;
13
+ use Magento \Framework \App \ObjectManager ;
13
14
use Magento \Framework \EntityManager \MetadataPool ;
14
15
use Magento \Framework \Exception \CouldNotDeleteException ;
15
16
use Magento \Framework \Exception \CouldNotSaveException ;
17
+ use Magento \Catalog \Model \Product \Action ;
18
+ use Magento \Framework \Stdlib \DateTime \DateTime as CoreDate ;
19
+ use Magento \Framework \Stdlib \DateTime ;
20
+ use Magento \Store \Model \Store ;
16
21
17
22
/**
18
23
* Class responsibly for persistence of prices.
@@ -69,6 +74,27 @@ class PricePersistence
69
74
*/
70
75
private $ itemsPerOperation = 500 ;
71
76
77
+ /**
78
+ * Product Action.
79
+ *
80
+ * @var Action
81
+ */
82
+ private $ productAction ;
83
+
84
+ /**
85
+ * Product Action.
86
+ *
87
+ * @var CoreDate
88
+ */
89
+ private $ coreDate ;
90
+
91
+ /**
92
+ * Product Action.
93
+ *
94
+ * @var DateTime
95
+ */
96
+ private $ dateTime ;
97
+
72
98
/**
73
99
* PricePersistence constructor.
74
100
*
@@ -77,19 +103,31 @@ class PricePersistence
77
103
* @param ProductIdLocatorInterface $productIdLocator
78
104
* @param MetadataPool $metadataPool
79
105
* @param string $attributeCode
106
+ * @param Action|null $productAction
107
+ * @param CoreDate|null $coreDate
108
+ * @param DateTime|null $dateTime
80
109
*/
81
110
public function __construct (
82
111
Attribute $ attributeResource ,
83
112
ProductAttributeRepositoryInterface $ attributeRepository ,
84
113
ProductIdLocatorInterface $ productIdLocator ,
85
114
MetadataPool $ metadataPool ,
86
- $ attributeCode = ''
115
+ $ attributeCode = '' ,
116
+ ?Action $ productAction = null ,
117
+ ?CoreDate $ coreDate = null ,
118
+ ?DateTime $ dateTime = null
87
119
) {
88
120
$ this ->attributeResource = $ attributeResource ;
89
121
$ this ->attributeRepository = $ attributeRepository ;
90
122
$ this ->attributeCode = $ attributeCode ;
91
123
$ this ->productIdLocator = $ productIdLocator ;
92
124
$ this ->metadataPool = $ metadataPool ;
125
+ $ this ->productAction = $ productAction ?: ObjectManager::getInstance ()
126
+ ->get (Action::class);
127
+ $ this ->coreDate = $ productAction ?: ObjectManager::getInstance ()
128
+ ->get (CoreDate::class);
129
+ $ this ->dateTime = $ productAction ?: ObjectManager::getInstance ()
130
+ ->get (DateTime::class);
93
131
}
94
132
95
133
/**
@@ -233,4 +271,27 @@ public function getEntityLinkField()
233
271
return $ this ->metadataPool ->getMetadata (ProductInterface::class)
234
272
->getLinkField ();
235
273
}
274
+
275
+ /**
276
+ * Update last updated date.
277
+ *
278
+ * @param array $productIds
279
+ * @return void
280
+ * @throws CouldNotSaveException
281
+ */
282
+ public function updateLastUpdatedAt (array $ productIds ): void
283
+ {
284
+ try {
285
+ $ this ->productAction ->updateAttributes (
286
+ $ productIds ,
287
+ [ProductInterface::UPDATED_AT => $ this ->dateTime ->formatDate ($ this ->coreDate ->gmtDate ())]
288
+ ,Store::DEFAULT_STORE_ID
289
+ );
290
+ } catch (\Exception $ e ) {
291
+ throw new CouldNotSaveException (
292
+ __ ("The attribute can't be saved. " ),
293
+ $ e
294
+ );
295
+ }
296
+ }
236
297
}
0 commit comments