Skip to content

Commit f98fcde

Browse files
committed
Make update functionality configurable
1 parent 1507f57 commit f98fcde

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

src/Model/Config.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\ProductIsNewAttribute\Model;
5+
6+
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Store\Model\ScopeInterface;
8+
9+
class Config
10+
{
11+
private ScopeConfigInterface $scopeConfig;
12+
13+
public function __construct(
14+
ScopeConfigInterface $scopeConfig
15+
) {
16+
$this->scopeConfig = $scopeConfig;
17+
}
18+
19+
public function isAutoGenerationEnabled(int $storeId = 0): bool
20+
{
21+
return $this->scopeConfig->isSetFlag(
22+
'catalog/product_is_new_attribute/is_autogeneration_enabled',
23+
ScopeInterface::SCOPE_STORE,
24+
$storeId
25+
);
26+
}
27+
}

src/Service/ProductsUpdateService.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace IntegerNet\ProductIsNewAttribute\Service;
55

6+
use IntegerNet\ProductIsNewAttribute\Model\Config;
67
use Magento\Catalog\Api\Data\ProductInterface;
78
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
89
use 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

src/etc/adminhtml/system.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
3+
<system>
4+
<section id="catalog">
5+
<group id="product_is_new_attribute" translate="label" type="text" sortOrder="1210" showInDefault="1" showInWebsite="1" showInStore="1">
6+
<label><![CDATA[Product "Is New" Attribute]]></label>
7+
<field id="is_autogeneration_enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
8+
<label>Will be updated automatically every night</label>
9+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
10+
</field>
11+
</group>
12+
</section>
13+
</system>
14+
</config>

src/etc/config.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
4+
<default>
5+
<catalog>
6+
<product_is_new_attribute>
7+
<is_autogeneration_enabled>1</is_autogeneration_enabled>
8+
</product_is_new_attribute>
9+
</catalog>
10+
</default>
11+
</config>

0 commit comments

Comments
 (0)