0

I want to populate attribute(Gender) values in select dropdown on listing page. On selecting specific value of the gender attribute, the product list should be filtered.

The gender attribute has 3 values

  1. Male
  2. Female
  3. Others

For some products the attribute value is set to male and female as the product is unisex.

I want to know how can I get the values of attributes(male, female, others) irrespective of products.

2
  • Do you want to get the product attribute value on the category page?
    – Sumit
    Oct 9, 2019 at 7:17
  • Yes, All values in select dropdown on category page.
    – Sejal Shah
    Oct 9, 2019 at 7:23

2 Answers 2

0

You can get the select attribute value for the product with the below code.

$_product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getValue($_product);

Hope this helps!!!

0

In block file, Use the following code...

protected $eavConfig;

public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
)
{
    ...
     $this->eavConfig = $eavConfig;
    ...
}

public function getGenderOptions()
{
    $attribute = $this->eavConfig->getAttribute('catalog_product', 'gender'[your attribute code]);
    $options = $attribute->getSource()->getAllOptions();
    $genderOptions = array();
    foreach ($options as $key => $option) 
    {
        if($option['label'])
        {         
            array_push($gender, $option['label']);
        }
    }
}

In the phtml file, Use the following code...

$genderOptions = $this->getGenderOptions();    
  foreach ($genderOptions as $option)     
  {        
          print_r($option);    
  }

You may see all the options on the category page,After that, you may use it as your wish...

Thanks.

Your Answer

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

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