magento get products from category, order by rand()

后端 未结 3 454
别跟我提以往
别跟我提以往 2020-12-25 14:02

I have the following:

$products = Mage::getModel(\'catalog/product\')
    ->getCollection()
    ->addAttributeToSort(\'id\', \'RAND()\')
    ->addAt         


        
3条回答
  •  执笔经年
    2020-12-25 14:53

    Magento collection do not accept parameters other then one of selected attribute. In this case you should get Zend_Db_Select object and add order instruction to it.

    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSort()
        ->addAttributeToSelect('small_image')
        ->addCategoryFilter(Mage::getModel('catalog/category')->load());
    $products->getSelect()->order(new Zend_Db_Expr('RAND()'));
    

    To see what query will be executed you may use this construnction

    $products->load(true, true); // first parameter show sql query in output, second show sql query in var/log/syslog
    

提交回复
热议问题