在Woocommerce订单总计行中添加计算的节省总计 [英] Add calculated saving total in Woocommerce order totals rows
本文介绍了在Woocommerce订单总计行中添加计算的节省总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Woocommerce中,我正在使用以下代码来计算并在购物车和结帐页面上的订单上显示总计节省":
In Woocommerce, I am using the following code to calculate and display 'Total Saving' on the order in Cart and checkout pages:
function wc_discount_total_30() {
global $woocommerce;
$discount_total = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ( $_product->is_on_sale() ) {
$regular_price = $_product->get_regular_price();
$sale_price = $_product->get_sale_price();
$discount = ($regular_price - $sale_price) * $values['quantity'];
$discount_total += $discount;
}
}
if ( $discount_total > 0 ) {
echo '<tr class="cart-discount">
<th>'. __( 'Saved', 'tsavedis' ) .'</th>
<td data-title=" '. __( 'Saved', 'tsavedis' ) .' ">'
. wc_price( $discount_total + $woocommerce->cart->discount_cart ) .'</td>
</tr>';
}
}
// Hook our values to the Basket and Checkout pages
add_action( 'woocommerce_cart_totals_after_order_total', 'wc_discount_total_30', 99);
add_action( 'woocommerce_review_order_after_order_total', 'wc_discount_total_30', 99);
我需要在后端的订单编辑"页面中将这笔总节省显示为自定义字段.
该怎么做?
I need to display this total saving in 'Order Edit' page at backend as a custom field.
How to do that ?
推荐答案
以下是在订单总计表中添加相同内容的方法:
Here is the way to add the same thing to orders totals table:
// Display the chosen delivery information
add_filter( 'woocommerce_get_order_item_totals', 'add_saving_total_order_totals', 10, 3 );
function add_saving_total_order_totals( $total_rows, $order, $tax_display ) {;
$saving_total = 0;
// Loop through Order items
foreach($order->get_items() as $item ){
$product = $item->get_product();
if( $product->is_on_sale() ){
$regular_price = (float) $product->get_regular_price();
$active_price = (float) $product->get_price();
$saving_total += ($regular_price - $active_price) * $item->get_quantity();
}
}
if( $saving_total > 0 ) {
$discount_total = $order->get_discount_total();
$label = __( 'Saved', 'tsavedis' );
$value = wc_price( $saving_total + $discount_total );
$total_rows['saving'] = array( 'label' => $label,'value' => $value );
}
return $total_rows;
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.
Code goes in function.php file of your active child theme (or active theme). tested and works.
这篇关于在Woocommerce订单总计行中添加计算的节省总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文