1010use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
1111use Magento \Catalog \Model \ProductIdLocatorInterface ;
1212use Magento \Catalog \Model \ResourceModel \Attribute ;
13+ use Magento \Framework \App \ObjectManager ;
1314use Magento \Framework \EntityManager \MetadataPool ;
1415use Magento \Framework \Exception \CouldNotDeleteException ;
1516use 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 ;
1621
1722/**
1823 * Class responsibly for persistence of prices.
@@ -69,6 +74,27 @@ class PricePersistence
6974 */
7075 private $ itemsPerOperation = 500 ;
7176
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+
7298 /**
7399 * PricePersistence constructor.
74100 *
@@ -77,19 +103,31 @@ class PricePersistence
77103 * @param ProductIdLocatorInterface $productIdLocator
78104 * @param MetadataPool $metadataPool
79105 * @param string $attributeCode
106+ * @param Action|null $productAction
107+ * @param CoreDate|null $coreDate
108+ * @param DateTime|null $dateTime
80109 */
81110 public function __construct (
82111 Attribute $ attributeResource ,
83112 ProductAttributeRepositoryInterface $ attributeRepository ,
84113 ProductIdLocatorInterface $ productIdLocator ,
85114 MetadataPool $ metadataPool ,
86- $ attributeCode = ''
115+ $ attributeCode = '' ,
116+ ?Action $ productAction = null ,
117+ ?CoreDate $ coreDate = null ,
118+ ?DateTime $ dateTime = null
87119 ) {
88120 $ this ->attributeResource = $ attributeResource ;
89121 $ this ->attributeRepository = $ attributeRepository ;
90122 $ this ->attributeCode = $ attributeCode ;
91123 $ this ->productIdLocator = $ productIdLocator ;
92124 $ 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);
93131 }
94132
95133 /**
@@ -233,4 +271,27 @@ public function getEntityLinkField()
233271 return $ this ->metadataPool ->getMetadata (ProductInterface::class)
234272 ->getLinkField ();
235273 }
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+ }
236297}
0 commit comments