Magento 以任意顺序获取产品集合

Magento get a product collection in an arbitrary order(Magento 以任意顺序获取产品集合)
本文介绍了Magento 以任意顺序获取产品集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我为我们的 Magento 商店开发了一个自定义搜索引擎,我正在尝试以非常特定的顺序加载产品系列(我已经根据我设计的算法对结果进行了排名).

I have developed a custom search engine for our Magento store and I am trying to load the product collection in a very specific order (I have ranked the results according to an algorithm I designed).

我可以正确加载产品集合,但它不是我想要的顺序.现在它的工作原理如下:

I can load the product collection correctly, however it is not in the order that I would like it to be in. Here is basically how it is working now:

我的数据库查询基本上返回一个包含产品 ID 的 PHP 数组.对于这个例子,假设它看起来像这样:

My database query basically comes back with a PHP array of product IDs. For this example lets say it looks like this:

$entity_ids = array(140452, 38601 );

现在我可以调换 140452 和 38601,并且产品集合每次都以相同的顺序返回.我希望产品集合与实体 ID 的 ID 顺序相同.

Now I can transpose the 140452 and the 38601 and the product collection comes back in the same order each time. I would like the product collection to be in the same order as the ID of the entity ids.

我用来创建我的收藏的代码如下:

The code I am using to create my collection is as follows:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $entity_ids))
    ->setPageSize($results_per_page)
    ->setCurPage($current_page)
    ->load();

有没有办法将排序顺序设置为 $entity_ids 数组的顺序?

Is there a way to set the sort order to be the order of the $entity_ids array?

推荐答案

集合继承自类

Varien_Data_Collection_Db

该类中有一个名为 addOrder 的方法.

There's a method named addOrder on that class.

public function addOrder($field, $direction = self::SORT_ORDER_DESC)
{
    return $this->_setOrder($field, $direction);
}

所以,你会认为这样的东西应该适用于基本的订购

So, you'd think something like this should work for basic ordering

Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addOrder('entity_id');

然而,事实并非如此.由于 EAV 集合中涉及的复杂连接,有一种特殊的方法用于向 order 子句添加属性

However, it doesn't. Because of the complex joining involved in EAV Collections, there's a special method used to add an attribute to the order clause

Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::addAttributeToSort

然而,这只能用于添加简单的属性.要创建任意排序,您需要直接操作 Zend_Select 对象.我不喜欢这个,也不喜欢使用自定义的 mysql 函数来实现,但它似乎是唯一的方法

However again, this can only be used to add simple attributes. To create an arbitrary sort, you'll need to manipulate the Zend_Select object directly. I'm not a big fan of this, and I'm not a big fan of using custom mysql functions to achieve things, but it appears it's the only way to do this

我在股票安装上测试了以下代码并获得了预期的结果.您应该能够使用它来获得您想要的东西.

I tested the following code on a stock install and got the desired results. You should be able to use it to get what you want.

        $ids = array(16,18,17,19);
        $products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('entity_id',$ids);           

                    //shakes fist at PDO's array parameter
                    $ids = array_map('intval', $ids);
        $products->getSelect()->order("find_in_set(e.entity_id,'".implode(',',$ids)."')");
        foreach($products as $product)
        {
            var_dump($product->getEntityId());
            var_dump($product->getSku());
        }

这篇关于Magento 以任意顺序获取产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)