【问题标题】:Magento2: add custom dropdown to product edit formMagento2:将自定义下拉列表添加到产品编辑表单
【发布时间】:2016-12-21 10:29:01
【问题描述】:

我想在 magento2 的产品编辑表单中添加一个新的下拉列表。数据应该来自我在自定义模块中创建的自定义表。我该怎么做,那里有很好的例子或教程吗?

我尝试创建一个观察者,如下所示:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="adminhtml_catalog_product_attribute_edit_prepare_form">
    <observer name="custom_product_fields" instance="Vendorname\Custom\Observer\CatalogProductEditPrepareForm"/>
    <!-- CatalogProductEditPrepareForm is name of class in which we'll add custom fields in form-->
  </event>
</config>

观察者看起来像这样:

class CatalogProductEditPrepareForm implements ObserverInterface
{
  protected $_coreRegistry;

  public function __construct(\Magento\Framework\Registry  $coreRegistry) {
    $this->_coreRegistry = $coreRegistry;
  }

  public function execute(\Magento\Framework\Event\Observer $observer){
    echo "why is this not called?!";
  }
  ...
}

谢谢!

【问题讨论】:

  • 您需要创建一个下拉类型的新产品属性,并使用自定义模型指定。
  • 如何使用自定义模型指定属性?

标签: php magento2


【解决方案1】:

您已在自定义扩展名中使用 InstallData.php 文件创建属性, 代码在这里。

<?php
namespace Vendor\Extension\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;
use Magento\Framework\Module\Setup\Migration;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;


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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        // Product Attribute
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'attribute_id',
            [
                'type' => 'varchar',
                'label' => 'Attribute Label',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'input' => 'select',
                'source' => 'Vendor\Extension\Model\Source\Mysource',
                'required' => false,
                'sort_order' => 6,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'searchable'        => false,
                'filterable'        => false
            ]
        );
        $setup->endSetup();
    }
}

以上代码创建产品属性,这里需要创建自定义源文件,

Vendor\Extension\Model\Source\Mysource.php

<?php
namespace Vendor\Extension\Model\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Mysource extends AbstractSource
{
     public function getOptionArray()
    {
        return [
            ['value' => 1, 'label'=>__('Label-1')],
            ['value' => 2, 'label'=>__('Label-2')],
        ];
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多