文档 下载安装 商店 博客 问答 主机

magento2 后台添加产品自定义字段,保存不到数据库中

开发
后端
magento2

#1

后台产品添加时自定义了两个字段,是可以显示的,但是在保存产品后,发现新增的字段没有保存
如下图:


我的product_form.xml 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
  <fieldset name="manage_note">
     <argument name="data" xsi:type="array">        
         <item name="config" xsi:type="array">
             <item name="label" xsi:type="string" translate="true">Group purchase</item>
             <item name="dataScope" xsi:type="string"/>
             <item name="sortOrder" xsi:type="number">0</item>
             <item name="collapsible" xsi:type="boolean">true</item>
             <item name="opened" xsi:type="boolean">true</item>
         </item>
     </argument>
     <field name="Collage_number">
         <argument name="data" xsi:type="array">
            
             <item name="config" xsi:type="array">
                 <item name="label" xsi:type="string" translate="true">Number of group buying</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="componentType" xsi:type="string">field</item>
                 <item name="component" xsi:type="string">Magento_CatalogInventory/js/components/qty-validator-changer</item>
                 <item name="visible" xsi:type="string">1</item>
                 <item name="require" xsi:type="string">0</item>
                 <item name="additionalClasses" xsi:type="string">admin__field-small</item>
                 <item name="breakLine" xsi:type="boolean">false</item>
                 <item name="dataType" xsi:type="string">number</item>
                 <item name="dataScope" xsi:type="string">qty</item>
                 <item name="source" xsi:type="string">product_details</item>
                 <item name="sortOrder" xsi:type="number">10</item>
                 
                 <!-- <item name="scopeLabel" xsi:type="string">[GLOBAL]</item> -->
             </item>
         </argument>
        
     </field>    
     <field name="Collage_price">
         <argument name="data" xsi:type="array">
          
             <item name="config" xsi:type="array">
                 <item name="DataType" xsi:type="string">price</item>
                 <item name="label" xsi:type="string" translate="true">Group purchase price</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="visible" xsi:type="string" >1</item>
                 <item name="required" xsi:type="string" >1</item>
                 
                 <item name="source" xsi:type="string" >product-details</item>
                 <item name="code" xsi:type="string" >Collage_price</item>
                 <item name="globalScope" xsi:type="boolean" >true</item>
                 <item name="componentType" xsi:type="string" >field</item>
                 <item name="addbefore" xsi:type="string" >CN¥</item>
                 <item name="additionalClasses" xsi:type="string" >admin_field-small</item>
                 <item name="component" xsi:type="string" >Magento_ConfigurableProduct/js/components/price-configurable</item>
                 <item name="sortOrder" xsi:type="number">30</item>
                 <!-- <item name="dataScope" xsi:type="string">Collage_price</item> -->
                 <!-- <item name="scopeLabel" xsi:type="string">[GLOBAL]</item> -->
             </item>
            
         </argument>
        
     </field>       
 </fieldset>

请问我该如何解决呢?


#2

这只是界面上增加了,你还需要了解一下Magento的EVA
https://www.mageplaza.com/how-create-eav-attribute-magento-2.html

如果感兴趣,请麻烦翻译后发到本站上,谢谢.


#3

创建自定义字段应该是
xxx/xxx/Setup/InstallData 中

namespace xxx\xxx\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,
      "collage_number",
      [
      'group' => "Group purchase",
      'label' => "Number of group buying",
      'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
      'visible' => true,
      'visible_on_front' => true,
      "used_in_product_listing" => true,
      "input" => "input",
      "type" => "int",
      ]
  );
    $eavSetup->addAttribute(
      \Magento\Catalog\Model\Product::ENTITY,
      "collage_price",
      [
      'group' => "Group purchase",
      'label' => "Group purchase price",
      'is_html_allowed_on_front' => true,
      'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
      'visible' => true,
      'required' => true,
      'visible_on_front' => true,
      "used_in_product_listing" => true,
      "input" => "input",
      "type" => "decimal",
      ]
  );
}

}

然后在运行 php bin/magento setup:upgrade 命令,就可以在后台看到新加的两个字段了,这时候就可以保存到数据库了,但是样式是没有的,采用的是默认样式,至于怎么更改样式我还没搞清楚