Skip to content

Remove related products that not exist in map list. #28896

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

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 @@ -110,6 +110,10 @@ private function findRelations(array $products, array $loadAttributes, int $link
//Matching products with related products.
$relationsData = [];
foreach ($relations as $productId => $relatedIds) {
//Remove related products that not exist in map list.
$relatedIds = array_filter($relatedIds, function ($relatedId) use ($relatedProducts) {
return isset($relatedProducts[$relatedId]);
});
$relationsData[$productId] = array_map(
function ($id) use ($relatedProducts) {
return $relatedProducts[$id];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Get related products test
* Test coverage for get related products
*/
class GetRelatedProductsTest extends GraphQlAbstract
{
Expand Down Expand Up @@ -49,6 +49,40 @@ public function testQueryRelatedProducts()
self::assertRelatedProducts($relatedProducts);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/products_related_disabled.php
*/
public function testQueryDisableRelatedProduct()
{
$productSku = 'simple_with_cross';

$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
related_products
{
sku
name
url_key
created_at
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('products', $response);
self::assertArrayHasKey('items', $response['products']);
self::assertCount(1, $response['products']['items']);
self::assertArrayHasKey(0, $response['products']['items']);
self::assertArrayHasKey('related_products', $response['products']['items'][0]);
$relatedProducts = $response['products']['items'][0]['related_products'];
self::assertCount(0, $relatedProducts);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/products_crosssell.php
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var $product \Magento\Catalog\Model\Product */
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setName('Simple Related Product')
->setSku('simple')
->setPrice(10)
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)
->setWebsiteIds([1])
->setStockData(['qty' => 100, 'is_in_stock' => 1, 'manage_stock' => 1])
->save();

/** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
$productLink = $objectManager->create(\Magento\Catalog\Api\Data\ProductLinkInterface::class);
$productLink->setSku('simple_with_cross');
$productLink->setLinkedProductSku('simple');
$productLink->setPosition(1);
$productLink->setLinkType('related');

$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setName('Simple Product With Related Product')
->setSku('simple_with_cross')
->setPrice(10)
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setStockData(['qty' => 100, 'is_in_stock' => 1, 'manage_stock' => 1])
->setProductLinks([$productLink])
->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);

try {
$firstProduct = $productRepository->get('simple', false, null, true);
$productRepository->delete($firstProduct);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
//Product already removed
}

try {
$secondProduct = $productRepository->get('simple_with_cross', false, null, true);
$productRepository->delete($secondProduct);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
//Product already removed
}

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);