2

I need to get all existing values for a custom product attribute.

Setup/UpgradeData.php

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'manufacturer_name',
    [
        'type' => 'varchar',
        'label' => 'Manufacturer Name',
        'input' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => true,
        'used_for_sort_by' => false,
        'group' => 'Roche',
        'sort_order' => 10
    ]
);

Assuming that the following data is present in the database

| product_id | manufacturer_name |
|------------+-------------------|
| 1          | Manufacturer 1    |
| 2          | Manufacturer 1    |
| 3          | NULL              |
| 4          | Manufacturer 2    |

I want to retrieve the following list

  Manufacturer 1
  Manufacturer 2

I've try the following code but it doesn't work, it always return a empty array.

Block/Manufacturers.php

/**
 * Get all manufacturers list
 * @return array
 */
public function getManufacturerListing()
{
    $attributes = $this->eavAttributeRepository->get(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,'manufacturer_name');
    $options = $attributes->getSource()->getAllOptions(true,true);
    return $options;
}

Do I need to create a SQL request with a join or something like that ? Please help. Thanks

5

Try this

protected $_eavAttribute;

public function __construct(
        \Magento\Eav\Api\AttributeRepositoryInterface $eavAttribute
) {
    $this->_eavAttribute = $eavAttribute;
}

public function getManufacturerListing()
{ 
    $manufacturerAttr = $this->_eavAttribute->get(\Magento\Catalog\Model\Product::ENTITY, 'manufacturer_name');
    $allOptions = $manufacturerAttr->getSource()->getAllOptions();
    return $allOptions;
}
2

See the Below Code and try :

1. Inside your block file,

  protected $eavAttributeRepository;

  public function __construct(
      \Magento\Catalog\Block\Product\Context $context,
      \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
  ){
      parent::__construct($context);
      $this->eavAttributeRepository = $eavAttributeRepository;
   }

  public function getManufacturerListing(){
      $attributes = $this->eavAttributeRepository->get(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,'manufacturer_name');
      $options = $attributes->getSource()->getAllOptions(false);
      return $options;
  }

2. Inside your phtml file,

 $optionList = $block->getManufacturerListing();
 foreach ($optionList as $value) {
     //print_r($value);
     echo $value['label'];
 }
0

I finally found it. The following code return exactly what I want :

Block/Manufacturers.php

/**
 * Get all manufacturers list
 * @return array
 */
public function getManufacturerListing()
{
    $productCollection = $this->manager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $productCollection->create();
    $collection->addAttributeToSelect('manufacturer_name')
        ->addAttributeToFilter('manufacturer_name', array('notnull' => true))// get only not null values
        ->getSelect()->group('manufacturer_name');// only get once each same value
    return $collection;
}

I just select the needed attribute and filter not null values (as a kind of array_filter)

view/templates/manufacturers.phtml

<select name="manufacturer">
    <?php foreach ($block->getManufacturerListing() as $product): ?>
        <option value="<?php echo $product->getManufacturerName() ?>"> <?php echo __($product->getManufacturerName()); ?> </option>
    <?php endforeach; ?>
</select>

I retrieve a ProductInterceptor objects collection in my template so I just call the "magic" method getManufacturerName() based on my custom product attribute name to retrieve the value of attribute.

It works perfectly!

1
  • I did exactly the same thing but got the error: Invalid attribute name: manufacturer_name. I've used addAttributeToSelect('*') also but my collection doesn't load all the attributes of products. – Umar Oct 5 '18 at 15:37

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.