Magento - Add Custom Mass Action PDF

為{幸葍}努か 提交于 2019-12-23 13:26:13

问题


Quick Question (Keep this in mind when reading):

Why is this error being produced (in explanation) and how can I edit the pdfRmaAction() to work correctly (Mass Action Printing)???

**Working in Magento v.1.10.1.0 which is the same as v.1.5.1.0

Long-winded explanation:

I've downloaded this extension (http://www.magentocommerce.com/magento-connect/admin-order-printing-extension.html) to add a button to each order so that when you go into the order, you have an extra button to print an RMA (Customized from this extensions Model pdf - turned into a custom RMA form based on invoice)

It works great. However, I want to add Mass Action printing to it so that you can check a few orders and choose Print RMA from the dropdown and print the forms for those orders.

In the extensions config.xml file (app/code/local/Nastnet/OrderPrint/etc/), inside the <config> tags is this:

<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>

In (app/code/local/Nastnet/OrderPrint/Block/Sales/Order/) in Grid.php is this:

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')));
    }
}

This leads to the desired result of inserting "Print RMA" into the dropdown menu on the Sales > Orders grid view screen.

In the OrderController.php file (app/code/local/Nastnet/OrderPrint/controllers/) I've added this [copied and edited from the pdfinvoicesAction() in app/code/core/Mage/Adminhtml/controllers/Sales/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('*/*/');
}

This leads to an error in the actual 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

However, if you go into the order and press the Print RMA button (instead of trying to Mass Action print it) then it works Just Fine!

My long winded explanation leads to this: Why is this error being produced and how can I edit the pdfRmaAction() to work correctly (Mass Action Printing)???


回答1:


The problem is that you use an $order variable which is not set as a parameter for the function getPdf. You should be fine with this function:

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('*/*/');
}


来源:https://stackoverflow.com/questions/9981315/magento-add-custom-mass-action-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!