Magento2.1 หมวดหมู่แอตทริบิวต์ที่กำหนดเองแบบเลื่อนลง


10

ขั้นตอนในการทำซ้ำ

1. สคริปต์โมดูล UpgradeData.php ประกอบด้วย:

$categorySetup->addAttribute(Category::ENTITY, 'roflcopter', [
                    'type' => 'int',
                    'label' => 'CMS Block',
                    'input' => 'select',
                    'source' => 'Magento\Catalog\Model\Category\Attribute\Source\Page',
                    'required' => false,
                    'sort_order' => 20,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'group' => 'Display Settings',
            ]);

2. ดู / adminhtml / ui_component / category_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="Navigation">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Navigation</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">100</item>
            </item>
        </argument>
        <field name="roflcopter">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">60</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Roflcopter</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

ผลลัพธ์ที่คาดหวัง

  1. ในรูปแบบหมวดหมู่ควรปรากฏดรอปดาวน์เลือก Roflcopter ด้วย CMS Blocks เป็นตัวเลือก

ผลลัพธ์ที่แท้จริง

  1. ดร็อปดาวน์ว่างเปล่า

คำตอบ:


14

เพิ่มแท็กตัวเลือกสำหรับการสร้างตัวเลือกที่เลือก ในกรณีของคุณควรจะเป็น


<field name="roflcopter">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Catalog\Model\Category\Attribute\Source\Page</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">70</item>
            <item name="dataType" xsi:type="string">string</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string" translate="true">Roflcopter</item>
        </item>
    </argument>
</field>


คุณอาจทราบหรือไม่ว่าฉันสามารถแสดง / ซ่อนแท็บนี้และ / หรือคุณลักษณะของมันตามเงื่อนไขบางอย่างเช่นความลึกของหมวดหมู่
Sergejs Zakatovs

ขอขอบคุณ! ฉันค้นหาสิ่งนี้มานาน เอกสารไม่ชัดเจนในหัวข้อนี้ คุณรู้เรื่องนี้ได้อย่างไร?
CompactCode

ไม่มีการบันทึกข้อมูลในฐานข้อมูล @Sohel Rana
Chirag Parmar

2

ฉันได้ทำในกรณีของฉัน ฉันมีตัวเลือกที่กำหนดเองเช่น L1, L2 และ L3 ฉันต้องการรับค่าแอตทริบิวต์ที่กำหนดเองเป็นค่า ดังนั้นฉันจึงสร้างไฟล์ต้นฉบับในโมดูล - ผู้ขาย \ module \ Model \ Config \ Source \ Options.php

ไฟล์นี้มีรหัสขนาดเล็กเพื่อสร้างตัวเลือกที่นี่คุณสามารถติดตามรหัส

 <?php
    /**
     * Copyright © 2013-2017 Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Vendor\module\Model\Config\Source;
    /**
     * Catalog category landing page attribute source
     *
     * @author      Magento Core Team <core@magentocommerce.com>
     */
    class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
    {
        /**
         * {@inheritdoc}
         * @codeCoverageIgnore
         */
        public function getAllOptions()
        {
            if (!$this->_options) {
                $this->_options = [
                    ['value' => 'l1', 'label' => __('L1')],
                    ['value' => 'l2', 'label' => __('L2')],
                    ['value' => 'l3', 'label' => __('L3')],
                ];
            }
            return $this->_options;
        }
          /**
         * Get options in "key-value" format
         *
         * @return array
         */
        public function toArray()
        {
            return [
                'l1' => __('L1'),
                'l2' => __('L2'),
                'L3' => __('L3'),
                ];
        }

    }

หลังจากนั้นใน installdata.php ของคุณคุณต้องเรียกสิ่งนี้ว่าเป็นแหล่งที่มา

$eavSetup->addAttribute(
            Category::ENTITY,
            'category_level_rendering',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Category Level rendering',
                'input' => 'select',
                'required' => false,
                'sort_order' => 100,
                'source' => '',
                'visible'  => true,
                'source' => 'vendor\module\Model\Config\Source\Options',
                'default'  => '0',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
                'used_in_product_listing' => true,
             ]
        );

จากนั้นเพิ่มบรรทัดในไฟล์ xml

<field name="category_level_rendering">
                <argument name="data" xsi:type="array">
/*Here is the code added to get the options on dropdown*/
<item name="options" xsi:type="object">Vendor\module\Model\Config\Source\Options</item>
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">10</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">select</item>
                        <item name="label" xsi:type="string" translate="true">Category Level Rendering</item>
                    </item>
                </argument>
            </field>

บันทึกล้างแคชและโปรดตรวจสอบ

หวังว่ามันจะช่วยคุณได้

โปรดให้ฉันตอบถ้ามันเหมาะกับคุณ


ฉันได้รับข้อผิดพลาดประเภทนี้: องค์ประกอบ 'ฟิลด์': ไม่คาดหวังองค์ประกอบนี้ คาดว่าเป็นหนึ่งใน (การตั้งค่าคอลัมน์ actionsColumn, selectionsColumn) Line: 681
Pratik Mehta

คุณบันทึกข้อมูลได้อย่างไร
Mujahidh

ไม่มีการบันทึกข้อมูลในฐานข้อมูล @Jdprasad V
Chirag Parmar

สิ่งนี้ใช้ได้สำหรับฉันโปรดตรวจสอบอีกครั้งหากคุณทำการเปลี่ยนแปลงใด ๆ ในหน้าสคีมา
Jdprasad V

1
+1 สำหรับสิ่งนี้ มันใช้งานได้สำหรับฉัน ] หายไปในอาเรย์ ฉันแก้ไขมัน
Chirag Parmar
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.