【问题标题】:Sort associated products by cutom attribute within a grouped product in Magento在 Magento 的分组产品中按自定义属性对关联产品进行排序
【发布时间】:2011-10-27 21:55:13
【问题描述】:

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

我已将 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”,并尝试通过修改集合来按自定义属性对关联产品进行排序:

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

但这并没有改变! :(

非常感谢您的帮助。

【问题讨论】:

    标签: magento


    【解决方案1】:

    您需要确保您的属性包含在选择中才能按它排序。我也做了同样的事情来按 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 中对我有用。请注意,您仍然按位置排序。所以您的查询会考虑到这一点。

    【讨论】:

    • 谢谢 Mike D,我已经这样做了,但问题是我的属性值中有带逗号的数字(如 3; 5; 7,5 ; 15),它们存储为文本领域!!
    【解决方案2】:

    也许这会有所帮助:

    $_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;
    });
    

    【讨论】:

    • 非常优雅。我可以补充一下://按价格排序 usort($_associatedProducts, function ($a, $b) { if (floatval($a->getPrice())==floatval($b->getPrice())) { return 0; } return floatval($a->getPrice())getPrice()) ? -1 : 1; });
    • 非常好,您在这里使用了回调函数来对集合进行排序。这个用了几次!谢谢!
    【解决方案3】:

    我也遇到过这类问题;也进行了广泛的搜索,结果,我找到了这个线程,但没有答案..仍然没有答案!。

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

    这有点骇人听闻,但我所做的是:

    <?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 欢呼。

    【讨论】:

    • 或只是 $_item->getAttribute('my_attribute') 而不是 getAttributeText.. 取决于您的属性类型。如果我没记错的话,我的是一个下拉属性或其他东西。
    【解决方案4】:

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

    <?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)

    【讨论】:

      猜你喜欢
      • 2014-06-09
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多