Magento - 添加自定义批量操作PDF

时间:2012-04-02 17:56:27

标签: php magento magento-1.5

快速提问(阅读时请记住这一点):

为什么会产生此错误(在解释中)以及如何编辑pdfRmaAction()才能正常工作(批量操作打印)???

**在Magento v.1.10.1.0工作,与v.1.5.1.0

相同

冗长的解释:

我已下载此扩展程序(http://www.magentocommerce.com/magento-connect/admin-order-printing-extension.html)以向每个订单添加一个按钮,这样当您进入订单时,您有一个额外的按钮来打印RMA(自定义)从这个扩展模型pdf - 变成基于发票的自定义RMA表单)

效果很好。但是,我想在其中添加批量操作打印,以便您可以检查一些订单并从下拉列表中选择打印RMA 并打印这些订单的表单。

在扩展程序config.xml文件(app / code / local / Nastnet / OrderPrint / etc /)中,<config>标记内是:

<modules>
    <Nastnet_OrderPrint>
        <version>0.1.3</version>
    </Nastnet_OrderPrint>
</modules>

<global>
    <blocks>
        <adminhtml>
            <rewrite>
                <sales_order_grid>Nastnet_OrderPrint_Block_Sales_Order_Grid</sales_order_grid> <!-- ADDED THIS FOR MASS ACTION PRINTING -->
                <sales_order_view>Nastnet_OrderPrint_Block_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>
    <rewrite>
        <Nastnet_OrderPrint_OrderController>
            <from><![CDATA[#/\w+/sales_order/print/#]]></from>
            <to>/orderprint/order/print/</to>
        </Nastnet_OrderPrint_OrderController>
    </rewrite>
    <models>
        <Nastnet_OrderPrint>
            <class>Nastnet_OrderPrint_Model</class>
        </Nastnet_OrderPrint>
    </models>
    <pdf>
        <order>
            <default>Nastnet_OrderPrint/order_pdf_items_order_default</default>
            <grouped>Nastnet_OrderPrint/order_pdf_items_order_grouped</grouped>
        </order>
    </pdf>
</global>
<admin>
    <routers>
        <Nastnet_OrderPrint>
             <use>admin</use>
            <args>
                <module>Nastnet_OrderPrint</module>
                <!-- This is used when "catching" the rewrite above -->
                <frontName>orderprint</frontName>
            </args>
        </Nastnet_OrderPrint>
    </routers>
</admin>

Grid.php中的(app / code / local / Nastnet / OrderPrint / Block / Sales / Order /)是这样的:

class Nastnet_OrderPrint_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    protected function _prepareMassaction()
    {
        parent::_prepareMassaction();

        // Append new mass action option
        $this->getMassactionBlock()->addItem('rmaprint',
        array('label' => $this->__('Print RMA'),
              'url'   => $this->getUrl('orderprint/order/pdfRma')));
    }
}

这会导致将“打印RMA ”插入Sales&gt;的下拉菜单中所需的结果。订单网格视图屏幕。

OrderController.php文件(app / code / local / Nastnet / OrderPrint / controllers /)中,我添加了[从app {/ code / core / Mage /中pdfinvoicesAction()复制和编辑的内容Adminhtml /控制器/销售/ OrderController.php:

public function pdfRmaAction(){
    $orderIds = $this->getRequest()->getPost('order_ids');
    //print_r($orderIds);
    $flag = false;
    if (!empty($orderIds)) {
        foreach ($orderIds as $orderId) {
            $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                ->setOrderFilter($orderId)
                ->load();
                //print get_class($invoices);
                //print_r($invoices->getSize());
            if ($invoices->getSize() > 0) {
                $flag = true;
                if (!isset($pdf)){
                    $pdf = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')->getPdf(array($order));
                } else {
                    $pages = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')->getPdf(array($order));
                    $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                }
            }
        }
        if ($flag) {
            return $this->_prepareDownloadResponse(
                'rma'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
                'application/pdf'
            );
        } else {
            $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
            $this->_redirect('*/*/');
        }
    }
    $this->_redirect('*/*/');
}

这会导致实际的PDF格式错误...

Fatal error: Call to a member function getStore() on a non-object in /chroot/home/artizara/dev.artizara.com/html/app/code/local/Nastnet/OrderPrint/Model/Order/Pdf/Order.php on line 60

但是,如果您按顺序并按打印RMA 按钮(而不是尝试按质量操作打印它),那么它可以正常工作 Just Fine!

我长篇大论的解释导致了这一点: 为什么会产生这个错误?如何编辑pdfRmaAction()才能正常工作(Mass Action Printing)??? /强>

1 个答案:

答案 0 :(得分:5)

问题是您使用的$order变量未设置为函数getPdf的参数。你可以使用这个功能:

public function pdfRmaAction() {
    $orderIds = $this->getRequest()->getPost('order_ids');
    $flag = false;
    if (!empty($orderIds)) {
        foreach ($orderIds as $orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            $flag = true;
            $order->setOrder($order);
            if (!isset($pdf)) {
                $pdf = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')->getPdf(array($order));
            } else {
                $pages = Mage::getModel('Nastnet_OrderPrint/order_pdf_order')->getPdf(array($order));
                $pdf->pages = array_merge($pdf->pages, $pages->pages);
            }
        }
        if ($flag) {
            return $this->_prepareDownloadResponse(
                'rma'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
                'application/pdf'
            );
        } else {
            $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
            $this->_redirect('*/*/');
        }
    }
    $this->_redirect('*/*/');
}