Skip to content

Commit 27dd58a

Browse files
committed
added Patch for update product url_key
1 parent 55c2266 commit 27dd58a

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Setup\Patch\Data;
9+
10+
use Magento\Catalog\Model\Product\Url;
11+
use Magento\Eav\Setup\EavSetup;
12+
use Magento\Eav\Setup\EavSetupFactory;
13+
use Magento\Framework\Setup\ModuleDataSetupInterface;
14+
use Magento\Framework\Setup\Patch\DataPatchInterface;
15+
use Magento\Framework\Setup\Patch\PatchVersionInterface;
16+
17+
/**
18+
* Update url_key all products.
19+
*/
20+
class UpdateUrlKeyForProducts implements DataPatchInterface, PatchVersionInterface
21+
{
22+
/**
23+
* @var ModuleDataSetupInterface
24+
*/
25+
private $moduleDataSetup;
26+
27+
/**
28+
* @var EavSetup
29+
*/
30+
private $eavSetup;
31+
32+
/**
33+
* @var Url
34+
*/
35+
private $urlProduct;
36+
37+
/**
38+
* @param ModuleDataSetupInterface $moduleDataSetup
39+
* @param EavSetupFactory $eavSetupFactory
40+
* @param Url $urlProduct
41+
*/
42+
public function __construct(
43+
ModuleDataSetupInterface $moduleDataSetup,
44+
EavSetupFactory $eavSetupFactory,
45+
Url $urlProduct
46+
) {
47+
$this->moduleDataSetup = $moduleDataSetup;
48+
$this->eavSetup = $eavSetupFactory->create(['setup' => $moduleDataSetup]);
49+
$this->urlProduct = $urlProduct;
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function apply()
56+
{
57+
$productTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
58+
$table = $this->moduleDataSetup->getTable('catalog_product_entity_varchar');
59+
$select = $this->moduleDataSetup->getConnection()->select()->from(
60+
$table,
61+
['value_id', 'value']
62+
)->where(
63+
'attribute_id = ?',
64+
$this->eavSetup->getAttributeId($productTypeId, 'url_key')
65+
);
66+
67+
$result = $this->moduleDataSetup->getConnection()->fetchAll($select);
68+
foreach ($result as $key => $item) {
69+
$result[$key]['value'] = $this->urlProduct->formatUrlKey($item['value']);
70+
}
71+
72+
foreach (array_chunk($result, 500, true) as $pathResult) {
73+
$this->moduleDataSetup->getConnection()->insertOnDuplicate($table, $pathResult, ['value']);
74+
}
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* @inheritDoc
81+
*/
82+
public static function getVersion()
83+
{
84+
return "2.4.0";
85+
}
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
public static function getDependencies()
91+
{
92+
return [];
93+
}
94+
95+
/**
96+
* @inheritdoc
97+
*/
98+
public function getAliases()
99+
{
100+
return [];
101+
}
102+
}

0 commit comments

Comments
 (0)