Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#26800 Fixed Undefined variable in ProductLink/Management #26979

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ProductLinkManagementInterface
*
* @param string $sku
* @param string $type
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return \Magento\Catalog\Api\Data\ProductLinkInterface[]
*/
public function getLinkedItemsByType($sku, $type);
Expand All @@ -28,6 +29,7 @@ public function getLinkedItemsByType($sku, $type);
* @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $items
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @return bool
*/
public function setProductLinks($sku, array $items);
Expand Down
68 changes: 34 additions & 34 deletions app/code/Magento/Catalog/Model/ProductLink/Management.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,42 @@

namespace Magento\Catalog\Model\ProductLink;

use Magento\Catalog\Api\Data;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\LinkTypeProvider;
use Magento\Catalog\Api\ProductLinkManagementInterface;

class Management implements \Magento\Catalog\Api\ProductLinkManagementInterface
/**
* Manage product links from api
*/
class Management implements ProductLinkManagementInterface
{
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
* @var ProductRepositoryInterface
*/
protected $productRepository;

/**
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
* @var LinkTypeProvider
*/
protected $linkTypeProvider;

/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
* @param ProductRepositoryInterface $productRepository
* @param LinkTypeProvider $linkTypeProvider
*/
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
ProductRepositoryInterface $productRepository,
LinkTypeProvider $linkTypeProvider
) {
$this->productRepository = $productRepository;
$this->linkTypeProvider = $linkTypeProvider;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getLinkedItemsByType($sku, $type)
{
Expand All @@ -63,47 +68,42 @@ public function getLinkedItemsByType($sku, $type)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setProductLinks($sku, array $items)
{

if (empty($items)) {
throw InputException::invalidFieldValue('items', 'empty array');
}

$linkTypes = $this->linkTypeProvider->getLinkTypes();

// Check if product link type is set and correct
if (!empty($items)) {
foreach ($items as $newLink) {
$type = $newLink->getLinkType();
if ($type == null) {
throw InputException::requiredField("linkType");
}
if (!isset($linkTypes[$type])) {
throw new NoSuchEntityException(
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
);
}
foreach ($items as $newLink) {
$type = $newLink->getLinkType();
if ($type == null) {
throw InputException::requiredField("linkType");
}
if (!isset($linkTypes[$type])) {
throw new NoSuchEntityException(
__('The "%1" link type wasn\'t found. Verify the type and try again.', $type)
);
}
}

$product = $this->productRepository->get($sku);

// Replace only links of the specified type
$existingLinks = $product->getProductLinks();
$newLinks = [];
if (!empty($existingLinks)) {
foreach ($existingLinks as $link) {
if ($link->getLinkType() != $type) {
$newLinks[] = $link;
}
}
$newLinks = array_merge($newLinks, $items);
} else {
$newLinks = $items;
}
$newLinks = array_merge($existingLinks, $items);

$product->setProductLinks($newLinks);
try {
$this->productRepository->save($product);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('The linked products data is invalid. Verify the data and try again.'));
throw new CouldNotSaveException(
__('The linked products data is invalid. Verify the data and try again.')
);
}

return true;
Expand Down
Loading