8
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
9
use Magento \Catalog \Model \Attribute \ScopeOverriddenValue ;
10
10
use Magento \Catalog \Model \Product ;
11
+ use Magento \Catalog \Model \Product \Attribute \Source \Status ;
11
12
use Magento \Catalog \Model \Product \Option \Repository as OptionRepository ;
12
13
use Magento \Catalog \Model \ProductFactory ;
13
14
use Magento \Framework \App \ObjectManager ;
14
15
use Magento \Framework \EntityManager \MetadataPool ;
16
+ use Magento \Store \Model \Store ;
15
17
use Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException ;
16
18
17
19
/**
@@ -72,40 +74,37 @@ public function __construct(
72
74
/**
73
75
* Create product duplicate
74
76
*
75
- * @param \Magento\Catalog\Model\Product $product
76
- *
77
- * @return \Magento\Catalog\Model\Product
78
- *
79
- * @throws \Exception
77
+ * @param Product $product
78
+ * @return Product
80
79
*/
81
- public function copy (Product $ product )
80
+ public function copy (Product $ product ): Product
82
81
{
83
82
$ product ->getWebsiteIds ();
84
83
$ product ->getCategoryIds ();
85
84
86
85
$ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
87
86
88
- /** @var \Magento\Catalog\Model\ Product $duplicate */
87
+ /** @var Product $duplicate */
89
88
$ duplicate = $ this ->productFactory ->create ();
90
89
$ productData = $ product ->getData ();
91
90
$ productData = $ this ->removeStockItem ($ productData );
92
91
$ duplicate ->setData ($ productData );
93
92
$ duplicate ->setOptions ([]);
93
+ $ duplicate ->setMetaTitle (null );
94
+ $ duplicate ->setMetaKeyword (null );
95
+ $ duplicate ->setMetaDescription (null );
94
96
$ duplicate ->setIsDuplicate (true );
95
97
$ duplicate ->setOriginalLinkId ($ product ->getData ($ metadata ->getLinkField ()));
96
- $ duplicate ->setStatus (\ Magento \ Catalog \ Model \ Product \ Attribute \ Source \ Status::STATUS_DISABLED );
98
+ $ duplicate ->setStatus (Status::STATUS_DISABLED );
97
99
$ duplicate ->setCreatedAt (null );
98
100
$ duplicate ->setUpdatedAt (null );
99
101
$ duplicate ->setId (null );
100
- $ duplicate ->setStoreId (\ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID );
102
+ $ duplicate ->setStoreId (Store::DEFAULT_STORE_ID );
101
103
$ this ->copyConstructor ->build ($ product , $ duplicate );
102
104
$ this ->setDefaultUrl ($ product , $ duplicate );
103
105
$ this ->setStoresUrl ($ product , $ duplicate );
104
106
$ this ->optionRepository ->duplicate ($ product , $ duplicate );
105
- $ product ->getResource ()->duplicate (
106
- $ product ->getData ($ metadata ->getLinkField ()),
107
- $ duplicate ->getData ($ metadata ->getLinkField ())
108
- );
107
+
109
108
return $ duplicate ;
110
109
}
111
110
@@ -118,11 +117,11 @@ public function copy(Product $product)
118
117
*/
119
118
private function setDefaultUrl (Product $ product , Product $ duplicate ) : void
120
119
{
121
- $ duplicate ->setStoreId (\ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID );
120
+ $ duplicate ->setStoreId (Store::DEFAULT_STORE_ID );
122
121
$ resource = $ product ->getResource ();
123
122
$ attribute = $ resource ->getAttribute ('url_key ' );
124
123
$ productId = $ product ->getId ();
125
- $ urlKey = $ resource ->getAttributeRawValue ($ productId , 'url_key ' , \ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID );
124
+ $ urlKey = $ resource ->getAttributeRawValue ($ productId , 'url_key ' , Store::DEFAULT_STORE_ID );
126
125
do {
127
126
$ urlKey = $ this ->modifyUrl ($ urlKey );
128
127
$ duplicate ->setUrlKey ($ urlKey );
@@ -175,7 +174,7 @@ private function setStoresUrl(Product $product, Product $duplicate) : void
175
174
$ productResource ->saveAttribute ($ duplicate , 'url_path ' );
176
175
$ productResource ->saveAttribute ($ duplicate , 'url_key ' );
177
176
}
178
- $ duplicate ->setStoreId (\ Magento \ Store \ Model \ Store::DEFAULT_STORE_ID );
177
+ $ duplicate ->setStoreId (Store::DEFAULT_STORE_ID );
179
178
}
180
179
181
180
/**
@@ -197,7 +196,7 @@ private function modifyUrl(string $urlKey) : string
197
196
* @param array $productData
198
197
* @return array
199
198
*/
200
- private function removeStockItem (array $ productData )
199
+ private function removeStockItem (array $ productData ): array
201
200
{
202
201
if (isset ($ productData [ProductInterface::EXTENSION_ATTRIBUTES_KEY ])) {
203
202
$ extensionAttributes = $ productData [ProductInterface::EXTENSION_ATTRIBUTES_KEY ];
0 commit comments