Skip to content

Commit

Permalink
Merge branch '2.4-develop-temporary-one-prs' of github.com:magento-en…
Browse files Browse the repository at this point in the history
…gcom/magento2ce into 2.4-develop
  • Loading branch information
okolesnyk committed Feb 27, 2020
2 parents f8da9f1 + dac263b commit 3fd50f2
Show file tree
Hide file tree
Showing 58 changed files with 1,303 additions and 576 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<annotations>
<stories value="Cardinal Commerce Settings"/>
<features value="CardinalCommerce"/>
<stories value="Configure CardinalCommerce"/>
<title value="CardinalCommerce settings hidden" />
<description value="CardinalCommerce config shouldn't be visible if the 3D secure is disabled for Authorize.Net."/>
<severity value="MINOR"/>
Expand Down
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

0 comments on commit 3fd50f2

Please sign in to comment.