【问题标题】:How to show same product attribute sort on compare page Magento如何在比较页面 Magento 上显示相同的产品属性排序
【发布时间】:2014-08-21 05:35:51
【问题描述】:

我不确定为什么比较页面上的产品属性排序与产品页面上的排序不同。就像在产品页面上一样,属性排序是

在产品页面上

1 name 
2 new attribute 
3 new attribute1 
4 color

但是当我比较时在comapre页面上[由于2个产品具有相同的属性,属性排序顺序变为

在比较页面上

1 name
2 color
3 new attribute
4 new attribute1 

我在谷歌上搜索了很多答案,但找不到。请帮我解决这个问题。

以下是我找到的功能

public function getComparableAttributes()
    {
        if (is_null($this->_comparableAttributes)) {
            $this->_comparableAttributes = array();
            $setIds = $this->_getAttributeSetIds();
            if ($setIds) {
                $attributeIds = $this->_getAttributeIdsBySetIds($setIds);

                $select = $this->getConnection()->select()
                    ->from(array('main_table' => $this->getTable('eav/attribute')))
                    ->join(
                        array('additional_table' => $this->getTable('catalog/eav_attribute')),
                        'additional_table.attribute_id=main_table.attribute_id'
                    )
                    ->joinLeft(
                        array('al' => $this->getTable('eav/attribute_label')),
                        'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int) $this->getStoreId(),
                        array('store_label' => $this->getConnection()->getCheckSql('al.value IS NULL', 'main_table.frontend_label', 'al.value'))
                    )
                    ->where('additional_table.is_comparable=?', 1)
                    ->where('main_table.attribute_id IN(?)', $attributeIds);
                $attributesData = $this->getConnection()->fetchAll($select);
                if ($attributesData) {
                    $entityType = Mage_Catalog_Model_Product::ENTITY;
                    Mage::getSingleton('eav/config')
                        ->importAttributesData($entityType, $attributesData);
                    foreach ($attributesData as $data) {
                        $attribute = Mage::getSingleton('eav/config')
                            ->getAttribute($entityType, $data['attribute_code']);
                        $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
                    }
                    unset($attributesData);
                }
            }
        }
        return $this->_comparableAttributes;
    }

/**
 * Load Comparable attributes
 *
 * @return Mage_Catalog_Model_Resource_Product_Compare_Item_Collection
 */
public function loadComparableAttributes()
{
    $comparableAttributes = $this->getComparableAttributes();
    $attributes = array();
    foreach ($comparableAttributes as $attribute) {
        $attributes[] = $attribute->getAttributeCode();
    }
    $this->addAttributeToSelect($attributes);

    return $this;
}

但我不明白如何按排序顺序过滤它。请建议

【问题讨论】:

    标签: magento sorting attributes compare webpage


    【解决方案1】:

    检查以下函数中的修改: 使用的文件是 vendor/magento/module-catalog/Model/ResourceModel/Product/Compare/Item/Collection.php 但这不是最好的方法。您需要根据 Magento 标准覆盖它。 我正在努力,很快就会更新。

    public function getComparableAttributes()
    {
        if ($this->_comparableAttributes === null) {
            $this->_comparableAttributes = [];
            $setIds = $this->_getAttributeSetIds();
            if ($setIds) {
                $attributeIds = $this->_getAttributeIdsBySetIds($setIds);
    
                $select = $this->getConnection()->select()->from(
                    ['main_table' => $this->getTable('eav_attribute')]
                )->join(
                    ['additional_table' => $this->getTable('catalog_eav_attribute')],
                    'additional_table.attribute_id=main_table.attribute_id'
                )->joinLeft(
                    ['al' => $this->getTable('eav_attribute_label')],
                    'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int)$this->getStoreId(),
                    [
                        'store_label' => $this->getConnection()->getCheckSql(
                            'al.value IS NULL',
                            'main_table.frontend_label',
                            'al.value'
                        )
                    ]
                )
                        ->joinLeft( //add sort order to sort the attributes as per backend sort.  -- Abid
                    ['as' => $this->getTable('eav_entity_attribute')],
                    'as.attribute_id = main_table.attribute_id'
                )
                        ->where(
                    'additional_table.is_comparable=?',
                    1
                )->where(
                    'main_table.attribute_id IN(?)',
                    $attributeIds
                )
                        ->order('as.sort_order') //sort by sort_order -- Abid
                        ;
                $attributesData = $this->getConnection()->fetchAll($select);
                if ($attributesData) {
                    $entityType = \Magento\Catalog\Model\Product::ENTITY;
                    $this->_eavConfig->importAttributesData($entityType, $attributesData);
                    foreach ($attributesData as $data) {
                        $attribute = $this->_eavConfig->getAttribute($entityType, $data['attribute_code']);
                        $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
                    }
                    unset($attributesData);
                }
            }
        }
        return $this->_comparableAttributes;
    }
    

    【讨论】:

      【解决方案2】:

      在产品页面上的属性按属性集排序。

      在比较页面属性根本不排序。 您可以在 Mage_Catalog_Model_Resource_Product_Compare_Item_Collection 类中重写函数 getComparableAttributes 并实现自己的排序逻辑。

      但请注意,此功能在不同的 Magento 版本中有所不同。您可以尝试使用免费扩展ET Advanced Compare 或从该扩展中获取部分代码(代码可在 bitbucket.org 上找到。链接 i 在扩展页面上)

      【讨论】:

      • 我用的是企业版,扩展和这个版本不兼容
      • 您可以从 AdvancedCompare 扩展 (bitbucket.org/etws/et_advancedcompare/src/…) 中获取此更改后的功能
      • 是的,我已经从那里得到了它,感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多