2016-09-20 39 views
3

我正在使用woocommerce和「打印發票&包裝清單」 可在「https://woocommerce.com/products/print-invoices-packing-lists/」處獲得。 用於顯示的總儲蓄在woocommerce車&結帳頁面,我用這個代碼和完美的作品:WooCommerce - 在訂單循環中轉置購物車循環以獲得pdf發票

function bbloomer_wc_discount_total() { 

    global $woocommerce; 

    $cart_subtotal = $woocommerce->cart->cart_contents; 

    $discount_total = 0; 

    foreach ($woocommerce->cart->cart_contents as $product_data) { 

     if ($product_data['variation_id'] > 0) { 
      $product = wc_get_product($product_data['variation_id']); 
     } else { 
      $product = wc_get_product($product_data['product_id']); 
     } 

     if (!empty($product->sale_price)) { 
     $discount = ($product->regular_price - $product->sale_price) * $product_data['quantity']; 
     $discount_total += $discount; 
     } 

    } 

    if ($discount_total > 0) { 
    echo '<tr class="cart-discount"> 
    <th>'. __('Total Saving :', 'woocommerce') .'</th> 
    <td data-title=" '. __('Total Saving :', 'woocommerce') .' ">' 
    . wc_price($discount_total+$woocommerce->cart->discount_cart) .'</td> 
    </tr>'; 
    } 
} 

add_action('woocommerce_cart_totals_after_order_total', 'bbloomer_wc_discount_total'); 
add_action('woocommerce_review_order_after_order_total', 'bbloomer_wc_discount_total'); 

總部設在woocommerce車的內容,此功能。 我如何可以顯示打印發票產生的總儲蓄在HTML發票&包裝列出了插件鉤子插件(根據訂單,而不是車的內容),如:

add_action('wc_pip_after_header', 'bbloomer_wc_discount_total'); add_action('wc_pip_document_header', 'bbloomer_wc_discount_total');

回答

2

- 更新2 -(增加了對WooCommerce版本兼容性3+)

Now as I know you are using WooCommerce PDF Invoices & Packing Slips plugin.

此代碼只能直接在invoice.php PIP模板中使用剛開始時,由該更換<?php global $wpo_wcpdf ?>

<?php 

    global $wpo_wcpdf, $woocommerce; 

    // Get the order object 
    $_order = $wpo_wcpdf->export->order; 

    foreach ($_order->get_items() as $item) { 
     // Added WC 3+ compatibility 
     $quantity = method_exists($item, 'get_quantity') ? $item->get_quantity() : $item['quantity']; 
     $variation_id = method_exists($item, 'get_variation_id') ? $item->get_variation_id() : $item['variation_id']; 
     $_product = version_compare(WC_VERSION, '3.0', '<') ? wc_get_product($item['product_id']) : $item->get_product(); 

     // Get the product object when it's a product variation (Before version WC 3+) 
     if (version_compare(WC_VERSION, '3.0', '<')) 
      if ($item['variation_id'] > 0) $_product = wc_get_product($item['variation_id']); 

     // Get prices 
     $sale_price = $_product->get_sale_price(); 
     $regular_price = $_product->get_regular_price(); 

     // Only when sale price exits 
     if (! empty($sale_price) && $sale_price > 0) { 
      $discount = ($regular_price - $sale_price) * $item->get_quantity(); 
      $discount_total += $discount; 
     } 

    } 
    if ($discount_total > 0) { 
     $display_discount = '<tr class="cart-discount"> 
      <th>'. __('Total you saved :', 'woocommerce') .'</th> 
      <td data-title=" '. __('Total you saved :', 'woocommerce') .' ">' . wc_price($discount_total + $_order->get_total_discount()) .'</td> 
     </tr>'; 
    } 

?> 

然後你可以使用它,你在模板中輸出想要的話,這種方式(裏面的html):

<?php echo $display_discount; ?> 

或(內php代碼):

echo $display_discount; 

此代碼已經過測試並可正常工作。

+0

感謝您的回覆。 我已將此代碼添加到我的模板中的functions.php中,並使用Woocommerce pip插件,然後在html發票頁面中顯示:致命錯誤:調用布爾型成員函數get_items() – Mani

+0

嗨,非常感謝爲了您的迴應,我使用「WooCommerce打印發票和裝箱清單」,這是我可以下載的插件:https://www.dropbox.com/s/7mmsuh7o0tjaa45/woocommerce-pip.zip?dl=0和這是Hooks refrence:https://docs.woocommerce.com/document/woocommerce-print-invoices-packing-lists-developer-reference/。什麼代碼我可以添加到我的functions.php來顯示在Html發票總儲蓄?非常感謝你 ! – Mani

+0

非常感謝,我可以將您的代碼添加到我的functions.php中。我如何使用你的代碼?謝謝 – Mani

相關問題