简体   繁体   English

为什么$ this-> getOrders方法总是在此代码Magento中返回空对象

[英]Why is the $this->getOrders method always returns a null object in this code Magento

I am new to magento.I am trying to create a view for the purchase history for users.Already there is a page to display this(As I am using a template).But as a experiment I tried to create a new page with the same content.As my first step. 我是magento的新手,我正在尝试为用户创建购买历史记录的视图。已经有一个页面可以显示此内容(因为我正在使用模板)。但是作为实验,我尝试使用相同的内容。作为我的第一步。 I created a new php block and then I copied same the content from the old purchase history file to the new one (Without any change) .Then I called that block in a cms page.But when I called the url of the new page,It gives this error 我创建了一个新的php块,然后将内容从旧的购买历史文件复制到了新的php块中(没有任何更改) 。然后我在cms页面中调用了该块。但是当我调用新页面的url时,它给出了这个错误

Fatal error: Call to a member function getSize() on null in /home/bvbellpm/public_html/newtrades/app/design/frontend/base/default/template/customer/form/purchasehistory.phtml on line 致命错误:在线上/home/bvbellpm/public_html/newtrades/app/design/frontend/base/default/template/customer/form/purchasehistory.phtml中的null上调用成员函数getSize()

This is the code which I used to display the history 这是我用来显示历史记录的代码

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $_orders = $this->getOrders(); ?>
<div class="page-title">
    <h1><?php echo $this->__('My Orders') ?></h1>
</div>
<?php echo $this->getPagerHtml(); ?>
<?php if($_orders->getSize()>0): ?>
<table class="data-table" id="my-orders-table">
    <thead>
        <tr>
            <th><?php echo $this->__('Order #') ?></th>
            <th><?php echo $this->__('Date') ?></th>
            <th><?php echo $this->__('Ship To') ?></th>
            <th><span class="nobr"><?php echo $this->__('Order Total') ?></span></th>
            <th><span class="nobr"><?php echo $this->__('Order Status') ?></span></th>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        <?php $_odd = ''; ?>
        <?php foreach ($_orders as $_order): ?>
        <tr>
            <td><?php echo $_order->getRealOrderId() ?></td>
            <td><span class="nobr"><?php echo $this->formatDate($_order->getCreatedAtStoreDate()) ?></span></td>
            <td><?php echo $_order->getShippingAddress() ? $this->htmlEscape($_order->getShippingAddress()->getName()) : '&nbsp;' ?></td>
            <td><?php echo $_order->formatPrice($_order->getGrandTotal()) ?></td>
            <td><em><?php echo $_order->getStatusLabel() ?></em></td>
            <td class="a-center">
                <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>

But this same code works fine in the old purchase history file.Why is this not working in the new page? 但是,相同的代码在旧的购买历史记录文件中也可以正常工作,为什么在新页面中不起作用? .I think $this->getOrders() is returning a null object.But it is working fine in the old page.As far as I know $this->getOrders() is a magic method in Magento.So I think it can be used any where.Can someone explain me the reason for this odd behavior? 我认为$ this-> getOrders()返回一个空对象,但在旧页面中工作正常,据我所知, $ this-> getOrders()是Magento中的魔术方法,所以我认为它可以可以在任何地方使用。有人可以向我解释这种奇怪行为的原因吗?

You need to declare the new page in your template.xml with the same blocks (I mean the logic not the template). 您需要使用相同的块在template.xml中声明新页面(我的意思是逻辑不是模板)。 You most probably this template linked to a block of type sales/order_history . 您最有可能将此模板链接到类型为sales/order_history的块。

You can look at app/design/frontend/base/default/layout/sales.xml for an example. 您可以查看app/design/frontend/base/default/layout/sales.xml作为示例。

The idea is to have the function getOrders in the logic of the template. 想法是在模板的逻辑中具有getOrders函数。 If you call a normal block (eg core/template) to be able to call it. 如果您调用普通块(例如核心/模板)就可以调用它。 Else, the getOrders tries to retrieve the variable orders of the block. 否则,getOrders尝试检索块的可变顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM