dongni3854 2014-03-12 18:01
浏览 66

WooCommerce,在PDF发票中添加自定义字段

I am new to woocommerce and my client requirement is that Plugin "PDF Invoice" should also display product description in generated PDF file.

Here Is My Code.

/**
* Add SKU to PDF order heading
*/
add_filter ( 'pdf_template_table_headings' , 'custom_pdf_table_headers' );

function custom_pdf_table_headers() {

    $headers =  '<table class="shop_table orderdetails" width="100%">' . 
                '<thead>' .
                '<tr><th colspan="7" align="left"><h2>' . esc_html__('Order Details', PDFLANGUAGE) . '</h2></th></tr>' .
                '<tr>' .
                '<th width="5%" valign="top" align="right">'  . __( 'Qty', PDFLANGUAGE )        . '</th>' .                
                '<th width="30%" valign="top" align="left">'  . __( 'Product', PDFLANGUAGE )    . '</th>' .
                '<th width="20%" valign="top" align="right">' . __( 'Description', PDFLANGUAGE )        . '</th>' .    
                '<th width="9%" valign="top" align="right">'  . __( 'Price Ex', PDFLANGUAGE )   . '</th>' .
                '<th width="9%" valign="top" align="right">'  . __( 'Total Ex.', PDFLANGUAGE )  . '</th>' .
                '<th width="7%" valign="top" align="right">'  . __( 'Tax', PDFLANGUAGE )        . '</th>' .
                '<th width="10%" valign="top" align="right">' . __( 'Price Inc', PDFLANGUAGE )  . '</th>' .
                '<th width="10%" valign="top" align="right">' . __( 'Total Inc', PDFLANGUAGE )  . '</th>' .
                '</tr>' .
                '</thead>' .
                '</table>';

    return $headers;

}

/**
 * Add SKU to order line
 */
add_filter ( 'pdf_template_line_output' , 'custom_pdf_lines', 10, 2 );

function custom_pdf_lines ( $line, $order_id ) {
    global $woocommerce;
    $order   = new WC_Order( $order_id );

    $pdflines  = '<table width="100%">';
    $pdflines .= '<tbody>';

    if ( sizeof( $order->get_items() ) > 0 ) : 

        foreach ( $order->get_items() as $item ) {

            if ( $item['qty'] ) :

                $line = '';
                $item_loop++;

                $_product   = $order->get_product_from_item( $item );
                $item_name  = $item['name'];
                $item_meta  = new WC_Order_Item_Meta( $item['item_meta'] );

                if ( $meta = $item_meta->display( true, true ) )
                    $item_name .= ' ( ' . $meta . ' )';

                $line =     '<tr>' .
                            '<td valign="top" width="5%" align="right">' . $item['qty'] . ' x</td>' .
                            '<td valign="top" width="40%">' .  $item_name . '</td>' .
                            '<td valign="top" width="10%">' .  $_product->get_sku() . '</td>' .
                            '<td valign="top" width="9%" align="right">' .  woocommerce_price( $item['line_subtotal'] ) . '</td>' . 
                            '<td valign="top" width="9%" align="right">' .  woocommerce_price( $item['qty']*$item['line_subtotal'] ) . '</td>' .    
                            '<td valign="top" width="7%" align="right">' .  woocommerce_price( $item['line_tax'] ) . '</td>' .          
                            '<td valign="top" width="10%" align="right">' .  woocommerce_price( $item['line_subtotal'] + $item['line_tax'] ) . '</td>' .
                            '<td valign="top" width="10%" align="right">' .  woocommerce_price( $item['qty']*($item['line_subtotal'] + $item['line_tax']) ) . '</td>' .
                            '</tr>' ;

                $pdflines .= $line;
            endif;
        }

    endif;

    $pdflines .=    '</tbody>';
    $pdflines .=    '</table>';

    return $pdflines;
}

I want to add Description detail in PDF. which variable should I pass ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Oracle触发器记录修改前后的字段值
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器