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
*/
8
6
namespace Magento \Catalog \Model \Product ;
9
7
10
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
+ use Magento \Catalog \Model \Product ;
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \EntityManager \MetadataPool ;
12
+ use Magento \Catalog \Model \ProductFactory ;
13
+ use Magento \Catalog \Model \Product \Option \Repository as OptionRepository ;
11
14
15
+ /**
16
+ * Catalog product copier. Creates product duplicate
17
+ */
12
18
class Copier
13
19
{
14
20
/**
15
- * @var Option\Repository
21
+ * @var OptionRepository
16
22
*/
17
23
protected $ optionRepository ;
18
24
@@ -22,22 +28,22 @@ class Copier
22
28
protected $ copyConstructor ;
23
29
24
30
/**
25
- * @var \Magento\Catalog\Model\ ProductFactory
31
+ * @var ProductFactory
26
32
*/
27
33
protected $ productFactory ;
28
34
29
35
/**
30
- * @var \Magento\Framework\EntityManager\ MetadataPool
36
+ * @var MetadataPool
31
37
*/
32
38
protected $ metadataPool ;
33
39
34
40
/**
35
41
* @param CopyConstructorInterface $copyConstructor
36
- * @param \Magento\Catalog\Model\ ProductFactory $productFactory
42
+ * @param ProductFactory $productFactory
37
43
*/
38
44
public function __construct (
39
45
CopyConstructorInterface $ copyConstructor ,
40
- \ Magento \ Catalog \ Model \ ProductFactory $ productFactory
46
+ ProductFactory $ productFactory
41
47
) {
42
48
$ this ->productFactory = $ productFactory ;
43
49
$ this ->copyConstructor = $ copyConstructor ;
@@ -46,18 +52,16 @@ public function __construct(
46
52
/**
47
53
* Create product duplicate
48
54
*
49
- * @param \Magento\Catalog\Model\ Product $product
50
- * @return \Magento\Catalog\Model\ Product
55
+ * @param Product $product
56
+ * @return Product
51
57
*/
52
- public function copy (\ Magento \ Catalog \ Model \ Product $ product )
58
+ public function copy (Product $ product )
53
59
{
54
60
$ product ->getWebsiteIds ();
55
61
$ product ->getCategoryIds ();
56
62
57
- /** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
58
63
$ metadata = $ this ->getMetadataPool ()->getMetadata (ProductInterface::class);
59
64
60
- /** @var \Magento\Catalog\Model\Product $duplicate */
61
65
$ duplicate = $ this ->productFactory ->create ();
62
66
$ productData = $ product ->getData ();
63
67
$ productData = $ this ->removeStockItem ($ productData );
@@ -83,6 +87,7 @@ public function copy(\Magento\Catalog\Model\Product $product)
83
87
$ duplicate ->save ();
84
88
$ isDuplicateSaved = true ;
85
89
} catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
90
+ } catch (\Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException $ e ) {
86
91
}
87
92
} while (!$ isDuplicateSaved );
88
93
$ this ->getOptionRepository ()->duplicate ($ product , $ duplicate );
@@ -94,27 +99,25 @@ public function copy(\Magento\Catalog\Model\Product $product)
94
99
}
95
100
96
101
/**
97
- * @return Option\Repository
102
+ * @return OptionRepository
98
103
* @deprecated 101.0.0
99
104
*/
100
105
private function getOptionRepository ()
101
106
{
102
107
if (null === $ this ->optionRepository ) {
103
- $ this ->optionRepository = \Magento \Framework \App \ObjectManager::getInstance ()
104
- ->get (\Magento \Catalog \Model \Product \Option \Repository::class);
108
+ $ this ->optionRepository = ObjectManager::getInstance ()->get (OptionRepository::class);
105
109
}
106
110
return $ this ->optionRepository ;
107
111
}
108
112
109
113
/**
110
- * @return \Magento\Framework\EntityManager\ MetadataPool
114
+ * @return MetadataPool
111
115
* @deprecated 101.0.0
112
116
*/
113
117
private function getMetadataPool ()
114
118
{
115
119
if (null === $ this ->metadataPool ) {
116
- $ this ->metadataPool = \Magento \Framework \App \ObjectManager::getInstance ()
117
- ->get (\Magento \Framework \EntityManager \MetadataPool::class);
120
+ $ this ->metadataPool = ObjectManager::getInstance ()->get (MetadataPool::class);
118
121
}
119
122
return $ this ->metadataPool ;
120
123
}
0 commit comments