Magento 2 – Configurable Product Attribute Sort Order in Dropdown

attributesdropdown-attributemagento2product

The order of the dropdown is not consistent with the order of the attribute itself.
Within the attribute size I have it ordered
Newborn
0-3 mo

and in the dropdown it shows up as
0-3
Newborn

the size Newborn was added a few days after the other sizes and the newborn simple products were imported after the 0-3 mo.
Any suggestions of how to get the order of the drop down to follow the order of the list within the attribute?
I have cleared cache and reindexed.
I read that @Khoa TruongDinh had the same issue but did not see a solution to the issue so am reposting.

Best Answer

I faced the same attribute options sorting issue at front-end, As I checked this issue and found that while fetching the attribute options, there is no sorting filter added in the query by default in Magento 2.1.2, So to fix this issue need to add the below code to add the ORDER By in function getAttributeOptions on line no 282 in file: vendor/magento/module-configurable-product/Model/ResourceModel/Product/Type/Configurable.php Now, it's working fine for me.

->joinInner(
        ['attribute_opt' => $this->getTable('eav_attribute_option')],
        'attribute_opt.option_id = entity_value.value',
        []
    )->order(
        'attribute_opt.sort_order ASC'
    );

If unable to edit the code then please replace this getAttributeOptions function with code as below:

public function getAttributeOptions($superAttribute, $productId)
{
    $scope  = $this->getScopeResolver()->getScope();
    $select = $this->getConnection()->select()->from(
        ['super_attribute' => $this->getTable('catalog_product_super_attribute')],
        [
            'sku' => 'entity.sku',
            'product_id' => 'product_entity.entity_id',
            'attribute_code' => 'attribute.attribute_code',
            'value_index' => 'entity_value.value',
            'option_title' => $this->getConnection()->getIfNullSql(
                'option_value.value',
                'default_option_value.value'
            ),
            'default_title' => 'default_option_value.value',
        ]
    )->joinInner(
        ['product_entity' => $this->getTable('catalog_product_entity')],
        "product_entity.{$this->getProductEntityLinkField()} = super_attribute.product_id",
        []
    )->joinInner(
        ['product_link' => $this->getTable('catalog_product_super_link')],
        'product_link.parent_id = super_attribute.product_id',
        []
    )->joinInner(
        ['attribute' => $this->getTable('eav_attribute')],
        'attribute.attribute_id = super_attribute.attribute_id',
        []
    )->joinInner(
        ['entity' => $this->getTable('catalog_product_entity')],
        'entity.entity_id = product_link.product_id',
        []
    )->joinInner(
        ['entity_value' => $superAttribute->getBackendTable()],
        implode(
            ' AND ',
            [
                'entity_value.attribute_id = super_attribute.attribute_id',
                'entity_value.store_id = 0',
                "entity_value.{$this->getProductEntityLinkField()} = "
                . "entity.{$this->getProductEntityLinkField()}",
            ]
        ),
        []
    )->joinLeft(
        ['option_value' => $this->getTable('eav_attribute_option_value')],
        implode(
            ' AND ',
            [
                'option_value.option_id = entity_value.value',
                'option_value.store_id = ' . $scope->getId(),
            ]
        ),
        []
    )->joinLeft(
        ['default_option_value' => $this->getTable('eav_attribute_option_value')],
        implode(
            ' AND ',
            [
                'default_option_value.option_id = entity_value.value',
                'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
        ),
        []
    )->where(
        'super_attribute.product_id = ?',
        $productId
    )->where(
        'attribute.attribute_id = ?',
        $superAttribute->getAttributeId()
    )->joinInner(
        ['attribute_opt' => $this->getTable('eav_attribute_option')],
        'attribute_opt.option_id = entity_value.value',
        []
    )->order(
        'attribute_opt.sort_order ASC'
    );

    return $this->getConnection()->fetchAll($select);
}
Related Topic