在Magento中按分组产品中的自定义属性对关联产品进行排序

时间:2011-08-16 09:26:26

标签: magento

如何在Magento的分组产品中按cutom属性对相关产品进行排序,

我已将文件Grouped.php复制到:app / code / local / Mage / Catalog / Model / Product / Type / Grouped.php

并尝试在我的getAssociatedProducts函数中添加它:

->addAttributeToSort('my_attribute', 'DESC');

但它不起作用,任何人都可以帮忙吗?

[编辑]

我已经覆盖了文件“app / code / core / Mage / Catalog / Model / Product / Type / Grouped.php”,我试图通过修改集合来按照自定义属性对关联产品进行排序:< / p>

$collection = $this->getAssociatedProductCollection($product)
    ->addAttributeToSelect('*')
    ->addFilterByRequiredOptions()
    ->setPositionOrder()
    ->addStoreFilter($this->getStoreFilter($product))
    ->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)))
    ->addAttributeToSort('my_attribute', 'DESC');

但是没有改变!! :(

非常感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

您需要确保您的属性包含在select中,以便能够对其进行排序。我也做同样的事情来按entity_id订购产品。

public function getAssociatedProducts($product = null)
{
    if (!$this->getProduct($product)->hasData($this->_keyAssociatedProducts)) {
        $associatedProducts = array();

        if (!Mage::app()->getStore()->isAdmin()) {
            $this->setSaleableStatus($product);
        }

        $collection = $this->getAssociatedProductCollection($product)
            ->addAttributeToSelect('*')
            ->addFilterByRequiredOptions()
            ->setPositionOrder()
            //MRD added to order by product id also.
            ->addAttributeToSort('entity_id', 'ASC')
            //END MRD
            ->addStoreFilter($this->getStoreFilter($product))
            ->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)));

        foreach ($collection as $item) {
            $associatedProducts[] = $item;
        }

        $this->getProduct($product)->setData($this->_keyAssociatedProducts, $associatedProducts);
    }
    return $this->getProduct($product)->getData($this->_keyAssociatedProducts);
}

这在Magento 1.4.1.1中对我有用。请注意,您仍然按位置订购。所以你的查询会考虑到这一点。

答案 1 :(得分:1)

也许这会有所帮助:

$_associatedProducts = $this->getAssociatedProducts();

usort($_associatedProducts, function ($a, $b) {
    if (intval($a->getCustomPosition())==intval($b->getExtraPosition())) {
        return 0;
    }
    return intval($a->getExtraPosition())<intval($b->getExtraPosition()) ? -1 : 1;
});

答案 2 :(得分:0)

我也碰到了这类问题;广泛搜索,结果,我发现这个线程但没有答案..仍然没有答案!

不幸的是,getAssociatedProducts()似乎只返回一个关联树数组,而不是常规的ProductCollection,它有我们想要的那个方法(addAttributeToSort)。

这有点像hackish,但我所做的却是:

<?php 
foreach($_associatedProducts as $_item) {
    $index[$_item->getAttributeText('my_attribute')] = $_item; 
}
ksort($index);
?>

这里的结果是我们将有一个按键排序的关联数组。然后我们可以使用它并继续显示相关产品:

<table class="data-table grouped-items-table" id="super-product-table">
<?php foreach($index as $_item) ?>
<tr>
     <td><?= $_item->getName() ?></td>
     <td><?= $this->getPriceHtml($_item, true) ?></td>
     <!-- etc.... some other stuff you want to display-->
</tr>
<?php endforeach; ?>
</table>

虽然不是一个非常好的解决方案,但也不错。适合我!如果有人有更优雅的解决方案,我也很乐意看到它! :D欢呼。

答案 3 :(得分:0)

此解决方案很有效,直到您有多个具有相同“my-attribute”值的关联产品,如大小或价格。在这种情况下,它只选择其中一个选项并跳过其余选项。如果用于对产品进行排序的属性值具有相同的值,我会更改它们,添加一个递增的计数器变量,以便显示所有选项。不是最优雅的解决方案,但它对我有用。顺便说一下,您可以创建其中几个索引来自定义不同属性集的排序方法。

<?php $key_count = 1; ?>
<?php foreach($_associatedProducts as $_item) {
            $index[$_item->getAttributeText('wheel_size') . "-" . $key_count] = $_item; 
            $key_count++;
            }
            ksort($index);
?>

此外,如果您的属性值是存储为文本的数字,可能会向ksort添加一个排序标志,将其更改为数字类型,仅用于排序目的,如ksort($ index,SORT_NUMERIC)

相关问题