Danh sách thuộc tính tùy chỉnh Magento2.1


10

Các bước để tái sản xuất

1. Tập lệnh Nâng cấpData.php chứa:

$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. xem / 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>

Kết quả mong đợi

  1. Ở dạng danh mục sẽ xuất hiện thả xuống, chọn Roflawaii với Khối CMS làm tùy chọn

Kết quả thực tế

  1. Thả xuống

Câu trả lời:


14

Thêm thẻ tùy chọn để tạo tùy chọn chọn. Trong trường hợp của bạn, điều này nên


<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>


Có lẽ bạn biết nếu tôi có thể hiển thị / ẩn tab này và / hoặc các thuộc tính của nó dựa trên một số điều kiện, ví dụ như độ sâu của danh mục?
Sergejs Zakatovs

CẢM ƠN! Tôi đã tìm kiếm điều này rất lâu. Tài liệu rất không rõ ràng về chủ đề này. Làm thế nào bạn biết điều này?
CompactCode

Dữ liệu không được lưu trong cơ sở dữ liệu @Sohel Rana
Chirag Parmar

2

Tôi đã làm trong trường hợp của tôi. Tôi có tùy chọn tùy chỉnh cũ. L1, L2 và L3. Tôi cần lấy chúng trên thuộc tính tùy chỉnh làm giá trị. Vì vậy, tôi đã được tạo một tệp nguồn trong mô-đun - nhà cung cấp \ mô-đun \ Mô hình \ Cấu hình \ Nguồn \ Tùy chọn.php

tệp này chứa mã nhỏ để tạo các tùy chọn, Tại đây bạn có thể làm theo mã

 <?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'),
                ];
        }

    }

sau đó trong installdata.php của bạn, bạn phải gọi nó là nguồn

$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,
             ]
        );

Sau đó, thêm dòng trong tệp 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>

Lưu nó, xóa bộ nhớ cache và vui lòng kiểm tra.

Hy vọng nó sẽ giúp bạn.

Xin vui lòng cho tôi trả lời nếu nó làm việc với bạn.


Tôi đã gặp loại lỗi này: Phần tử 'trường': Phần tử này không được mong đợi. Dự kiến ​​là một trong (cài đặt, cột, hành độngColumn, lựa chọnColumn). Đường dây: 681
Pratik Mehta

Làm thế nào bạn lưu dữ liệu,
Mujahidh

Dữ liệu không được lưu trong cơ sở dữ liệu @Jdprasad V
Chirag Parmar

Điều này làm việc cho tôi, vui lòng kiểm tra lại, nếu bạn đã thực hiện bất kỳ thay đổi nào trên trang lược đồ.
Jdprasad V

1
+1 cho điều này. Nó làm việc cho tôi. ] bị thiếu trong mảng. Tôi chỉnh sửa nó.
Chirag Parmar
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.