3

I created my product set, group and attributes inside.

$entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);

$eavSetup->addAttribute(
    $entityTypeId,
    'si_qty',
    [
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Si Qty',
        'input' => 'text',
        'class' => '',
        'source' => '',
        'global' => 0,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => null,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => false,
        'unique' => false,
        'apply_to' => 'mySpecialProductType',
        'system' => 1,
        'group' => 'My Group Name',
        'option' => ['values' => []]
    ]
);

Currently I try to get the attribute value from catalog products page and product page.

// \Magento\Catalog\Model\Product $product

var_dump($product->getData('si_qty')); // => NULL
var_dump($product->getResource()->getAttribute('si_qty')->getFrontend()->getValue($product)); // => NULL
var_dump($product->getCustomAttribute('si_qty')); // => NULL
var_dump($product->getCustomAttributes()); // => ...

Array
(
    [0] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => image
                    [value] => /b/2/b2214-a.jpg
                )

        )

    [1] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => small_image
                    [value] => /b/2/b2214-a.jpg
                )

        )

    [2] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => minimal_price
                    [value] => 64.5000
                )

        )

    [3] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => required_options
                    [value] => 0
                )

        )

    [4] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => has_options
                    [value] => 0
                )

        )

    [5] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => url_key
                    [value] => fine-gauge-flare-sweater-dress-grey
                )

        )

)

Attribute congiguration (continue play with it): attribute configuration

Attribute in product page in admin: attribute in product page in admin

How I can get my custom product attribute?

P.S. Magento 2.1.5.

4
  • Your setup script created new custom attribute successfully? Commented Feb 28, 2017 at 16:28
  • Yes, I also see all my attributes in a product page in admin when select my set. Commented Feb 28, 2017 at 16:36
  • Did you assign the custom attribute to the current product? Commented Feb 28, 2017 at 16:55
  • Yes, I think. You can see image above. Commented Feb 28, 2017 at 17:04

1 Answer 1

1

I have decided my problem.

In code (not in admin panel, I don't know why) change:

[
    ...

    'visible_on_front' => true,

    ...
]

You can also turn on attributes to show in a product page:

[
    ...

    'used_in_product_listing' => true,

    ...
]

UPD: Sometimes we need load a full product data to model. Try

$newProduct = $product->load($product->getId());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.