@@ -74,22 +74,9 @@ public function copy(Product $product)
74
74
$ duplicate ->setUpdatedAt (null );
75
75
$ duplicate ->setId (null );
76
76
$ duplicate ->setStoreId (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
77
-
78
77
$ 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 );
78
+ $ this ->setDefaultUrl ($ product , $ duplicate );
79
+ $ this ->setStoresUrl ($ product , $ duplicate );
93
80
$ this ->getOptionRepository ()->duplicate ($ product , $ duplicate );
94
81
$ product ->getResource ()->duplicate (
95
82
$ product ->getData ($ metadata ->getLinkField ()),
@@ -98,6 +85,80 @@ public function copy(Product $product)
98
85
return $ duplicate ;
99
86
}
100
87
88
+ /**
89
+ * Set default URL.
90
+ *
91
+ * @param Product $product
92
+ * @param Product $duplicate
93
+ * @return void
94
+ */
95
+ private function setDefaultUrl (Product $ product , Product $ duplicate ) : void
96
+ {
97
+ $ duplicate ->setStoreId (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
98
+ $ resource = $ product ->getResource ();
99
+ $ attribute = $ resource ->getAttribute ('url_key ' );
100
+ $ productId = $ product ->getId ();
101
+ $ urlKey = $ resource ->getAttributeRawValue ($ productId , 'url_key ' , \Magento \Store \Model \Store::DEFAULT_STORE_ID );
102
+ do {
103
+ $ urlKey = $ this ->modifyUrl ($ urlKey );
104
+ $ duplicate ->setUrlKey ($ urlKey );
105
+ } while (!$ attribute ->getEntity ()->checkAttributeUniqueValue ($ attribute , $ duplicate ));
106
+ $ duplicate ->setData ('url_path ' , null );
107
+ $ duplicate ->save ();
108
+ }
109
+
110
+ /**
111
+ * Set URL for each store.
112
+ *
113
+ * @param Product $product
114
+ * @param Product $duplicate
115
+ * @return void
116
+ */
117
+ private function setStoresUrl (Product $ product , Product $ duplicate ) : void
118
+ {
119
+ $ storeIds = $ duplicate ->getStoreIds ();
120
+ $ productId = $ product ->getId ();
121
+ $ productResource = $ product ->getResource ();
122
+ $ defaultUrlKey = $ productResource ->getAttributeRawValue (
123
+ $ productId ,
124
+ 'url_key ' ,
125
+ \Magento \Store \Model \Store::DEFAULT_STORE_ID
126
+ );
127
+ $ duplicate ->setData ('save_rewrites_history ' , false );
128
+ foreach ($ storeIds as $ storeId ) {
129
+ $ isDuplicateSaved = false ;
130
+ $ duplicate ->setStoreId ($ storeId );
131
+ $ urlKey = $ productResource ->getAttributeRawValue ($ productId , 'url_key ' , $ storeId );
132
+ if ($ urlKey === $ defaultUrlKey ) {
133
+ continue ;
134
+ }
135
+ do {
136
+ $ urlKey = $ this ->modifyUrl ($ urlKey );
137
+ $ duplicate ->setUrlKey ($ urlKey );
138
+ $ duplicate ->setData ('url_path ' , null );
139
+ try {
140
+ $ duplicate ->save ();
141
+ $ isDuplicateSaved = true ;
142
+ } catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
143
+ }
144
+ } while (!$ isDuplicateSaved );
145
+ }
146
+ $ duplicate ->setStoreId (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
147
+ }
148
+
149
+ /**
150
+ * Modify URL key.
151
+ *
152
+ * @param string $urlKey
153
+ * @return string
154
+ */
155
+ private function modifyUrl (string $ urlKey ) : string
156
+ {
157
+ return preg_match ('/(.*)-(\d+)$/ ' , $ urlKey , $ matches )
158
+ ? $ matches [1 ] . '- ' . ($ matches [2 ] + 1 )
159
+ : $ urlKey . '-1 ' ;
160
+ }
161
+
101
162
/**
102
163
* Returns product option repository.
103
164
*
0 commit comments