Magento 1.9.1 not sorting Configurable product attributes dropdown by position

后端 未结 5 1503
庸人自扰
庸人自扰 2021-02-20 04:42

Fresh install of Magento 1.9.1.

Magento is ignoring the attribute position set in Catalogue->Attributes->Manage Attributes->Manage Labels/Options for a configurable prod

5条回答
  •  囚心锁ツ
    2021-02-20 05:36

    Override the attribute collection and add the code changes as below. This will rectify the sort problem and also, the problem of loading of high options values. "The usort is giving problem in the prices, so commented out"

    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();
    
                //custom codes
                if (!Mage::app()->getStore()->isAdmin() && isset(self::$_pricings[$this->getProduct()->getId()])) {
                    $values = self::$_pricings[$this->getProduct()->getId()];
    
                } else {//custom codes
                    foreach ($this->_items as $item) {
                        $productAttribute = $item->getProductAttribute();
                        if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) {
                            continue;
                        }
                        $options = $productAttribute->getFrontend()->getSelectOptions();
    
                        $optionsByValue = array();
                        $sortOrders = array(); //custom codes
                        $sortOrder = 1; //custom codes
                        foreach ($options as $option) {
                            $optionsByValue[$option['value']] = $option['label'];
                            $sortOrders[$option['value']] = $sortOrder++; //custom codes
                        }
    
                        foreach ($this->getProduct()->getTypeInstance(true)
                                     ->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct())
                                 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,
                                        'sort_order'                 => $sortOrders[$optionValue] //custom codes
                                    );
                                }
                            }
                        }
                    }
    
                    //custom codes
                    self::$_pricings[$this->getProduct()->getId()] = $values;
                    /**usort($values, function($a, $b) {
                        return $a['sort_order'] > $b['sort_order'];
                    });**/
                }
    
                foreach ($pricings[0] as $pricing) {
                    // Affffding 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 $data) {
                    $this->getItemById($data['product_super_attribute_id'])->addPrice($data);
                }
            }
            return $this;
        }
    
    }
    

    override the class Mage_Catalog_Block_Product_View_Type_Configurable check for the function public function getJsonConfig() change $prices = $attribute->getPrices(); to $prices = $this->_sortPrices($attribute->getPrices()); The function is as below

    public function _sortPrices($prices) {
        $sort_orders = array();
        $sorted_prices = array();
        foreach($prices as $key => $value) {
            $sort_orders[$key] = $value['sort_order'];
        }
        asort($sort_orders);
    
        foreach($sort_orders as $key => $value) {
            $sorted_prices[] = $prices[$key];
        }
        return $sorted_prices;
    }
    

提交回复
热议问题