1
1
<?php
2
2
/**
3
- * Catalog product copier. Creates product duplicate
4
- *
5
3
* Copyright © Magento, Inc. All rights reserved.
6
4
* See COPYING.txt for license details.
7
5
*/
11
9
use Magento \Catalog \Model \Product ;
12
10
13
11
/**
14
- * The copier creates product duplicates.
12
+ * Catalog product copier.
13
+ *
14
+ * Creates product duplicate.
15
+ *
16
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
17
*/
16
18
class Copier
17
19
{
@@ -74,22 +76,9 @@ public function copy(Product $product)
74
76
$ duplicate ->setUpdatedAt (null );
75
77
$ duplicate ->setId (null );
76
78
$ duplicate ->setStoreId (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
77
-
78
79
$ this ->copyConstructor ->build ($ product , $ duplicate );
79
- $ isDuplicateSaved = false ;
80
- do {
81
- $ urlKey = $ duplicate ->getUrlKey ();
82
- $ urlKey = preg_match ('/(.*)-(\d+)$/ ' , $ urlKey , $ matches )
83
- ? $ matches [1 ] . '- ' . ($ matches [2 ] + 1 )
84
- : $ urlKey . '-1 ' ;
85
- $ duplicate ->setUrlKey ($ urlKey );
86
- $ duplicate ->setData ('url_path ' , null );
87
- try {
88
- $ duplicate ->save ();
89
- $ isDuplicateSaved = true ;
90
- } catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
91
- }
92
- } while (!$ isDuplicateSaved );
80
+ $ this ->setDefaultUrl ($ product , $ duplicate );
81
+ $ this ->setStoresUrl ($ product , $ duplicate );
93
82
$ this ->getOptionRepository ()->duplicate ($ product , $ duplicate );
94
83
$ product ->getResource ()->duplicate (
95
84
$ product ->getData ($ metadata ->getLinkField ()),
@@ -98,6 +87,81 @@ public function copy(Product $product)
98
87
return $ duplicate ;
99
88
}
100
89
90
+ /**
91
+ * Set default URL.
92
+ *
93
+ * @param Product $product
94
+ * @param Product $duplicate
95
+ * @return void
96
+ */
97
+ private function setDefaultUrl (Product $ product , Product $ duplicate ) : void
98
+ {
99
+ $ duplicate ->setStoreId (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
100
+ $ resource = $ product ->getResource ();
101
+ $ attribute = $ resource ->getAttribute ('url_key ' );
102
+ $ productId = $ product ->getId ();
103
+ $ urlKey = $ resource ->getAttributeRawValue ($ productId , 'url_key ' , \Magento \Store \Model \Store::DEFAULT_STORE_ID );
104
+ do {
105
+ $ urlKey = $ this ->modifyUrl ($ urlKey );
106
+ $ duplicate ->setUrlKey ($ urlKey );
107
+ } while (!$ attribute ->getEntity ()->checkAttributeUniqueValue ($ attribute , $ duplicate ));
108
+ $ duplicate ->setData ('url_path ' , null );
109
+ $ duplicate ->save ();
110
+ }
111
+
112
+ /**
113
+ * Set URL for each store.
114
+ *
115
+ * @param Product $product
116
+ * @param Product $duplicate
117
+ * @return void
118
+ */
119
+ private function setStoresUrl (Product $ product , Product $ duplicate ) : void
120
+ {
121
+ $ storeIds = $ duplicate ->getStoreIds ();
122
+ $ productId = $ product ->getId ();
123
+ $ productResource = $ product ->getResource ();
124
+ $ defaultUrlKey = $ productResource ->getAttributeRawValue (
125
+ $ productId ,
126
+ 'url_key ' ,
127
+ \Magento \Store \Model \Store::DEFAULT_STORE_ID
128
+ );
129
+ $ duplicate ->setData ('save_rewrites_history ' , false );
130
+ foreach ($ storeIds as $ storeId ) {
131
+ $ isDuplicateSaved = false ;
132
+ $ duplicate ->setStoreId ($ storeId );
133
+ $ urlKey = $ productResource ->getAttributeRawValue ($ productId , 'url_key ' , $ storeId );
134
+ if ($ urlKey === $ defaultUrlKey ) {
135
+ continue ;
136
+ }
137
+ do {
138
+ $ urlKey = $ this ->modifyUrl ($ urlKey );
139
+ $ duplicate ->setUrlKey ($ urlKey );
140
+ $ duplicate ->setData ('url_path ' , null );
141
+ try {
142
+ $ duplicate ->save ();
143
+ $ isDuplicateSaved = true ;
144
+ // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
145
+ } catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
146
+ }
147
+ } while (!$ isDuplicateSaved );
148
+ }
149
+ $ duplicate ->setStoreId (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
150
+ }
151
+
152
+ /**
153
+ * Modify URL key.
154
+ *
155
+ * @param string $urlKey
156
+ * @return string
157
+ */
158
+ private function modifyUrl (string $ urlKey ) : string
159
+ {
160
+ return preg_match ('/(.*)-(\d+)$/ ' , $ urlKey , $ matches )
161
+ ? $ matches [1 ] . '- ' . ($ matches [2 ] + 1 )
162
+ : $ urlKey . '-1 ' ;
163
+ }
164
+
101
165
/**
102
166
* Returns product option repository.
103
167
*
0 commit comments