1

I'm trying to add custom text box named cashback in Magento 2 product add/edit page. I added the text box programmatically. But my below code doesn't add column in the table also not saving the value. I'm a beginner so I can't understand Magento 2 structure. I need clarification about which table does it stores and need to store data given in admin panel.

<?php

namespace Rk\Wallet\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(
          "cashback",
          [
          'group' => "",
          'label' => "Cashback",
          'is_html_allowed_on_front' => true,
          'default' => '1',
          'note' => '',
          'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
          'visible' => true,
          'required' => false,
          'user_defined' => true,
          'searchable' => true,
          'filterable' => true,
          'comparable' => false,
          'visible_on_front' => true,
          'visible_in_advanced_search' => false,
          'unique' => false,
          "frontend_class" => "cashbackclass",
          "used_in_product_listing" => true,
          "type"                    => "int",
          "input" => "text",
          "type" => "int",
          "source" => "Vendor\Module\Model\Config\Source\TimeSetup",
          'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
          ]
      );
    }
}
1

2 Answers 2

0

Magento 2 stores setup data/schema versions in setup_module table. I think that first what you have done was to run setup:upgrade after you added module and according version was set in table.

InstallData/Schema scripts are run only when there is no version stored in db, so at the first run of setup upgrade. If my assumption is correct then either remove relevant entry in setup_module table or put your script into UpgradeData and update module's version in etc/module.xml to higher.

Check other UpgradeData scripts to see how you can compare versions.

2
  • thanks @Bartosh but also after upgrading setup and module version it remains the same.
    – Ramesh KR
    Commented Nov 8, 2017 at 8:46
  • Thanks @Bartosh after removing module record the table value saved.
    – Ramesh KR
    Commented Nov 9, 2017 at 5:32
0

There are some errors in your script. please try this.

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY, 'cashback', [
            'group' => 'Product Details',
            'type' => 'text',
            'sort_order' => 102,
            'backend' => '',
            'frontend' => '',
            'label' => 'CashBack',
            'input' => 'textarea',
            'class' => '',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'searchable' => true,
            'filterable' => true,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
        ]);
    }

Note: Remove the entry of your module from setup_module table. Also check the attribute named cashback in eav_attribute table. If you find one, remove that as well.

12
  • thanks @Sachin Agarwal above code not added column in eav_attribute table. I need your help. I added new textbox named 'cashback' in adminpanel. Now I need to add the entered value in the text box to the new column in catalog_product_entity table. Pls help me.
    – Ramesh KR
    Commented Nov 8, 2017 at 8:45
  • @RameshKR You don't need to add a new column in the catalog_product_entity table. The eav_attribute table must a record for this cashback record. So catalog_product_entity_varchar table will store the value of this cashback attribute with the attribute_id which you ca find in the eav_attribute table. Commented Nov 8, 2017 at 9:40
  • also no column like cashback found in eav_attribute tabel
    – Ramesh KR
    Commented Nov 8, 2017 at 10:15
  • @RameshKR FYI: eav_attribute doesn't have a new column. it has new record (Row) for you attribute. Commented Nov 9, 2017 at 4:42
  • Thanks Sachin Agarwal. Cashback attribute code is updated in db. I have a text box in magento product edit page. I need to save the value given in the text box into the db. After entering data into text box and giving save it's not saving the value. For example if I give 10 in the text box in have to save. This is my logic. Pls help I searched lot of tutorials but I'm a beginner.
    – Ramesh KR
    Commented Nov 9, 2017 at 4:45

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.