2

I want to add Custom Field at Product Edit Page(Admin Side) like related/upsell/crosssell product i want to add Custom Products for it, found much similar solution but it's not working at all,If you know how to, Please help me out..

<?php


namespace CustomField\Ui\DataProvider;


use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Ui\Component\Form\Fieldset;
use Magento\Ui\Component\Form\Field;



class Fields extends AbstractModifier
{

/**
 * @param array $data
 * @return array
 * @since 100.1.0
 */
public function modifyData(array $data)
{
    return $data;
}

/**
 * @param array $meta
 * @return array
 * @since 100.1.0
 */
public function modifyMeta(array $meta)
{
    $meta = array_replace_recursive(
        $meta,
        [
            'yash' => [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'label' => __('Add to Custom Products'),
                            'collapsible' => true,
                            'componentType' => Fieldset::NAME,
                            'dataScope' => 'data.ktpl',
                            'sortOrder' => 10
                        ],
                    ],
                ],

            ],
        ]
    );


    return $meta;
}
protected function getButtonSet()
{
    return [
        'arguments' => [
            'data' => [
                'config' => [
                    'formElement' => 'container',
                    'componentType' => 'container',
                    'label' => false,
                    'content' => 'This ',
                    'template' => 'ui/form/components/complex',
                ],
            ],
        ]
    ];
}

}

Thanks

2

Step 1: Create file InstallData.php

We will start with the InstallData class which located in app/code/Mageplaza/HelloWorld/Setup/InstallData.php. The content for this file:

<?php
namespace Mageplaza\HelloWorld\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

}

Step 2: Define the install() method

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

}

Step 3: Create custom attribute

<?php
namespace Mageplaza\HelloWorld\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sample_attribute',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Sample Atrribute',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => true,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

Run the upgrade script php bin/magento setup:upgrade to install the module and the product attribute sample_attribute will be created. After run upgrade complete, please run php bin/magento setup:static-content:deploy -f and go to product from admin to check the result. it will show like this:

enter image description here

Reference link : Magento 2 Add Product Attribute Programmatically

|improve this answer|||||
  • Not attribute want to add Custom FIeld like ADding Related Product!! – Yash Panchal Dec 16 '19 at 6:07

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.