Hanya untuk menambahkan dua sen saya, dua jawaban yang lain dengan baik mengarahkan saya ke arah fix, tetapi saya pikir saya akan menyerangnya pada sumber daripada ke titik presentasi blok.
Anda dapat mencapai hasil yang sama dengan memperluas metode Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection
model _loadPrices()
, yang meskipun namanya adalah tempat perubahan dilakukan (mungkin untuk kinerja) menghasilkan atribut yang dipesan oleh ID daripada oleh relevansi.
Perubahan tampaknya telah dibuat untuk menghindari foreach
pernyataan bersarang , tetapi pada gilirannya kehilangan urutan yang benar juga. Solusi ini sedikit mengubah logika yang diperbarui untuk melacak opsi atribut, kemudian melakukan loop lain berdasarkan urutan asli untuk benar-benar melakukan penambahan.
Berikut langkah-langkah penyesuaian yang mirip dengan jawaban meogi di atas :
Langkah 1: Daftarkan modul baru
Catatan: jika Anda sudah memilikinya, gunakan kembali yang sudah ada.
# 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>
Langkah 2: Buat konfigurasi modul
# 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>
Langkah 3: Tambahkan ekstensi model sumber daya
# 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 <robbie.averill@kathmandu.co.nz>
*/
$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 <robbie.averill@kathmandu.co.nz>
*/
}
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;
}
}
Langkah 4: Kosongkan cache Anda
Untuk referensi , perubahan aktual ke kelas inti di a git diff
akan di bawah (jangan langsung mengedit file inti!):
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 <robbie.averill@kathmandu.co.nz>
+ */
+ $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 <robbie.averill@kathmandu.co.nz>
+ */
}
foreach ($pricings[0] as $pricing) {
Ini juga ada di GitHub jika ada yang menginginkannya untuk referensi.
Sunting: Saya juga mencatat ini sebagai bug pada Magento .