3

I'm using woocommerce and "Print Invoices & Packing lists" available at "https://woocommerce.com/products/print-invoices-packing-lists/" . For Display the Total Saving in the woocommerce cart & checkout pages I'm used this code and works perfectly :

 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');

This function based in the woocommerce cart contents . How I can Display The Total Saving in the Html Invoice generated By Print Invoices & Packing lists Plugin ( based On the Order Instead of the cart contents ) in the plugin Hooks like :

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

1 Answer 1

2

— Update 2 — (Added compatibility for WooCommerce version 3+)

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

This code can only be used directly in invoice.php PIP template at the beginning, replacing <?php global $wpo_wcpdf ?> by this:

<?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>';
    }

?>

Then you can use it where you want in the template to output it, this way (inside html):

<?php echo $display_discount; ?>

Or (inside php code):

echo $display_discount;

This code is tested and works.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks to your response . I have been added this code to my functions.php in the my template , and using the Woocommerce pip plugin , then in the html invoice page displays : Fatal error: Call to a member function get_items() on boolean
Hi Thanks alot for your response , I'm using "WooCommerce Print Invoices & Packing Lists" , and this is my plugin you can download : dropbox.com/s/7mmsuh7o0tjaa45/woocommerce-pip.zip?dl=0 and this is the Hooks refrence : docs.woocommerce.com/document/… . what code i can add to my functions.php to displays Total Saving in the Html Invoices ? thank you very much !
Thanks a lot , can i add your code to my functions.php . How I can using of your code ? Thank you
Thanks , But i don't using the "WooCommerce PDF Invoices & Packing Slips" plugin . I am using "Print Invoices & Packing lists" available at "woocommerce.com/products/print-invoices-packing-lists" and my plugin don't have this function : $wpo_wcpdf . How i can use your code with "Print Invoices & Packing lists" Plugin ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.