0

How to create column called link_title to the catalog_product_link table & what are the steps, need to be followed to set the value to the respected column

enter image description here

Did small prototype as followed here but unable to save data.

<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$sourceMaterial='24-MB01';
$productLink = $objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface')
    ->setSku($sourceMaterial)
    ->setLinkedProductSku('24-MB06')
    ->setLinkTitle("helloworld")
    ->setLinkType('required');
$linkData[] = $productLink;
$product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($sourceMaterial);
if($product) {
    $product->setProductLinks($linkData)->save();
}
?>

The recorded data is saving without link_title value not saving.

2

For create link_title column

Create InstallSchema.php :

app/code/RH/Helloworld/Setup/InstallSchema.php

<?php
namespace RH\Helloworld\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface {

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) {
        $installer = $setup;

        $installer->startSetup();

        $eavTable = $installer->getTable('catalog_product_link');

        $columns = [
            'link_title' => [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true,
                'comment' => 'Link Title',
            ],

        ];

        $connection = $installer->getConnection();
        foreach ($columns as $name => $definition) {
            $connection->addColumn($eavTable, $name, $definition);
        }

        $installer->endSetup();
    }
}

For save record in custom column :

Inject this below code in your construct :

    protected $productFactory;

    public function __construct(
        ...
        \Magento\Catalog\Model\ProductFactory $productFactory
        ...
    ) {
        ....
        $this->productFactory = $productFactory;
        ....
    }

And then, use this below code in your function :

$product = $this->productFactory->create()->load(45); //Set your product id here
$link = $product->getLinkInstance()->load(1); //Set your link id here
$link->setLinkTitle('test'); //Set your link title value here
$link->save();

Don't forget to execute this below command :

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c

Hope, It will helpful for you.


Output : enter image description here

| improve this answer | |
  • 1
    Thanks for your inputs @rohan let me check and get back to you. – Nagaraju K Jan 3 at 11:04
  • Hi, @rohab Here we would like to use set title concept by the custom import, In this scenario how do we get link_id can u advise? – Nagaraju K Jan 3 at 11:24
  • You can get link id based on product id and then, you need to set based on your requirement. – Rohan Hapani Jan 3 at 11:28
  • Can u advise me based on this concept codeshare.io/G7jEwr here how do i set? – Nagaraju K Jan 3 at 11:55
  • Why you run script from root path. You can also create controller and set code which I posted. It's working. I tested in my instance. – Rohan Hapani Jan 3 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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