0

I need to remove All the Related products for the particular product with Id. I referred and got the SQL query to do that. Can this achieve by Model & Resource Model? Any help is appreciated.

delete from catalog_product_link where product_id in (15) and link_type_id=1;

Answer found in Stack which using direct SQL query

3 Answers 3

1

After a struggle Managed to do it myself! Sharing the Answer might help others.

class Example
{
    protected $productRepository;

    public function __construct(ProductRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function removeRelatedLinks($sku)
    {

        $product = $this->productRepository->get($sku);
        $links = $product->getProductLinks();
        if (count($links)) {
            foreach ($links as $key => $link) {
                if ($link->getLinkType() === 'related') {
                    unset($links[$key]);
                }
            }
            $product->setProductLinks($links);
            $this->productRepository->save($product);
        }
    }
}
1
  • Thank you! Great information :) May 23, 2022 at 15:27
0

You can do is by below script code. You need to load product object by SKU or ID.

$obj = \Magento\Framework\App\ObjectManager::getInstance();

/** @var \Magento\Catalog\Model\Product $product */
$productObject = $obj->get('Magento\Catalog\Model\Product');
$product = $productObject->loadByAttribute('sku', 'Test Test');
$linkDataAll = [];

$existingRelatedProducts = $product->getProductLinks();

if($existingRelatedProducts) {
    $product->setProductLinks($linkDataAll);
    $product->save();
}
1
  • This will remove all the links to product, including Up sell & Cross Sells.
    – Sathya
    Jun 18, 2021 at 15:46
0

In my cases, I needed to remove all related and up-sell products for all products. So I've made a shell script:

Here is my class:

<?php

namespace Vendor\Product\Console\Command;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Exception;
class UnlinkProducts extends Command
{

    /**
     * @var CollectionFactory
     */
    private CollectionFactory $collectionFactory;

    /**
     * @var State
     */
    private State $state;

    public function __construct(
        State $state,
        CollectionFactory $collectionFactory
    ) {
        parent::__construct();
        $this->collectionFactory = $collectionFactory;
        $this->state = $state;
    }

    /**
     * @return void
     */
    protected function configure()
    {
        $this->setName('vendor:unlink-products');
        $this->setDescription('Unlink related, cross and up sells products');

        parent::configure();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return void
     * @throws LocalizedException
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->state->setAreaCode(Area::AREA_FRONTEND);

        $productCollection = $this->collectionFactory->create();
        // $productCollection->addAttributeToFilter('sku','my_sku');
        foreach ($productCollection as $product){
            $linkedProducts = $product->getProductLinks();
            if (count($linkedProducts) > 0){
                echo $product->getSku().PHP_EOL;
                foreach ($linkedProducts as $key => $linked){
                    if ((string) $linked->getLinkType() === 'related' || (string) $linked->getLinkType() === 'upsell'){
                        unset($linkedProducts[$key]);
                    }
                }

                $product->setProductLinks($linkedProducts);
                try{
                    $product->save($product);
                }catch(Exception $error){
                    echo $error->getMessage().PHP_EOL;
                    continue;
                }
            }
        }

        $output->writeln('Done!');
    }
}

Now if you run : bin/magento vendor:unlink-products, it should work. Don't forget to run bin/magento setup:di:compile before.

Good Luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.