33
44namespace IntegerNet \ProductIsNewAttribute \Service ;
55
6+ use IntegerNet \ProductIsNewAttribute \Model \Config ;
67use Magento \Catalog \Api \Data \ProductInterface ;
78use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
89use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
@@ -21,27 +22,34 @@ class ProductsUpdateService
2122 private StoreManagerInterface $ storeManager ;
2223 private TimezoneInterface $ timezone ;
2324 private ProductResource $ productResource ;
25+ private Config $ config ;
2426
2527 public function __construct (
2628 ProductCollectionFactory $ productCollectionFactory ,
2729 StoreManagerInterface $ storeManager ,
2830 TimezoneInterface $ timezone ,
29- ProductResource $ productResource
31+ ProductResource $ productResource ,
32+ Config $ config
3033 ) {
3134 $ this ->productCollectionFactory = $ productCollectionFactory ;
3235 $ this ->storeManager = $ storeManager ;
3336 $ this ->timezone = $ timezone ;
3437 $ this ->productResource = $ productResource ;
38+ $ this ->config = $ config ;
3539 }
3640
3741 public function updateIsNewValues (): void
3842 {
3943 foreach ($ this ->storeManager ->getStores (true ) as $ store ) {
44+ if (!$ this ->config ->isAutoGenerationEnabled ((int )$ store ->getId ())) {
45+ continue ;
46+ }
47+
4048 foreach ($ this ->getProductsToCheck ($ store ) as $ product ) {
4149 $ hasProductNewAttributeValue = $ product ->hasData (self ::ATTRIBUTE_CODE_IS_NEW );
4250 $ isProductNew = $ this ->isProductNew ($ product );
4351 $ wasProductNew = (bool )$ product ->getData (self ::ATTRIBUTE_CODE_IS_NEW );
44- if (!$ hasProductNewAttributeValue || ($ isProductNew != $ wasProductNew )) {
52+ if (!$ hasProductNewAttributeValue || ($ isProductNew !== $ wasProductNew )) {
4553 $ product ->setData (self ::ATTRIBUTE_CODE_IS_NEW , $ isProductNew );
4654 $ this ->productResource ->saveAttribute ($ product , self ::ATTRIBUTE_CODE_IS_NEW );
4755 }
@@ -58,23 +66,12 @@ private function getProductsToCheck(StoreInterface $store): array
5866 /** @var ProductCollection $productCollection */
5967 $ productCollection = $ this ->productCollectionFactory ->create ();
6068 $ productCollection ->setStore ($ store );
69+ $ productCollection ->addWebsiteFilter ($ store ->getWebsiteId ());
6170 $ productCollection ->addAttributeToSelect ([
6271 self ::ATTRIBUTE_CODE_IS_NEW ,
6372 self ::ATTRIBUTE_CODE_NEW_FROM_DATE ,
6473 self ::ATTRIBUTE_CODE_NEW_TO_DATE ,
6574 ]);
66- // $productCollection->addAttributeToFilter( // OR filter
67- // [
68- // [
69- // 'attribute' => self::ATTRIBUTE_CODE_NEW_FROM_DATE,
70- // 'notnull' => true,
71- // ],
72- // [
73- // 'attribute' => self::ATTRIBUTE_CODE_NEW_TO_DATE,
74- // 'notnull' => true,
75- // ],
76- // ]
77- // );
7875 return $ productCollection ->getItems ();
7976 }
8077
0 commit comments