Magento2 Product Attribute Value Not Showing in Admin Form – Troubleshooting Guide

eav-attributesmagento2productproduct-attribute

I have created a custom 'Forme' product attribute select type based on option value

class FormeOptions extends AbstractSource
{
    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        if (null === $this->_options) {
            $this->_options=[
                ['label' => __('-- Non renseigné --'), 'value' => 0],
                ['label' => __('Coeur 65 à 75g'), 'value' => 1],
                ['label' => __('Grand Galet 75 à 79g'), 'value' => 2],
                ['label' => __('Galet 50g environ'), 'value' => 3],
                ['label' => __('Boule 45 à 55g environ'), 'value' => 4],
                ['label' => __('Boule de 100g environ'), 'value' => 5],
                ['label' => __('Boule de 100g environ'), 'value' => 6],
            ];
        }
        return $this->_options;
    }
}

Created like below

           $eavSetup->addAttribute(
                Product::ENTITY,
                $attributeCode,
                [
                    'type' => 'varchar',
                    'label' => 'Forme',
                    'input' => 'select',
                    'required' => false,
                    'source' => 'Cpy\Import\Model\Config\FormeOptions',
                    'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'used_in_product_listing' => true,
                    'visible_on_front' => true,
                    'is_used_in_grid' => true,
                    'sort_order' => 52,
                ]
            );

In my catalog_product_entity_varchar

The attribute value is properly registered (stored during a custom import)

value_id,attribute_id,store_id,entity_id,value
108417,173,0,5487,Coeur 65 à 75g

But on the product form admin page…it's still shows the field first value and if i go to see in the listing grid the value is empty.

grid

listing

Any idea on what I am missing there ?

EDIT : If i'm submitting from the product admin page, the value is saved and shown properly.

The problem seems to occured only when I try to do this on my import :

        try{
            $productChild = $this->productRepository->getById($childProductId);
            switch ($attribute['name']){
                case 'forme':
                    $value = $attribute['value'];
                    $productChild->setForme($value);
                    var_dump("Written forme :".$productChild->getForme());
                    break;
                case 'poids':
                    $value = $attribute['value'];
                    $productChild->setPoids($value);
                    var_dump("Written Poids :".$productChild->getPoids());
                    break;
                default :
                    break;
            }
            $this->productRepository->save($productChild);

Best Answer

@claims as I understood you have not saved value for all the products, so whenever you edit any product it will show the first value as '-- Non renseigné --' . And in the grid it will display blank values.

Forex: If you have saved B product value as 'Galet 50g environ' from the import/catalog edit page then it will show 'Galet 50g environ' value for B product in the grid.

The solution is I would suggest set the default value '-- Non renseigné --' for all the products by catalog import and it will display the same for all the products in the grid if you don't want to display the blank value.

Please let me know if this will not help.

Related Topic