Magento - 如何获取除具有特定状态的订单之外的所有订单

Magento - How to get all orders except the ones with specific status

提问人:Venelin 提问时间:7/14/2016 最后编辑:Venelin 更新时间:4/8/2019 访问量:1593

问:

我正在使用 Magento 1.9.2,并且正在开发自定义扩展。

以下是我用来选择所有具有特定状态的订单的代码:

<?php $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'complete'); ?>

如何选择除特定状态的订单外的所有订单?

这是我使用上面显示的代码的完整代码:

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'complete') ?>
<div class="page-title">
    <h1><?php echo $this->__('My Orders') ?></h1>
</div>
<?php echo $this->getChildHtml('info');?>
<?php echo $this->getPagerHtml(); ?>
<?php if($_orders->getSize()): ?>
<table class="data-table orders" id="my-orders-table">
    <col width="1" />
    <col width="1" />
    <col />
    <col width="1" />
    <col width="1" />
    <col width="1" />
    <thead>
        <tr>
            <th class="number"><?php echo $this->__('Order #') ?></th>
            <th class="date"><?php echo $this->__('Date') ?></th>
            <th class="ship"><?php echo $this->__('Ship To') ?></th>
            <th class="total"><span class="nobr"><?php echo $this->__('Order Total') ?></span></th>
            <th class="status"><span class="nobr"><?php echo $this->__('Order Status') ?></span></th>
            <th class="view">&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        <?php $_odd = ''; ?>
        <?php foreach ($_orders as $_order): ?>
        <tr>
            <td class="number"><?php echo $_order->getRealOrderId() ?></td>
            <td class="date"><span class="nobr"><?php echo $this->formatDate($_order->getCreatedAtStoreDate()) ?></span></td>
            <td class="ship"><?php echo $_order->getShippingAddress() ? $this->escapeHtml($_order->getShippingAddress()->getName()) : '&nbsp;' ?></td>
            <td class="total"><?php echo $_order->formatPrice($_order->getGrandTotal()) ?></td>
            <td class="status"><em><?php echo $_order->getStatusLabel() ?></em></td>
            <td class="a-center view">
                <span class="nobr"><a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>
                    <?php /*<span class="separator">|</span><a href="<?php echo $this->getTrackUrl($_order) ?>"><?php echo $this->__('Track Order') ?></a>&nbsp;*/ ?>
                    <?php if ($this->helper('sales/reorder')->canReorder($_order)) : ?>
                    <span class="separator">|</span> <a href="<?php echo $this->getReorderUrl($_order) ?>" class="link-reorder"><?php echo $this->__('Reorder') ?></a>
                <?php endif ?>
                </span>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<script type="text/javascript">decorateTable('my-orders-table');</script>
<?php echo $this->getPagerHtml(); ?>
<?php else: ?>
    <p><?php echo $this->__('You have placed no orders.'); ?></p>
<?php endif ?>

提前致谢!

PHP 马根托

评论


答:

2赞 Pawel Dubiel 7/14/2016 #1
<?php $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status',array('neq' => 'complete')); ?>

或者当您拥有多个状态时

<?php $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status',array('nin' => array('pending','complete'))); ?>

评论

0赞 Pawel Dubiel 7/14/2016
@TonyStark 这很奇怪:它对我有用。你得到什么样的错误。也许该错误与PHP中的可用内存有关
0赞 Pawel Dubiel 7/14/2016
@TonyStark尝试添加 addAttributeToSelect('*'),例如 $_orders = Mage::getModel('sales/order')->getCollection()->addAttributeToSelect('*') ->addFieldToFilter('status',array('neq' => 'complete')); ?>
0赞 Pawel Dubiel 7/14/2016
@TonyStark,如果您检查PHP错误日志,这将很有用。
0赞 Venelin 7/14/2016
我在哪里可以找到它?我收到一个空白页,没有任何错误?
0赞 Pawel Dubiel 7/14/2016
让我们在聊天中继续讨论
1赞 Stuart 7/14/2016 #2

您可以使用“不等于”:

...->addAttributeToFilter('status', array('neq' => 'thestatus'));

评论

0赞 zus 12/11/2019
我可以 magento.stackexchange.com/q/298382/57334@Stuart获得帮助吗
1赞 Naveenbos 7/14/2016 #3
<?php $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status',array('neq' => 'complete')); ?>
1赞 Mohammed Muzammil 4/8/2019 #4

获取已取消订单的状态

$orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'canceled');

foreach ($orders as $order) {
   echo $order->getStatus();
}

获取处理订单的状态

$orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', 'processing');

    foreach ($orders as $order) {
       echo $order->getStatus();
    }

只需更改订单状态即可获取相应订单的详细信息 您也可以从$order对象获取所有订单数据