/
CategoryLinkManagement.php
125 lines (111 loc) · 4.47 KB
/
CategoryLinkManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model;
/**
* Class CategoryLinkManagement
*/
class CategoryLinkManagement implements \Magento\Catalog\Api\CategoryLinkManagementInterface
{
/**
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var ResourceModel\Product
*/
protected $productResource;
/**
* @var \Magento\Catalog\Api\CategoryLinkRepositoryInterface
*/
protected $categoryLinkRepository;
/**
* @var \Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory
*/
protected $productLinkFactory;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
/**
* CategoryLinkManagement constructor.
*
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param ResourceModel\Product $productResource
* @param \Magento\Catalog\Api\CategoryLinkRepositoryInterface $categoryLinkRepository
* @param \Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory $productLinkFactory
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
*/
public function __construct(
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
ResourceModel\Product $productResource,
\Magento\Catalog\Api\CategoryLinkRepositoryInterface $categoryLinkRepository,
\Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory $productLinkFactory,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->productResource = $productResource;
$this->categoryLinkRepository = $categoryLinkRepository;
$this->productLinkFactory = $productLinkFactory;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function getAssignedProducts($categoryId)
{
$category = $this->categoryRepository->get($categoryId);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
$products = $category->getProductCollection();
$products->addFieldToSelect('position');
/** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface[] $links */
$links = [];
/** @var \Magento\Catalog\Model\Product $product */
foreach ($products->getItems() as $product) {
/** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $link */
$link = $this->productLinkFactory->create();
$link->setSku($product->getSku())
->setPosition($product->getData('cat_index_position'))
->setCategoryId($category->getId());
$links[] = $link;
}
return $links;
}
/**
* Assign product to given categories
*
* @param string $productSku
* @param \int[] $categoryIds
* @return bool
*/
public function assignProductToCategories($productSku, array $categoryIds)
{
$product = $this->productRepository->get($productSku);
$assignedCategories = $this->productResource->getCategoryIds($product);
foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) {
$this->categoryLinkRepository->deleteByIds($categoryId, $productSku);
}
foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) {
/** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $categoryProductLink */
$categoryProductLink = $this->productLinkFactory->create();
$categoryProductLink->setSku($productSku);
$categoryProductLink->setCategoryId($categoryId);
$categoryProductLink->setPosition(0);
$this->categoryLinkRepository->save($categoryProductLink);
}
$productCategoryIndexer = $this->indexerRegistry->get(Indexer\Product\Category::INDEXER_ID);
if (!$productCategoryIndexer->isScheduled()) {
$productCategoryIndexer->reindexRow($product->getId());
}
return true;
}
}