Magento 1.9.1不按排序配置产品属性下拉

问题描述:

新安装的Magento 1.9.1。Magento 1.9.1不按排序配置产品属性下拉

Magento忽略在Catalogue-> Attributes-> Manage Attributes-> Manage Labels/Options中为可配置产品下拉菜单设置的属性位置。而是使用产品ID来确定列表顺序。

对以下文件/函数进行了比较,除了少量税款计算之外,自1.7.0.2以来没有任何代码发生过更改。

法师/目录/型号/产品/类型/ Configuarable.php:

public function getConfigurableAttributes($product = null) 

法师/目录/型号/产品/ Option.php:

public function getProductOptionCollection(Mage_Catalog_Model_Product $product) 

法师/目录/座/产品/浏览/类型/ Configuarable.php:

public function getJsonConfig() 

我还测试了直播网站的副本数据库和所有属性的排序上是基于产品ID。

复制。

  1. 创建一个属性 - 颜色

  2. 添加标签 - 黑,红,绿,蓝

  3. 保存属性。

  4. 使用以上顺序的属性创建可配置和简单的关联产品。

编辑属性和更改标签位置。蓝色0,绿色1,红色3,黑色4

查看产品时,Magento仍然按产品ID排序属性并忽略位置。

+0

不任何人有关于如何设置排序顺序回到位置的任何想法? – hejhog 2014-12-07 10:26:01

由Meogi提供的答案并不完美,因为它只会对前端的选项进行排序。尝试从管理面板为可配置产品创建订单。您仍然会得到错误排序的属性选项列表。

相反,您可以将app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php复制到本地文件夹app/code/local/Mage/Catalog/Model/Resource /Product/Type/Configurable/Attribute/Collection.php并应用该补丁:

Index: app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
=================================================================== 
--- app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
+++ app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
@@ -301,7 +301,28 @@ 
        } 
       } 
      } 

+   /** 
+    * Mage 1.9+ fix for configurable attribute options not sorting to position 
+    * @author Harshit <[email protected]> 
+    */ 
+   $sortOrder = 1; 
+   foreach ($this->_items as $item) { 
+    $productAttribute = $item->getProductAttribute(); 
+    if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) { 
+     continue; 
+    } 
+    $options = $productAttribute->getFrontend()->getSelectOptions(); 
+    foreach ($options as $option) { 
+     if (!$option['value']) { 
         continue; 
        } 

+     if (isset($values[$item->getId() . ':' . $option['value']])) { 
+      $values[$item->getId() . ':' . $option['value']]['order'] = $sortOrder++; 
+     } 
+    } 
+   } 
+   usort($values, function ($a, $b) { 
+    return $a['order'] - $b['order']; 
+   }); 
+    
      foreach ($values as $data) { 
       $this->getItemById($data['product_super_attribute_id'])->addPrice($data); 
      } 

如果你正在犹豫复制的跨越核心文件下载到本地文件夹,然后我可以创造一个快速的模块,这<rewrite> Collection.php文件,并重写_loadPrices()函数并引入此修复程序。

+2

我修补了这个文件,并为那些不知道该怎么做的人创建了一个漫步通过http://magentosupport.help/knowledgebase/solved-sort-configurable-product-attribute-options-and-dropdowns/ – 2015-04-17 14:28:43

+0

这里也有答案(http://magento.stackexchange.com/a/76090/14992),它做了类似的事情,但是在本地模块中,而不是基于文件的覆盖 – 2015-08-06 21:08:43

+3

@Harshit,谢谢你的解决方案。对于任何需要它的人,我已经把它放在了一个模块[这里](https://github.com/rossmc/SwatchSorting)。 – Holly 2016-05-11 14:39:05

注意:此处列出的解决方案扩展了Magento核心库中的块类文件。我在这种方法之前审查了Magento的源代码,并确定没有一个好的事件可以避免这种做法。如果在未来的Magento版本中解决了这个排序问题,您可以简单地通过禁用它的应用/ etc/modules XML文件中的扩展名来撤消这些更改。

步骤1:创建文件应用的/ etc /模块/ FirstScribe_CatalogOptionSortFix.xml

内容:

<?xml version="1.0"?> 
<config> 
    <modules> 
     <FirstScribe_CatalogOptionSortFix> 
      <active>true</active> 
      <codePool>local</codePool> 
      <depends> 
       <Mage_Catalog /> 
      </depends> 
     </FirstScribe_CatalogOptionSortFix> 
    </modules> 
</config> 

注:对于步骤2和3,创建这些目录必要的文件。例如,您可能已经拥有目录app/code/local,或者您可能没有,具体取决于您的网站上已安装了哪些扩展。

步骤2:创建文件应用程序/代码/本地/ FirstScribe/CatalogOptionSortFix的/ etc/config.xml中

内容:

<?xml version="1.0"?> 
<!-- 
/** 
* Magento 1.9.1.0 has a bug in that the configurable options are sorted by 
* ID rather than position for the Configurable Product's front end view script. 
* This extension addresses this problem. 
* 
* @category FirstScribe 
* @package  FirstScribe_CatalogOptionSortFix 
* @version  2014.12.15 
*/ 
--> 
<config> 
    <modules> 
     <FirstScribe_CatalogOptionSortFix> 
      <version>1.0.0</version> 
     </FirstScribe_CatalogOptionSortFix> 
    </modules> 
    <global> 
     <blocks> 
      <catalog> 
       <rewrite> 
        <product_view_type_configurable>FirstScribe_CatalogOptionSortFix_Block_Product_View_Type_Configurable</product_view_type_configurable> 
       </rewrite> 
      </catalog> 
     </blocks> 
    </global> 
</config> 

步骤3:创建文件app/code/local/FirstScribe/CatalogOptionSortFix/Block/Product/View/Type/Configurable.php

内容:

<?php 
/** 
* Magento 1.9.1.0 has a bug in that the configurable options are sorted by 
* ID rather than position for the Configurable Product's front end view script. 
* This extension addresses this problem. 
* 
* @category FirstScribe 
* @package  FirstScribe_CatalogOptionSortFix 
* @version  2014.12.15 
*/ 
class FirstScribe_CatalogOptionSortFix_Block_Product_View_Type_Configurable extends Mage_Catalog_Block_Product_View_Type_Configurable 
{ 
    /** 
    * @var Magento_Db_Adapter_Pdo_Mysql 
    */ 
    protected $_read; 

    /** 
    * @var string 
    */ 
    protected $_tbl_eav_attribute_option; 

    /** 
    * Composes configuration for js 
    * 
    * @version 2014.12.15 - Addition of this line: 
    * $info['options'] = $this->_sortOptions($info['options']); 
    * 
    * @return string 
    */ 
    public function getJsonConfig() 
    { 
     $attributes = array(); 
     $options = array(); 
     $store  = $this->getCurrentStore(); 
     $taxHelper = Mage::helper('tax'); 
     $currentProduct = $this->getProduct(); 

     $preconfiguredFlag = $currentProduct->hasPreconfiguredValues(); 
     if ($preconfiguredFlag) { 
      $preconfiguredValues = $currentProduct->getPreconfiguredValues(); 
      $defaultValues  = array(); 
     } 

     foreach ($this->getAllowProducts() as $product) { 
      $productId = $product->getId(); 

      foreach ($this->getAllowAttributes() as $attribute) { 
       $productAttribute = $attribute->getProductAttribute(); 
       $productAttributeId = $productAttribute->getId(); 
       $attributeValue  = $product->getData($productAttribute->getAttributeCode()); 
       if (!isset($options[$productAttributeId])) { 
        $options[$productAttributeId] = array(); 
       } 

       if (!isset($options[$productAttributeId][$attributeValue])) { 
        $options[$productAttributeId][$attributeValue] = array(); 
       } 
       $options[$productAttributeId][$attributeValue][] = $productId; 
      } 
     } 

     $this->_resPrices = array(
      $this->_preparePrice($currentProduct->getFinalPrice()) 
     ); 

     foreach ($this->getAllowAttributes() as $attribute) { 
      $productAttribute = $attribute->getProductAttribute(); 
      $attributeId = $productAttribute->getId(); 
      $info = array(
        'id'  => $productAttribute->getId(), 
        'code'  => $productAttribute->getAttributeCode(), 
        'label'  => $attribute->getLabel(), 
        'options' => array() 
      ); 

      $optionPrices = array(); 
      $prices = $attribute->getPrices(); 
      if (is_array($prices)) { 
       foreach ($prices as $value) { 
        if(!$this->_validateAttributeValue($attributeId, $value, $options)) { 
         continue; 
        } 
        $currentProduct->setConfigurablePrice(
          $this->_preparePrice($value['pricing_value'], $value['is_percent']) 
        ); 
        $currentProduct->setParentId(true); 
        Mage::dispatchEvent(
          'catalog_product_type_configurable_price', 
          array('product' => $currentProduct) 
        ); 
        $configurablePrice = $currentProduct->getConfigurablePrice(); 

        if (isset($options[$attributeId][$value['value_index']])) { 
         $productsIndex = $options[$attributeId][$value['value_index']]; 
        } else { 
         $productsIndex = array(); 
        } 

        $info['options'][] = array(
          'id'  => $value['value_index'], 
          'label'  => $value['label'], 
          'price'  => $configurablePrice, 
          'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']), 
          'products' => $productsIndex, 
        ); 
        $optionPrices[] = $configurablePrice; 
       } 
      } 

      // CALL SORT ORDER FIX 
      $info['options'] = $this->_sortOptions($info['options']); 

      /** 
      * Prepare formated values for options choose 
      */ 
      foreach ($optionPrices as $optionPrice) { 
       foreach ($optionPrices as $additional) { 
        $this->_preparePrice(abs($additional-$optionPrice)); 
       } 
      } 
      if($this->_validateAttributeInfo($info)) { 
       $attributes[$attributeId] = $info; 
      } 

      // Add attribute default value (if set) 
      if ($preconfiguredFlag) { 
       $configValue = $preconfiguredValues->getData('super_attribute/' . $attributeId); 
       if ($configValue) { 
        $defaultValues[$attributeId] = $configValue; 
       } 
      } 
     } 

     $taxCalculation = Mage::getSingleton('tax/calculation'); 
     if (!$taxCalculation->getCustomer() && Mage::registry('current_customer')) { 
      $taxCalculation->setCustomer(Mage::registry('current_customer')); 
     } 

     $_request = $taxCalculation->getDefaultRateRequest(); 
     $_request->setProductClassId($currentProduct->getTaxClassId()); 
     $defaultTax = $taxCalculation->getRate($_request); 

     $_request = $taxCalculation->getRateRequest(); 
     $_request->setProductClassId($currentProduct->getTaxClassId()); 
     $currentTax = $taxCalculation->getRate($_request); 

     $taxConfig = array(
       'includeTax'  => $taxHelper->priceIncludesTax(), 
       'showIncludeTax' => $taxHelper->displayPriceIncludingTax(), 
       'showBothPrices' => $taxHelper->displayBothPrices(), 
       'defaultTax'  => $defaultTax, 
       'currentTax'  => $currentTax, 
       'inclTaxTitle'  => Mage::helper('catalog')->__('Incl. Tax') 
     ); 

     $config = array(
       'attributes'  => $attributes, 
       'template'   => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()), 
       'basePrice'   => $this->_registerJsPrice($this->_convertPrice($currentProduct->getFinalPrice())), 
       'oldPrice'   => $this->_registerJsPrice($this->_convertPrice($currentProduct->getPrice())), 
       'productId'   => $currentProduct->getId(), 
       'chooseText'  => Mage::helper('catalog')->__('Choose an Option...'), 
       'taxConfig'   => $taxConfig 
     ); 

     if ($preconfiguredFlag && !empty($defaultValues)) { 
      $config['defaultValues'] = $defaultValues; 
     } 

     $config = array_merge($config, $this->_getAdditionalConfig());  

     return Mage::helper('core')->jsonEncode($config); 
    } 

    /** 
    * Sort the options based off their position. 
    * 
    * @param array $options 
    * @return array 
    */ 
    protected function _sortOptions($options) 
    { 
     if (count($options)) { 
      if (!$this->_read || !$this->_tbl_eav_attribute_option) { 
       $resource = Mage::getSingleton('core/resource'); 

       $this->_read = $resource->getConnection('core_read'); 
       $this->_tbl_eav_attribute_option = $resource->getTableName('eav_attribute_option'); 
      } 

      // Gather the option_id for all our current options 
      $option_ids = array(); 
      foreach ($options as $option) { 
       $option_ids[] = $option['id']; 

       $var_name = 'option_id_'.$option['id']; 
       $$var_name = $option; 
      } 

      $sql = "SELECT `option_id` FROM `{$this->_tbl_eav_attribute_option}` WHERE `option_id` IN('".implode('\',\'', $option_ids)."') ORDER BY `sort_order`"; 
      $result = $this->_read->fetchCol($sql); 

      $options = array(); 
      foreach ($result as $option_id) { 
       $var_name = 'option_id_'.$option_id; 
       $options[] = $$var_name; 
      } 
     } 

     return $options; 
    } 
} 

第4步:如果启用,刷新Magento的 “配置” 缓存类型系统下 - >管理面板的高速缓存管理。

扩展概述

  1. 扩展Mage_Catalog_Block_Product_View_Type_Configurable类。
  2. 添加一种方法,通过从数据库中提取此信息,按其position值排序选项。
  3. 在收集属性的选项后,重写getJsonConfig方法以调用我们的新函数。
+0

这个答案是正确的,完美的作品,谢谢。希望它不会导致任何问题,一旦正式更新发布......我很惊讶,关于这个话题没有更多的。 – Joe 2015-01-27 22:13:07

+2

@Joe您的欢迎。关于更新的反馈 - 解决方案是以一种方式编写的,这样一旦Magento的核心团队在更新中修复了问题,我们所要做的就是在'app/etc/modules/FirstScribe_CatalogOptionSortFix.xml'中禁用此扩展,并且所有内容都将返回使用核心法师类,不要再重写核心类。 – Meogi 2015-01-27 23:08:49

+0

我按照你的指示,它在产品页面上完美的工作。商品列表页面上的颜色样本仍然不合格。你有什么建议? – tzvi 2015-05-13 18:35:15

覆盖属性集合并添加代码更改如下。这将纠正排序问题以及加载高选项值的问题。 “该usort在价格给予问题,所以注释掉”

<?php 
class Custom_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection extends Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection { 

    protected static $_pricings = array(); 

    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(); 

      //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) { 
       // 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 $data) { 
       $this->getItemById($data['product_super_attribute_id'])->addPrice($data); 
      } 
     } 
     return $this; 
    } 

} 

覆盖类Mage_Catalog_Block_Product_View_Type_Configurable 检查功能公共职能getJsonConfig() 变化$价格= $属性 - > getPrices();到$ prices = $ this - > _ sortPrices($ attribute-> getPrices()); 功能是如下

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; 
} 

我想补充我的两分钱,其他两个答案做得很好,指向我修复的方向,但我想我会在源攻击它,而不是块演示点。

您可以通过扩展Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection模型的_loadPrices()方法来获得相同的结果,尽管该名称是进行更改(推测可能是因为性能),导致属性按ID排序而不是按相关性排序。

更改似乎是为了避免嵌套的foreach语句,但也会丢失正确的顺序。此解决方案稍微修改更新后的逻辑以跟踪属性选项,然后根据原始顺序执行另一个循环以实际进行添加。

下面是类似meogi's answer above调整后的演练:


第1步:注册一个新的模块

注:如果你已经有一个,重新使用现有的。

# File: app/etc/modules/YourCompany_AttributeFix.xml 
<?xml version="1.0"?> 
<config> 
    <modules> 
     <YourCompany_AttributeFix> 
      <active>true</active> 
      <codePool>local</codePool> 
      <depends> 
       <Mage_Catalog /> 
      </depends> 
     </YourCompany_AttributeFix> 
    </modules> 
</config> 

第2步:创建模块的配置

# File: app/code/local/YourCompany/AttributeFix/etc/config.xml 
<?xml version="1.0"?> 
<config> 
    <modules> 
     <YourCompany_AttributeFix> 
      <version>0.1.0</version> 
     </YourCompany_AttributeFix> 
    </modules>  
    <global> 
     <models> 
      <catalog_resource> 
       <rewrite> 
        <product_type_configurable_attribute_collection>YourCompany_AttributeFix_Model_Resource_Product_Type_Configurable_Attribute_Collection</product_type_configurable_attribute_collection> 
       </rewrite> 
      </catalog_resource> 
     </models> 
    </global> 
</config> 

第3步:添加资源模型扩展

# File: app/code/local/YourCompany/AttributeFix/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
/** 
* Catalog Configurable Product Attribute Collection - overridden to re-enable the attribute option 
* sorting by relevance rather than by ID as changed in the Magento core class 
*/ 
class YourCompany_AttributeFix_Model_Resource_Product_Type_Configurable_Attribute_Collection 
    extends Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection 
{ 
    /** 
    * Load attribute prices information 
    * 
    * @return Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection 
    */ 
    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']; 
       } 

       /** 
       * Modification to re-enable the sorting by relevance for attribute options 
       * @author Robbie Averill <[email protected]> 
       */ 
       $toAdd = array(); 
       foreach ($this->getProduct()->getTypeInstance(true) 
          ->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct()) 
         as $associatedProduct) { 

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

        if (array_key_exists($optionValue, $optionsByValue)) { 
         $toAdd[] = $optionValue; 
        } 
       } 

       // Add the attribute options, but in the relevant order rather than by ID 
       foreach (array_intersect_key($optionsByValue, array_flip($toAdd)) as $optionValueKey => $optionValue) { 
        // If option available in associated product 
        if (!isset($values[$item->getId() . ':' . $optionValue])) { 
         // If option not added, we will add it. 
         $values[$item->getId() . ':' . $optionValueKey] = array(
          'product_super_attribute_id' => $item->getId(), 
          'value_index'    => $optionValueKey, 
          'label'      => $optionsByValue[$optionValueKey], 
          'default_label'    => $optionsByValue[$optionValueKey], 
          'store_label'    => $optionsByValue[$optionValueKey], 
          'is_percent'     => 0, 
          'pricing_value'    => null, 
          'use_default_value'   => true 
         ); 
        } 
       } 
       /** 
       * End attribute option order modification 
       * @author Robbie Averill <[email protected]> 
       */ 
      } 

      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 $data) { 
       $this->getItemById($data['product_super_attribute_id'])->addPrice($data); 
      } 
     } 
     return $this; 
    } 
} 

第4步:清除缓存


佛R参考,在git diff到核心类的实际变化将低于(不要直接编辑核心文件!):

diff --git a/app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php b/app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
index 135d9d3..4d2a59b 100644 
--- a/app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
+++ b/app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php 
@@ -254,6 +254,11 @@ class Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection 
        $optionsByValue[$option['value']] = $option['label']; 
       } 

+    /** 
+     * Modification to re-enable the sorting by relevance for attribute options 
+     * @author Robbie Averill <[email protected]> 
+     */ 
+    $toAdd = array(); 
       foreach ($this->getProduct()->getTypeInstance(true) 
           ->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct()) 
          as $associatedProduct) { 
@@ -261,22 +266,31 @@ class Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection 
        $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 
-       ); 
-      } 
+      $toAdd[] = $optionValue; 
        } 
       } 
+ 
+    // Add the attribute options, but in the relevant order rather than by ID 
+    foreach (array_intersect_key($optionsByValue, array_flip($toAdd)) as $optionValueKey => $optionValue) { 
+     // If option available in associated product 
+     if (!isset($values[$item->getId() . ':' . $optionValue])) { 
+      // If option not added, we will add it. 
+      $values[$item->getId() . ':' . $optionValueKey] = array(
+       'product_super_attribute_id' => $item->getId(), 
+       'value_index'    => $optionValueKey, 
+       'label'      => $optionsByValue[$optionValueKey], 
+       'default_label'    => $optionsByValue[$optionValueKey], 
+       'store_label'    => $optionsByValue[$optionValueKey], 
+       'is_percent'     => 0, 
+       'pricing_value'    => null, 
+       'use_default_value'   => true 
+      ); 
+     } 
+    } 
+    /** 
+     * End attribute option order modification 
+     * @author Robbie Averill <[email protected]> 
+     */ 
      } 

      foreach ($pricings[0] as $pricing) { 

This is also on GitHub如果有人想以供参考。我也有logged this as a bug with Magento

+1

它的真棒....谢谢你@Robbie – 2017-02-01 10:38:29

+0

@MilanGajjar请使用GitHub回购,自从这篇文章以来有一些错误修复 – 2017-02-01 10:39:24

+0

我已经使用你的git代码D:@Robbie – 2017-02-01 10:42:06

根据Magento的,这个问题将在CE 1.9.3.0

Magento的团队 |发布15年8月14日下午4时02

问题被安排在下未成年人(不是补丁)发布了在2016年上半年

来源预计:https://www.magentocommerce.com/bug-tracking/issue/index/id/413