向Magento发票添加订单评论

时间:2014-07-23 05:17:26

标签: magento invoices

我一直在努力获取客户评论,以及订单上的礼品消息,以便在发票上打印,但到目前为止还没有运气。

我已经尝试过一些我几乎找不到的建议,但没有任何建议。应该有一个简单的方法来做到这一点,但它让我感到困惑,我似乎无法找到解决方案

我也看过许多扩展,但似乎没有这个选项

我正在使用magento 1.7

我也希望这些也会在确认电子邮件中显示......任何建议或想法都会被学徒用

这是我的invoice.php(复制到本地结构)

class Mage_Sales_Model_Order_Pdf_Invoice extends 

Mage_Sales_Model_Order_Pdf_Abstract
{

    /***** Begin order comment modification *****/
protected function _getCustomerOrderComments($invoice)
    {
        $order = $invoice->getOrder();
        if($orderComments = $order->getBiebersdorfCustomerordercomment()){
            return $orderComments;
        } else {
            return false;
        }
    }
 /***** End order comment modification *****/


    /**
     * Draw header for item table
     *
     * @param Zend_Pdf_Page $page
     * @return void
     */
    protected function _drawHeader(Zend_Pdf_Page $page)
    {
        /* Add table head */
        $this->_setFontRegular($page, 10);
        $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
        $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
        $page->setLineWidth(0.5);
        $page->drawRectangle(25, $this->y, 570, $this->y -15);
        $this->y -= 10;
        $page->setFillColor(new Zend_Pdf_Color_RGB(0, 0, 0));

        //columns headers
        $lines[0][] = array(
            'text' => Mage::helper('sales')->__('Products'),
            'feed' => 35
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('SKU'),
            'feed'  => 290,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Qty'),
            'feed'  => 435,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Price'),
            'feed'  => 360,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Tax'),
            'feed'  => 495,
            'align' => 'right'
        );

        $lines[0][] = array(
            'text'  => Mage::helper('sales')->__('Subtotal'),
            'feed'  => 565,
            'align' => 'right'
        );

        $lineBlock = array(
            'lines'  => $lines,
            'height' => 5
        );

        $this->drawLineBlocks($page, array($lineBlock), array('table_header' => true));
        $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
        $this->y -= 20;
    }

    /**
     * Return PDF document
     *
     * @param  array $invoices
     * @return Zend_Pdf
     */
    public function getPdf($invoices = array())
    {
        $this->_beforeGetPdf();
        $this->_initRenderer('invoice');

        $pdf = new Zend_Pdf();
        $this->_setPdf($pdf);
        $style = new Zend_Pdf_Style();
        $this->_setFontBold($style, 10);

        foreach ($invoices as $invoice) {
            if ($invoice->getStoreId()) {
                Mage::app()->getLocale()->emulate($invoice->getStoreId());
                Mage::app()->setCurrentStore($invoice->getStoreId());
            }
            $page  = $this->newPage();
            $order = $invoice->getOrder();
            /* Add image */
            $this->insertLogo($page, $invoice->getStore());
            /* Add address */
            $this->insertAddress($page, $invoice->getStore());
            /* Add head */
            $this->insertOrder(
                $page,
                $order,
                Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())
            );
            /* Add document text and number */
            $this->insertDocumentNumber(
                $page,
                Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId()
            );
            /* Add table */
            $this->_drawHeader($page);
            /* Add body */
            foreach ($invoice->getAllItems() as $item){
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }
                /* Draw item */
                $this->_drawItem($item, $page, $order);
                $page = end($pdf->pages);
            }
            /* Add totals */
            $this->insertTotals($page, $invoice);
            if ($invoice->getStoreId()) {
                Mage::app()->getLocale()->revert();
            }
        }


        $message = Mage::getModel('giftmessage/message');

        $gift_message_id = $invoice->getOrder()->getGiftMessageId();
        if(!is_null($gift_message_id)) {
              $message->load((int)$gift_message_id);
              $gift_sender = $message->getData('sender');
              $gift_recipient = $message->getData('recipient');
              $gift_message = $message->getData('message');
              $page->drawText(Mage::helper('sales')->__('Message from:'), 35, $this->y, 'UTF-8');
             $page->drawText(Mage::helper('sales')->__($gift_sender), 120, $this->y, 'UTF-8');
              $this->y -=10;
              $page->drawText(Mage::helper('sales')->__('Message to:'), 35, $this->y, 'UTF-8');
              $page->drawText(Mage::helper('sales')->__($gift_recipient), 120, $this->y, 'UTF-8');
              $this->y -=20;
              $page->drawText(Mage::helper('sales')->__('Message:'), 35, $this->y, 'UTF-8');
              $page->drawText(Mage::helper('sales')->__($gift_message), 120, $this->y, 'UTF-8');
          }

       /***** Begin order comment modification *****/
        if($this->_getCustomerOrderComments($invoice)){
            $this->_setFontBold($page);
            $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
            $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
            $page->setLineWidth(0.5);
            $page->drawRectangle(25, $this->y, 570, $this->y-15);
            $this->y -=10;
            $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
            $page->drawText('Order comments:', 35, $this->y, 'UTF-8');
            $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
            $this->y -=20;

            $comments = $this->_getCustomerOrderComments($invoice)."\n";

            //wrap
            $comments = wordwrap($comments,118,"\n",false);
            $token = strtok($comments, "\n");

            while ($token != false) {
                $page->drawText($token, 35, $this->y, 'UTF-8');
                $this->y-=15;
                $token = strtok("\n");
            }

            $this->y -=30;
            $this->_setFontRegular($page);
        }
        /***** end order comment modification *****/


        $this->_afterGetPdf();
        return $pdf;
    }

    /**
     * Create new page and assign to PDF object
     *
     * @param  array $settings
     * @return Zend_Pdf_Page
     */
    public function newPage(array $settings = array())
    {
        /* Add new table head */
        $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4);
        $this->_getPdf()->pages[] = $page;
        $this->y = 800;
        if (!empty($settings['table_header'])) {
            $this->_drawHeader($page);
        }
        return $page;
    }
}

0 个答案:

没有答案