Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryLinkManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Catalog\Model;
8 
13 {
18 
22  protected $productRepository;
23 
27  protected $productResource;
28 
33 
38 
42  protected $indexerRegistry;
43 
50  public function __construct(
52  \Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory $productLinkFactory
53  ) {
54  $this->categoryRepository = $categoryRepository;
55  $this->productLinkFactory = $productLinkFactory;
56  }
57 
61  public function getAssignedProducts($categoryId)
62  {
63  $category = $this->categoryRepository->get($categoryId);
64 
66  $products = $category->getProductCollection();
67  $products->addFieldToSelect('position');
68 
70  $links = [];
71 
73  foreach ($products->getItems() as $product) {
75  $link = $this->productLinkFactory->create();
76  $link->setSku($product->getSku())
77  ->setPosition($product->getData('cat_index_position'))
78  ->setCategoryId($category->getId());
79  $links[] = $link;
80  }
81  return $links;
82  }
83 
91  public function assignProductToCategories($productSku, array $categoryIds)
92  {
93  $product = $this->getProductRepository()->get($productSku);
94  $assignedCategories = $this->getProductResource()->getCategoryIds($product);
95  foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) {
96  $this->getCategoryLinkRepository()->deleteByIds($categoryId, $productSku);
97  }
98 
99  foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) {
101  $categoryProductLink = $this->productLinkFactory->create();
102  $categoryProductLink->setSku($productSku);
103  $categoryProductLink->setCategoryId($categoryId);
104  $categoryProductLink->setPosition(0);
105  $this->getCategoryLinkRepository()->save($categoryProductLink);
106  }
107  $productCategoryIndexer = $this->getIndexerRegistry()->get(Indexer\Product\Category::INDEXER_ID);
108  if (!$productCategoryIndexer->isScheduled()) {
109  $productCategoryIndexer->reindexRow($product->getId());
110  }
111  return true;
112  }
113 
119  private function getProductRepository()
120  {
121  if (null === $this->productRepository) {
122  $this->productRepository = \Magento\Framework\App\ObjectManager::getInstance()
123  ->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
124  }
126  }
127 
133  private function getProductResource()
134  {
135  if (null === $this->productResource) {
136  $this->productResource = \Magento\Framework\App\ObjectManager::getInstance()
137  ->get(\Magento\Catalog\Model\ResourceModel\Product::class);
138  }
139  return $this->productResource;
140  }
141 
147  private function getCategoryLinkRepository()
148  {
149  if (null === $this->categoryLinkRepository) {
150  $this->categoryLinkRepository = \Magento\Framework\App\ObjectManager::getInstance()
151  ->get(\Magento\Catalog\Api\CategoryLinkRepositoryInterface::class);
152  }
154  }
155 
161  private function getIndexerRegistry()
162  {
163  if (null === $this->indexerRegistry) {
164  $this->indexerRegistry = \Magento\Framework\App\ObjectManager::getInstance()
165  ->get(\Magento\Framework\Indexer\IndexerRegistry::class);
166  }
167  return $this->indexerRegistry;
168  }
169 }