WooCommerce - 在 pdf 发票的订单循环中转置购物车循环

WooCommerce - Transpose Cart loop in Order loop for pdf invoices

我正在使用 woocommerce 并且 "Print Invoices & Packing lists" 可在“https://woocommerce.com/products/print-invoices-packing-lists/”获得。 为了在 woocommerce 购物车和结帐页面中显示总节省,我使用了这段代码并且效果很好:

 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 — (添加了 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;

此代码已经过测试并且有效。