1

我在 magento 的产品表中添加了自定义字段。我想使用该自定义字段进行排序,但不会。不知道是什么问题。。

    $_cproduct = Mage::getmodel('catalog/product');
    $_productCollection = $_cproduct->getCollection();
    $_productCollection->addAttributeToSelect('name')->addAttributeToSelect('expired')->addAttributeToSelect('dealcategory')->addAttributeToSelect('dealcity')->addAttributeToSelect('deal_position')->addAttributeToSelect('special_to_date')->addAttributeToSelect('title')->setStore(Mage::app()->getStore()->getId())->addStoreFilter(Mage::app()->getStore()->getId())->addAttributeToSort('entity_id', 'DESC')->addAttributeToSort('dealcategory', 'DESC');
    $_productCollection->addAttributeToFilter('status', 1)->addAttributeToFilter('expired', 0);


$_productCollection->addAttributeToFilter('dealcategory', array('in' => array(129, 135, 136, 137, 141, 156, 205)));


    $now = Mage::getModel('core/date')->timestamp(time());
    $dateStart = date('Y-m-d' . ' 00:00:00', $now);    

    $_productCollection->addFieldToFilter('special_to_date', array('from' => $dateStart));
    $dealcity = Mage::app()->getRequest()->getParam('dealcity', false);
    $_productCollection->addAttributeToSort('deal_position', 'ASC');


    echo "<pre>";
    print_r($_productCollection->getData());
    echo "</pre>";

但是每次我得到像下面这样的异常订单时,我都想根据“ deal_position”排序

Array
(
    [0] => Array
        (
            [entity_id] => 2568
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => virtual
            [sku] => comedybar7/2/12
            [created_at] => 2012-07-02 18:36:22
            [updated_at] => 2012-10-16 09:52:41
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side travel deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 135
            [special_to_date] => 2012-10-25 00:00:00
            [deal_position] => 4
        )

    [1] => Array
        (
            [entity_id] => 2566
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => virtual
            [sku] => livingwellfinal
            [created_at] => 2012-06-28 14:48:34
            [updated_at] => 2012-10-16 09:51:58
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-11-30 00:00:00
            [deal_position] => 1
        )

    [2] => Array
        (
            [entity_id] => 2565
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => simple
            [sku] => laser-toenail-10-toes
            [created_at] => 2012-06-27 21:10:19
            [updated_at] => 2012-10-16 09:52:23
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-11-30 00:00:00
            [deal_position] => 3
        )

    [3] => Array
        (
            [entity_id] => 2564
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => simple
            [sku] => laser-toenail-5-toes
            [created_at] => 2012-06-27 21:10:19
            [updated_at] => 2012-10-16 09:52:10
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-12-31 00:00:00
            [deal_position] => 2
        )

)

我使用addOrder()它也返回相同的输出。问题是什么?

4

1 回答 1

3

您似乎迷失在自己的代码中。您已添加3 个!在您的代码中排序:

line3: ...->addAttributeToSort('entity_id', 'DESC')->addAttributeToSort('dealcategory', 'DESC');
line10: $_productCollection->addAttributeToSort('deal_position', 'ASC');

确保你只有你需要的排序。另外,不要addAttributeToSelect多次使用,只使用一次,而是将数组作为参数传递:

$_productCollection->addAttributeToSelect(array('name', 'expired', ...));
于 2012-10-16T11:09:27.593 回答