Create custom product link type in Magento 2: Part 1

How to add a custom product link type using Data Patch in Magento 2?

There are three types of product link types in Magento, Related, Upsell, and Cross-sell, which define product relation. You can refer to more about product link types here. In this blog, we get an idea of how we can create a custom product link type using a Data Patch.

In the following example, we are creating a custom product link type as parts in a custom module named Mage2_CustomProductLink

File Path: app/code/Mage2/CustomProductLink/Model/Product.php

<?php

declare(strict_types=1);

namespace Mage2\CustomProductLink\Model;

class Product
{
    public const PARTS_LINK_CODE = 'parts';
    public const PARTS_LINK_TYPE_ID = 6;
}

File Path: app/code/Mage2/CustomProductLink/Setup/Patch/Data/AddCustomProductLinkType.php

<?php

declare(strict_types=1);

namespace Mage2\CustomProductLink\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Mage2\CustomProductLink\Model\Product;

class AddCustomProductLinkType implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private ModuleDataSetupInterface $moduleDataSetup;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
    }

    /**
     * @return void
     */
    public function apply(): void
    {
        $setup = $this->moduleDataSetup;
        $data = [
            ['link_type_id' => Product::PARTS_LINK_TYPE_ID, 'code' => Product::PARTS_LINK_CODE]
        ];
        foreach ($data as $bind) {
            $setup->getConnection()
                ->insertForce($setup->getTable('catalog_product_link_type'), $bind);
        }
        $data = [
            [
                'link_type_id' => Product::PARTS_LINK_TYPE_ID,
                'product_link_attribute_code' => 'position',
                'data_type' => 'int',
            ]
        ];
        $setup->getConnection()->insertMultiple($setup->getTable('catalog_product_link_attribute'), $data);
    }

    /**
     * @return array
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * @return array
     */
    public function getAliases(): array
    {
        return [];
    }
}

Once creating above data patch and a model file, run the following command in the command line:

bin/magento setup:upgrade

The above code example would add a new link type id and link type code in the catalog_product_link_type table and a new link attribute for the same link type in the catalog_product_link_attribute table as shown in the following screenshots.

We hope this blog may be understandable and useful to you. You can email us at mage2developer@gmail.com if we missed anything or want to add any suggestions. We will respond to you as soon as possible. Happy to help 🙂

Need Further Help? or Questions?

Contact Us

Create your account

chatsimple