Skip to content

Commit 0f652b8

Browse files
committed
Added an option to disable checking for product problems when those products are invisible. Fixes #31
1 parent 1e51191 commit 0f652b8

File tree

9 files changed

+86
-9
lines changed

9 files changed

+86
-9
lines changed

Checker/Catalog/Product/UrlKey/DuplicateUrlKey.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Product\UrlKey as UrlKeyChecker;
88
use Baldwin\UrlDataIntegrityChecker\Console\Progress;
9+
use Baldwin\UrlDataIntegrityChecker\Util\Configuration as ConfigUtil;
910
use Baldwin\UrlDataIntegrityChecker\Util\Stores as StoresUtil;
1011
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory as AttributeScopeOverriddenValueFactory;
1213
use Magento\Catalog\Model\Product as ProductModel;
14+
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
1315
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1416
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1517
use Magento\Store\Model\Store;
@@ -28,17 +30,20 @@ class DuplicateUrlKey
2830
/** @var array<string, string> */
2931
private $cachedProductUrlKeyData;
3032
private $cachedProductSkusByIds;
33+
private $configUtil;
3134

3235
public function __construct(
3336
StoresUtil $storesUtil,
3437
Progress $progress,
3538
ProductCollectionFactory $productCollectionFactory,
36-
AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory
39+
AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory,
40+
ConfigUtil $configUtil
3741
) {
3842
$this->storesUtil = $storesUtil;
3943
$this->progress = $progress;
4044
$this->productCollectionFactory = $productCollectionFactory;
4145
$this->attributeScopeOverriddenValueFactory = $attributeScopeOverriddenValueFactory;
46+
$this->configUtil = $configUtil;
4247

4348
$this->progressIndex = 0;
4449
$this->cachedProductUrlKeyData = [];
@@ -82,12 +87,15 @@ private function checkForDuplicatedUrlKeyAttributeValues(): array
8287
->addAttributeToSelect(UrlKeyChecker::URL_KEY_ATTRIBUTE)
8388
->addAttributeToFilter(UrlKeyChecker::URL_KEY_ATTRIBUTE, ['notnull' => true], $joinType)
8489
->addAttributeToFilter(UrlKeyChecker::URL_KEY_ATTRIBUTE, ['neq' => ''], $joinType)
85-
// TODO: remove!
86-
// ->addAttributeToFilter('entity_id', [
87-
// 'in' => [147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158],
88-
// ])
8990
;
9091

92+
if ($this->configUtil->getOnlyCheckVisibleProducts()) {
93+
$collection->addAttributeToFilter(
94+
ProductInterface::VISIBILITY,
95+
['neq' => ProductVisibility::VISIBILITY_NOT_VISIBLE]
96+
);
97+
}
98+
9199
if ($this->progressIndex === 0) {
92100
$this->progress->setGuestimatedSize(count($storeIds), $collection->getSize());
93101
}

Checker/Catalog/Product/UrlKey/EmptyUrlKey.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Product\UrlKey as UrlKeyChecker;
88
use Baldwin\UrlDataIntegrityChecker\Console\Progress;
9+
use Baldwin\UrlDataIntegrityChecker\Util\Configuration as ConfigUtil;
910
use Baldwin\UrlDataIntegrityChecker\Util\Stores as StoresUtil;
1011
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory as AttributeScopeOverriddenValueFactory;
1213
use Magento\Catalog\Model\Product as ProductModel;
14+
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
1315
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1416
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1517
use Magento\Store\Model\Store;
@@ -23,17 +25,20 @@ class EmptyUrlKey
2325
private $progressIndex;
2426
private $productCollectionFactory;
2527
private $attributeScopeOverriddenValueFactory;
28+
private $configUtil;
2629

2730
public function __construct(
2831
StoresUtil $storesUtil,
2932
Progress $progress,
3033
ProductCollectionFactory $productCollectionFactory,
31-
AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory
34+
AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory,
35+
ConfigUtil $configUtil
3236
) {
3337
$this->storesUtil = $storesUtil;
3438
$this->progress = $progress;
3539
$this->productCollectionFactory = $productCollectionFactory;
3640
$this->attributeScopeOverriddenValueFactory = $attributeScopeOverriddenValueFactory;
41+
$this->configUtil = $configUtil;
3742

3843
$this->progressIndex = 0;
3944
}
@@ -84,6 +89,13 @@ private function checkForEmptyUrlKeyAttributeValues(): array
8489
], null, $joinType)
8590
;
8691

92+
if ($this->configUtil->getOnlyCheckVisibleProducts()) {
93+
$collection->addAttributeToFilter(
94+
ProductInterface::VISIBILITY,
95+
['neq' => ProductVisibility::VISIBILITY_NOT_VISIBLE]
96+
);
97+
}
98+
8799
if ($this->progressIndex === 0) {
88100
$this->progress->setGuestimatedSize(count($storeIds), $collection->getSize());
89101
}

Checker/Catalog/Product/UrlPath.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Product;
66

7+
use Baldwin\UrlDataIntegrityChecker\Util\Configuration as ConfigUtil;
78
use Baldwin\UrlDataIntegrityChecker\Util\Stores as StoresUtil;
89
use Magento\Catalog\Api\Data\ProductInterface;
910
use Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory as AttributeScopeOverriddenValueFactory;
1011
use Magento\Catalog\Model\Product as ProductModel;
12+
use Magento\Catalog\Model\Product\Visibility as ProductVisibility;
1113
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1214
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1315
use Magento\Store\Model\Store;
@@ -23,15 +25,18 @@ class UrlPath
2325
private $storesUtil;
2426
private $productCollectionFactory;
2527
private $attributeScopeOverriddenValueFactory;
28+
private $configUtil;
2629

2730
public function __construct(
2831
StoresUtil $storesUtil,
2932
ProductCollectionFactory $productCollectionFactory,
30-
AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory
33+
AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory,
34+
ConfigUtil $configUtil
3135
) {
3236
$this->storesUtil = $storesUtil;
3337
$this->productCollectionFactory = $productCollectionFactory;
3438
$this->attributeScopeOverriddenValueFactory = $attributeScopeOverriddenValueFactory;
39+
$this->configUtil = $configUtil;
3540
}
3641

3742
/**
@@ -64,6 +69,13 @@ private function checkForNonEmptyUrlPathAttributeValues(): array
6469
->addAttributeToFilter(self::URL_PATH_ATTRIBUTE, ['notnull' => true], $joinType)
6570
;
6671

72+
if ($this->configUtil->getOnlyCheckVisibleProducts()) {
73+
$collection->addAttributeToFilter(
74+
ProductInterface::VISIBILITY,
75+
['neq' => ProductVisibility::VISIBILITY_NOT_VISIBLE]
76+
);
77+
}
78+
6779
$productsWithProblems[] = $this->getProductsWithProblems($storeId, $collection);
6880
}
6981

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ In the Magento admin, you can find the results in:
6767
The results of the checkers are currently stored by default in the directory `var/tmp` as `.json` files.
6868
But you can change the path in the backend settings under Stores > Configuration > Catalog > Url Data Integrity Checker by entering a relative path starting from the Magento installation directory or an absolute path. The directory you enter there needs to exist before it will work.
6969

70+
You can configure this module to ignore problems with invisible products (via Stores > Configuration > Catalog > Url Data Integrity Checker). Because in recent versions of Magento, url rewrites for invisible products are not being generated, so if there are problems with the `url_path` or `url_key` attributes of such products, they should not cause issues with url rewrites. An additional benefit of this option is that it will use less time and less memory to run the product checkers. This option is disabled by default, so you'll need to enable it.
71+
7072
## Some screenshots
7173

7274
### Example of backend report for product url key problems

Test/Checker/Catalog/Product/UrlKey/DuplicateUrlKeyTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Product\UrlKey\DuplicateUrlKey as UrlKeyChecker;
88
use Baldwin\UrlDataIntegrityChecker\Console\Progress;
9+
use Baldwin\UrlDataIntegrityChecker\Util\Configuration as ConfigUtil;
910
use Baldwin\UrlDataIntegrityChecker\Util\Stores as StoresUtil;
1011
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue as AttributeScopeOverriddenValue;
1112
use Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory as AttributeScopeOverriddenValueFactory;
@@ -119,11 +120,18 @@ function ($productsData) {
119120
->method('create')
120121
->willReturn($attributeScopeOverriddenValueMock);
121122

123+
/** @var ConfigUtil&MockObject */
124+
$configUtilMock = $this
125+
->getMockBuilder(ConfigUtil::class)
126+
->disableOriginalConstructor()
127+
->getMock();
128+
122129
$urlKeyChecker = new UrlKeyChecker(
123130
$storesUtilMock,
124131
$progressMock,
125132
$productCollectionFactoryMock,
126-
$attributeScopeOverriddenValueFactoryMock
133+
$attributeScopeOverriddenValueFactoryMock,
134+
$configUtilMock
127135
);
128136
$results = $urlKeyChecker->execute();
129137

Util/Configuration.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baldwin\UrlDataIntegrityChecker\Util;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
9+
class Configuration
10+
{
11+
const CONFIG_ONLY_CHECK_VISIBLE_PRODUCTS = 'url_data_integrity_checker/configuration/only_check_visible_products';
12+
13+
private $scopeConfig;
14+
15+
public function __construct(
16+
ScopeConfigInterface $scopeConfig
17+
) {
18+
$this->scopeConfig = $scopeConfig;
19+
}
20+
21+
public function getOnlyCheckVisibleProducts(): bool
22+
{
23+
$configValue = (bool) $this->scopeConfig->getValue(
24+
self::CONFIG_ONLY_CHECK_VISIBLE_PRODUCTS
25+
);
26+
27+
return $configValue;
28+
}
29+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"magento/framework": "^100.1 || ^101.0 || ^102.0 || ^103.0",
1616
"magento/module-backend": "^100.1 || ^101.0 || ^102.0",
1717
"magento/module-catalog": "^101.0 || ^102.0 || ^103.0 || ^104.0",
18+
"magento/module-config": "^100.1 || ^101.0",
1819
"magento/module-cron": "^100.1",
1920
"magento/module-store": "^100.1 || ^101.0",
2021
"magento/module-ui": "^100.1 || ^101.0",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
<label>Path to store results</label>
1212
<comment>Path where to store json files, can be either an absolute or a relative path</comment>
1313
</field>
14+
<field id="only_check_visible_products" type="select" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0" translate="label">
15+
<label>Don't check for problems with invisible products</label>
16+
<comment>Sometimes you want to focus on products that are visible only, because Magento (at least in recent versions) only generated url rewrites for products that are visible. Be aware, after switching this config setting that you should re-run the checkers.</comment>
17+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
18+
</field>
1419
</group>
1520
</section>
1621
</system>

0 commit comments

Comments
 (0)