1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Rebuild Url Rewrite for magento 2 |
4
|
|
|
* Copyright (C) 2016 |
5
|
|
|
* |
6
|
|
|
* This file is part of DanielSousa/UrlRewrite. |
7
|
|
|
* |
8
|
|
|
* DanielSousa/UrlRewrite is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace DanielSousa\UrlRewrite\Console\Command; |
23
|
|
|
|
24
|
|
|
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class Products extends AbstractUrlRewriteCommand |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this->setName('urlrewrite:rebuild:products'); |
38
|
|
|
$this->setDescription('Rebuild Product URL Rewrites'); |
39
|
|
|
parent::configure(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
parent::execute($input,$output); |
49
|
|
|
try { |
50
|
|
|
$productCollection = $this->getProductCollection(); |
51
|
|
|
$this->progressBar->start($productCollection->getSize()); |
52
|
|
|
|
53
|
|
|
$this->getIterator()->walk( |
54
|
|
|
$productCollection->getSelect(), |
55
|
|
|
[[$this, 'callbackGenerateProductUrl']], |
56
|
|
|
[ |
57
|
|
|
'product' => $this->getProductFactory() |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$this->progressBar->finish(); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
$this->output->writeln($e->getMessage()); |
64
|
|
|
return \Magento\Framework\Console\Cli::RETURN_FAILURE; |
65
|
|
|
} |
66
|
|
|
return \Magento\Framework\Console\Cli::RETURN_SUCCESS; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generate product url by product |
72
|
|
|
* @param $args |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function callbackGenerateProductUrl($args) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (!isset($args['row']['entity_id'])) { |
77
|
|
|
$this->output->writeln('Id not found'); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
$id = $args['row']['entity_id']; |
81
|
|
|
$this->progressBar->setMessage($id); |
82
|
|
|
$this->progressBar->advance(); |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$product = clone $args['product']; |
86
|
|
|
$product->load($id); |
87
|
|
|
$product->setStoreId(null); |
88
|
|
|
$this->replaceUrls( |
89
|
|
|
$this->prepareUrls($product) |
90
|
|
|
); |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
$this->output->writeln($e->getMessage() . '- Product ID -' . $id); |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Remove Product urls |
102
|
|
|
* |
103
|
|
|
* @param $productId |
104
|
|
|
* @param null $storeId |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
protected function removeProductUrls($productId, $storeId = null) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
if (!is_numeric($productId)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
$data = [ |
113
|
|
|
UrlRewrite::ENTITY_ID => $productId, |
114
|
|
|
UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, |
115
|
|
|
UrlRewrite::REDIRECT_TYPE => 0, |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
if (!is_null($storeId)) { |
119
|
|
|
$data[] = [UrlRewrite::STORE_ID => $storeId]; |
120
|
|
|
} |
121
|
|
|
try { |
122
|
|
|
$this->getUrlPersist()->deleteByData($data); |
123
|
|
|
} catch (\Exception $e) { |
124
|
|
|
$this->output->writeln($e->getMessage()); |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Generate list of product urls |
132
|
|
|
* |
133
|
|
|
* @param $product |
134
|
|
|
* @return UrlRewrite[] |
135
|
|
|
*/ |
136
|
|
|
private function prepareUrls($product) |
137
|
|
|
{ |
138
|
|
|
return $this->getProductUrlRewriteGenerator()->generate($product); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.