6
6
namespace Magento \Catalog \Model \Product ;
7
7
8
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
9
10
use Magento \Catalog \Model \Attribute \ScopeOverriddenValue ;
10
11
use Magento \Catalog \Model \Product ;
11
12
use Magento \Catalog \Model \Product \Attribute \Source \Status ;
12
13
use Magento \Catalog \Model \Product \Option \Repository as OptionRepository ;
13
14
use Magento \Catalog \Model \ProductFactory ;
14
- use Magento \Framework \ App \ ObjectManager ;
15
+ use Magento \Catalog \ Model \ ResourceModel \ DuplicatedProductAttributesCopier ;
15
16
use Magento \Framework \EntityManager \MetadataPool ;
16
17
use Magento \Store \Model \Store ;
17
18
use Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException ;
@@ -50,25 +51,41 @@ class Copier
50
51
*/
51
52
private $ scopeOverriddenValue ;
52
53
54
+ /**
55
+ * @var ProductRepositoryInterface
56
+ */
57
+ private $ productRepository ;
58
+
59
+ /**
60
+ * @var DuplicatedProductAttributesCopier
61
+ */
62
+ private $ attributeCopier ;
63
+
53
64
/**
54
65
* @param CopyConstructorInterface $copyConstructor
55
66
* @param ProductFactory $productFactory
56
67
* @param ScopeOverriddenValue $scopeOverriddenValue
57
68
* @param OptionRepository|null $optionRepository
58
69
* @param MetadataPool|null $metadataPool
70
+ * @param ProductRepositoryInterface $productRepository
71
+ * @param DuplicatedProductAttributesCopier $attributeCopier
59
72
*/
60
73
public function __construct (
61
74
CopyConstructorInterface $ copyConstructor ,
62
75
ProductFactory $ productFactory ,
63
76
ScopeOverriddenValue $ scopeOverriddenValue ,
64
77
OptionRepository $ optionRepository ,
65
- MetadataPool $ metadataPool
78
+ MetadataPool $ metadataPool ,
79
+ ProductRepositoryInterface $ productRepository ,
80
+ DuplicatedProductAttributesCopier $ attributeCopier
66
81
) {
67
82
$ this ->productFactory = $ productFactory ;
68
83
$ this ->copyConstructor = $ copyConstructor ;
69
84
$ this ->scopeOverriddenValue = $ scopeOverriddenValue ;
70
85
$ this ->optionRepository = $ optionRepository ;
71
86
$ this ->metadataPool = $ metadataPool ;
87
+ $ this ->productRepository = $ productRepository ;
88
+ $ this ->attributeCopier = $ attributeCopier ;
72
89
}
73
90
74
91
/**
@@ -79,11 +96,13 @@ public function __construct(
79
96
*/
80
97
public function copy (Product $ product ): Product
81
98
{
82
- $ product ->getWebsiteIds ();
83
- $ product ->getCategoryIds ();
84
-
85
99
$ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
86
100
101
+ /* Regardless in what scope the product was provided,
102
+ for duplicating we want to clone product in Global scope first */
103
+ if ((int )$ product ->getStoreId () !== Store::DEFAULT_STORE_ID ) {
104
+ $ product = $ this ->productRepository ->getById ($ product ->getId (), true , Store::DEFAULT_STORE_ID );
105
+ }
87
106
/** @var Product $duplicate */
88
107
$ duplicate = $ this ->productFactory ->create ();
89
108
$ productData = $ product ->getData ();
@@ -102,6 +121,7 @@ public function copy(Product $product): Product
102
121
$ duplicate ->setStoreId (Store::DEFAULT_STORE_ID );
103
122
$ this ->copyConstructor ->build ($ product , $ duplicate );
104
123
$ this ->setDefaultUrl ($ product , $ duplicate );
124
+ $ this ->attributeCopier ->copyProductAttributes ($ product , $ duplicate );
105
125
$ this ->setStoresUrl ($ product , $ duplicate );
106
126
$ this ->optionRepository ->duplicate ($ product , $ duplicate );
107
127
0 commit comments