2016-09-20 8 views
3

私はwoocommerceを使用しています。 "https://woocommerce.com/products/print-invoices-packing-lists/"で入手可能な&梱包リスト&梱包リストを使用しています。WooCommerce - Transpose Cartループを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カートの内容に基づいて、この機能:表示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

ご回答いただきありがとうございます。 私のテンプレートのmy functions.phpにこのコードが追加され、Woocommerceピッププラグインを使用してHTML請求書ページに表示されます:致命的なエラー:ブール値のget_items()を呼び出すメンバー関数への呼び出し – Mani

+0

こんにちは、ありがとうあなたの回答では、私は「WooCommerce Print Invoices&Packing Lists」を使用しています。これは私がダウンロードできるプラグインです: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

関連する問題