1

I've made changes to my Magento code to remove the "Choose an Option" in the drop down for configurable products. Now, I want to ensure that the drop down menu options are loaded in alphabetical order except for the first option. I want the current first value to remain the same. The first associated sku should be the default, pre-selected options on the page.

app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php

protected function _loadPrices()
{
    if ($this->count()) {
        $pricings = array(
            0 => array()
        );

        if ($this->getHelper()->isPriceGlobal()) {
            $websiteId = 0;
        } else {
            $websiteId = (int)Mage::app()->getStore($this->getStoreId())->getWebsiteId();
            $pricing[$websiteId] = array();
        }

        $select = $this->getConnection()->select()
            ->from(array('price' => $this->_priceTable))
            ->where('price.product_super_attribute_id IN (?)', array_keys($this->_items));

        if ($websiteId > 0) {
            $select->where('price.website_id IN(?)', array(0, $websiteId));
        } else {
            $select->where('price.website_id = ?', 0);
        }

        $query = $this->getConnection()->query($select);

        while ($row = $query->fetch()) {
            $pricings[(int)$row['website_id']][] = $row;
        }

        $values = array();

        foreach ($this->_items as $item) {
            $productAttribute = $item->getProductAttribute();
            if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) {
                continue;
            }
            $options = $productAttribute->getFrontend()->getSelectOptions();

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

            $assProds = $this->getProduct()->getTypeInstance(true)
                         ->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct());

            foreach ($assProds
                     as $associatedProduct) {

                $optionValue = $associatedProduct->getData($productAttribute->getAttributeCode());

                if (array_key_exists($optionValue, $optionsByValue)) {
                    // If option available in associated product
                    if (!isset($values[$item->getId() . ':' . $optionValue])) {
                        // If option not added, we will add it.
                        $values[$item->getId() . ':' . $optionValue] = array(
                            'product_super_attribute_id' => $item->getId(),
                            'value_index'                => $optionValue,
                            'label'                      => $optionsByValue[$optionValue],
                            'default_label'              => $optionsByValue[$optionValue],
                            'store_label'                => $optionsByValue[$optionValue],
                            'is_percent'                 => 0,
                            'pricing_value'              => null,
                            'use_default_value'          => true
                        );
                    }
                }
            }
        }

        foreach ($pricings[0] as $pricing) {
            // Addding pricing to options
            $valueKey = $pricing['product_super_attribute_id'] . ':' . $pricing['value_index'];
            if (isset($values[$valueKey])) {
                $values[$valueKey]['pricing_value']     = $pricing['pricing_value'];
                $values[$valueKey]['is_percent']        = $pricing['is_percent'];
                $values[$valueKey]['value_id']          = $pricing['value_id'];
                $values[$valueKey]['use_default_value'] = true;
            }
        }

        if ($websiteId && isset($pricings[$websiteId])) {
            foreach ($pricings[$websiteId] as $pricing) {
                $valueKey = $pricing['product_super_attribute_id'] . ':' . $pricing['value_index'];
                if (isset($values[$valueKey])) {
                    $values[$valueKey]['pricing_value']     = $pricing['pricing_value'];
                    $values[$valueKey]['is_percent']        = $pricing['is_percent'];
                    $values[$valueKey]['value_id']          = $pricing['value_id'];
                    $values[$valueKey]['use_default_value'] = false;
                }
            }
        }

/*          foreach ($values as $key => $row) {
            $val[$key]  = $row['label'];
            $i++;
        }
        array_multisort($val, SORT_ASC, $values);
*/

        $val = array_column($values, 'label');
        $val[0] = ""; // fix first row
        array_multisort($val, SORT_ASC, $values);

        foreach ($values as $data) {
            $this->getItemById($data['product_super_attribute_id'])->addPrice($data);
        }
    }
    return $this;
}

At the bottom, I added code to sort (array_multisort) the $values multidimensional array by 'label'. I also added code to ignore the first row when sorting. This works for the first attribute but the 2nd and 3rd attributes are in alphabetical order (default not pre-selected). Is this the approach I should use? Am I missing something? Any suggestions? I'm using Magento 1.9.1.

Current Results:

Color
    Blue
    Black
    Green
    Orange
    Red
    Yellow
Size
    Large
    Small

Blue and Small should be the defaults but attribute 2 is not showing correct default:

Color
    Blue
    Black
    Green
    Orange
    Red
    Yellow
Size
    Small
    Large

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.