Magetno 2:自定义属性不起作用

时间:2017-12-26 08:07:08

标签: magento magento2

我已经以编程方式创建了自定义属性。它没有在管理面板的产品部分显示。我在创建InstallData.php&之后使用了以下命令。 Options.php文件:

php bin / magento setup:upgrade php bin / magento cache:clean

之后,我无法在管理员的产品部分找到自定义属性。

代码是

public static void main(String[] args) {
    String v = "Iam8 you64 asjkj125 asdjkj333 heis216s";
    int sum = 0, max = 0, start = 0;
    StringBuilder maxWord = new StringBuilder();
    for (int i = 0; i < v.length(); ++i) {
        char ch = v.charAt(i);
        if (Character.isWhitespace(ch)) {
            max = updateMax(v, maxWord, max, sum, start, i);
            start = i + 1;
            sum = 0;
        } else if (Character.isDigit(ch)) {
            sum += ch - 48;
        }
    }
    if (sum > 0) {
        updateMax(v, maxWord, max, sum, start, v.length());
    }
    System.out.println(maxWord.toString());
}

private static int updateMax(String v, StringBuilder maxWord, int max, int sum, int start, int i) {
    if (sum >= max && isPowerOfThree(sum)) {
        maxWord.replace(0, maxWord.length(), v.substring(start, i));
        return sum;
    }
    return max;
}

private static boolean isPowerOfThree(int sum) {
    // see any from https://stackoverflow.com/questions/1804311/how-to-check-if-an-integer-is-a-power-of-3
    while (sum % 3 == 0) {
        sum /= 3;
    }
    return sum == 1;
}

1 个答案:

答案 0 :(得分:0)

您也可以使用它,因为我曾经以编程方式创建自定义产品属性。<​​/ p>

  

应用程序/代码/ [供应商] / [模块] /Setup/InstallData.php

namespace [Vendor]\[Module]\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Quote\Setup\QuoteSetupFactory;

class InstallData implements InstallDataInterface
{

private $eavSetupFactory;
private $quoteSetupFactory;
private $salesSetupFactory;

/**
 * InstallData constructor.
 * @param EavSetupFactory $eavSetupFactory
 * @param QuoteSetupFactory $quoteSetupFactory
 */
public function __construct(
    EavSetupFactory $eavSetupFactory,
    QuoteSetupFactory $quoteSetupFactory,
    SalesSetupFactory $salesSetupFactory
)
{
    $this->eavSetupFactory = $eavSetupFactory;
    $this->quoteSetupFactory = $quoteSetupFactory;
    $this->salesSetupFactory = $salesSetupFactory;
}

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

    /**
     * Add attributes to the eav/attribute
     */
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'dropdown_attribute',
        [
            'type'                    => 'int',
            'label'                   => 'Dropdown Attribute',
            'input'                   => 'select',
            'global'                  => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible'                 => true,
            'required'                => false,
            'user_defined'            => true,
            'default'                 => '',
            'searchable'              => false,
            'filterable'              => false,
            'comparable'              => false,
            'visible_on_front'        => false,
            'used_in_product_listing' => false,
            'unique'                  => false,
            'option'                  => [
                'values' => [
                    'Option 1',
                    'Option 2',
                    'Option 3'
                ],
            ]
        ]
    );

    $attributeSetId = $eavSetup->getDefaultAttributeSetId('catalog_product');
    $eavSetup->addAttributeToSet(
        'catalog_product',
        $attributeSetId,
        'General',
        'dropdown_attribute'
    );

    $attributeOptions = [
        'type'     => Table::TYPE_TEXT,
        'visible'  => true,
        'required' => false
    ];
}
}