2

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.

  • Your setup script created new custom attribute successfully? – Khoa TruongDinh Feb 28 '17 at 16:28
  • Yes, I also see all my attributes in a product page in admin when select my set. – Mykhailo Shatilov Feb 28 '17 at 16:36
  • Did you assign the custom attribute to the current product? – Khoa TruongDinh Feb 28 '17 at 16:55
  • Yes, I think. You can see image above. – Mykhailo Shatilov Feb 28 '17 at 17:04
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, privacy policy and cookie policy

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