Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
CategoryLinkRepository Class Reference
Inheritance diagram for CategoryLinkRepository:
CategoryLinkRepositoryInterface

Public Member Functions

 __construct (\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
 
 save (\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
 
 delete (\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
 
 deleteByIds ($categoryId, $sku)
 

Protected Attributes

 $categoryRepository
 
 $productRepository
 

Detailed Description

Definition at line 12 of file CategoryLinkRepository.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Api\CategoryRepositoryInterface  $categoryRepository,
\Magento\Catalog\Api\ProductRepositoryInterface  $productRepository 
)
Parameters
\Magento\Catalog\Api\CategoryRepositoryInterface$categoryRepository
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository

Definition at line 28 of file CategoryLinkRepository.php.

Member Function Documentation

◆ delete()

{Remove the product assignment from the category

Parameters
\Magento\Catalog\Api\Data\CategoryProductLinkInterface$productLink
Returns
bool will returned True if products successfully deleted
Exceptions
}

Implements CategoryLinkRepositoryInterface.

Definition at line 65 of file CategoryLinkRepository.php.

66  {
67  return $this->deleteByIds($productLink->getCategoryId(), $productLink->getSku());
68  }

◆ deleteByIds()

deleteByIds (   $categoryId,
  $sku 
)

{Remove the product assignment from the category by category id and sku

Parameters
string$sku
string$sku
Returns
bool will returned True if products successfully deleted
Exceptions
}

Implements CategoryLinkRepositoryInterface.

Definition at line 73 of file CategoryLinkRepository.php.

74  {
75  $category = $this->categoryRepository->get($categoryId);
76  $product = $this->productRepository->get($sku);
77  $productPositions = $category->getProductsPosition();
78 
79  $productID = $product->getId();
80  if (!isset($productPositions[$productID])) {
81  throw new InputException(__("The category doesn't contain the specified product."));
82  }
83  $backupPosition = $productPositions[$productID];
84  unset($productPositions[$productID]);
85 
86  $category->setPostedProducts($productPositions);
87  try {
88  $category->save();
89  } catch (\Exception $e) {
90  throw new CouldNotSaveException(
91  __(
92  'Could not save product "%product" with position %position to category %category',
93  [
94  "product" => $product->getId(),
95  "position" => $backupPosition,
96  "category" => $category->getId()
97  ]
98  ),
99  $e
100  );
101  }
102  return true;
103  }
__()
Definition: __.php:13

◆ save()

{Assign a product to the required category

Parameters
\Magento\Catalog\Api\Data\CategoryProductLinkInterface$productLink
Returns
bool will returned True if assigned
Exceptions
}

Implements CategoryLinkRepositoryInterface.

Definition at line 39 of file CategoryLinkRepository.php.

40  {
41  $category = $this->categoryRepository->get($productLink->getCategoryId());
42  $product = $this->productRepository->get($productLink->getSku());
43  $productPositions = $category->getProductsPosition();
44  $productPositions[$product->getId()] = $productLink->getPosition();
45  $category->setPostedProducts($productPositions);
46  try {
47  $category->save();
48  } catch (\Exception $e) {
49  throw new CouldNotSaveException(
50  __(
51  'Could not save product "%1" with position %2 to category %3',
52  $product->getId(),
53  $productLink->getPosition(),
54  $category->getId()
55  ),
56  $e
57  );
58  }
59  return true;
60  }
__()
Definition: __.php:13

Field Documentation

◆ $categoryRepository

$categoryRepository
protected

Definition at line 17 of file CategoryLinkRepository.php.

◆ $productRepository

$productRepository
protected

Definition at line 22 of file CategoryLinkRepository.php.


The documentation for this class was generated from the following file: