2016-09-20 38 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

相关问题