6

I need to get the label and sort order (as defined in backend) of all the options of a custom attribute, programmatically.

I'm using this interface: Magento\Eav\Api\AttributeRepositoryInterface

This is what I tried already:

// Access to the attribute interface
$attribute = $this->_attributeRepository->get(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code');

// Get an array of options
$options = $attribute->getOptions();

// Get the label and sort order of each option
foreach ($options as $option) {
    var_dump($option->getLabel()); // Works OK
    var_dump($option->getSortOrder()); // Always returns NULL
}

I have double checked and there exists a sort order, both in database and in admin area, but the getSortOrder method always returns NULL.

Any ideas?

3
//Use the following code.

function getExistingOptions( $object_Manager ) {

$eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$options = $attribute->getSource()->getAllOptions();

$optionsExists = array();

foreach($options as $option) {
    $optionsValues[] = $option['value'];
    $optionsExists[] = $option['label'];
}

return $optionsExists;

}

Click here for detailed explanation. http://www.pearlbells.co.uk/code-snippets/get-magento-attribute-options-programmatically/

  • How to get the options in ascending order – Jaisa Dec 6 at 9:11
1

Use following code for getting attribute options and sort order.

$eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'attribute_code');

$options = $attribute->getSource()->getAllOptions(); // for attribute option.

$attribute->getPosition(); // for attribute sort order or option.
0

By Default its giving you the result sorted so if you want store the sortorderid in that array then you just store the index value of that loop.

0

Long answer:

<?php

namespace Namespace/Module/Model;

class Options
{
    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
     */
    private $attrOptionCollectionFactory;

    /**
     * Options constructor.
     */
    public function __construct(
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
    )
    {
        $this->attrOptionCollectionFactory = $attrOptionCollectionFactory;
    }

    /**
     * @param \Magento\Eav\Model\Attribute $attribute
     */
    public function fullAllOptions($attribute)
    {
        $collection = $this->attrOptionCollectionFactory->create()
            ->setAttributeFilter($attribute->getAttributeId())
            ->setStoreFilter($attribute->getStoreId())
            ->load();

        /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */
        foreach($collection as $option) {
            $label = $option->getLabel();
            $value = $option->getValue();
            $sort = $option->getSortOrder();
        }
    }
}

Short answer:

/**
 * @param \Magento\Eav\Model\Attribute $attribute
 */
public function fullAllOptions($attribute)
{
    $sort = 0;
    foreach ($attribute->getOptions() as $option) {
        $label = $option->getLabel();
        $value = $option->getValue();
        $sort = $sort++;    // because this collection already sorted by sortOrder field
    }
}

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.