我正在使用“ https://woocommerce.com/products/print-invoices-packing-lists/ ”上提供的 woocommerce 和“打印发票和装箱单”。为了在 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');