11
11
use Magento \Catalog \Api \Data \ProductInterface ;
12
12
use Magento \Catalog \Model \Product \Gallery \MimeTypeExtensionMap ;
13
13
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
+ use Magento \Catalog \Api \CategoryLinkManagementInterface ;
14
15
use Magento \Eav \Model \Entity \Attribute \Exception as AttributeException ;
15
16
use Magento \Framework \Api \Data \ImageContentInterface ;
16
17
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
@@ -161,6 +162,16 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
161
162
*/
162
163
private $ readExtensions ;
163
164
165
+ /**
166
+ * @var CategoryLinkManagementInterface
167
+ */
168
+ private $ categoryLinkManagement ;
169
+
170
+ /**
171
+ * @var AssignProductToCategories
172
+ */
173
+ private $ assignProductToCategories = false ;
174
+
164
175
/**
165
176
* ProductRepository constructor.
166
177
* @param ProductFactory $productFactory
@@ -187,6 +198,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
187
198
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
188
199
* @param int $cacheLimit [optional]
189
200
* @param ReadExtensions|null $readExtensions
201
+ * @param Magento\Catalog\Api\CategoryLinkManagementInterface|null $categoryLinkManagement
190
202
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191
203
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
192
204
*/
@@ -214,7 +226,8 @@ public function __construct(
214
226
CollectionProcessorInterface $ collectionProcessor = null ,
215
227
\Magento \Framework \Serialize \Serializer \Json $ serializer = null ,
216
228
$ cacheLimit = 1000 ,
217
- ReadExtensions $ readExtensions = null
229
+ ReadExtensions $ readExtensions = null ,
230
+ CategoryLinkManagementInterface $ categoryLinkManagement = null
218
231
) {
219
232
$ this ->productFactory = $ productFactory ;
220
233
$ this ->collectionFactory = $ collectionFactory ;
@@ -239,6 +252,8 @@ public function __construct(
239
252
$ this ->cacheLimit = (int )$ cacheLimit ;
240
253
$ this ->readExtensions = $ readExtensions ?: \Magento \Framework \App \ObjectManager::getInstance ()
241
254
->get (ReadExtensions::class);
255
+ $ this ->categoryLinkManagement = $ categoryLinkManagement ?:\Magento \Framework \App \ObjectManager::getInstance ()
256
+ ->get (CategoryLinkManagementInterface::class);
242
257
}
243
258
244
259
/**
@@ -589,6 +604,7 @@ public function save(ProductInterface $product, $saveOptions = false)
589
604
$ extensionAttributes = $ product ->getExtensionAttributes ();
590
605
if (empty ($ extensionAttributes ->__toArray ())) {
591
606
$ product ->setExtensionAttributes ($ existingProduct ->getExtensionAttributes ());
607
+ $ this ->assignProductToCategories = true ;
592
608
}
593
609
} catch (NoSuchEntityException $ e ) {
594
610
$ existingProduct = null ;
@@ -626,6 +642,12 @@ public function save(ProductInterface $product, $saveOptions = false)
626
642
}
627
643
628
644
$ this ->saveProduct ($ product );
645
+ if ($ this ->assignProductToCategories === true ) {
646
+ $ this ->categoryLinkManagement ->assignProductToCategories (
647
+ $ product ->getSku (),
648
+ $ product ->getCategoryIds ()
649
+ );
650
+ }
629
651
$ this ->removeProductFromLocalCache ($ product ->getSku ());
630
652
unset($ this ->instancesById [$ product ->getId ()]);
631
653
0 commit comments