Magento – Get product custom attribute data Magento 2

attributescustom-attributesmagento-2.1magento2product-attribute

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.

Best Answer

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());