uj5u.com熱心網友回復:
既然你不想把它添加到總數中,你可以在woocommerce-checkout-review-order-table中添加一個自定義表行,而不是購物車費用。所以我的答案不是基于WooCommerce費用,而是完全獨立于它。
自定義表格行將顯示/隱藏百分比,基于是否選中復選框。
通過單行注釋進行解釋,添加到我的答案中。
所以你得到:
//Add checkbox field。
function action_woocommerce_before_order_notes( $checkout ) {
//span>添加欄位。
woocommerce_form_field( 'my_id', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide' ) 。
'label' => __( '15% and some other text', 'woocommerce' ) 。
'required' => false,
), $checkout->get_value( 'my_id' )) 。
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 ) 。
//保存復選框的值。
function action_woocommerce_checkout_create_order( $order, $data ) {
//設定正確的值。
$checkbox_value = isset( $_POST['my_id'] ) ?'yes' : 'no';
//更新元資料
$order->update_meta_data( '_my_checkbox_value', $checkbox_value ) 。
}
add_action( 'woocommerce_checkout_create_order'/span>, 'action_woocommerce_checkout_create_order'/span>, 10, 2 ) 。
//在結賬頁上添加表行。
function action_woocommerce_before_order_total() /span>{
//初始化。
$percent = 15;
//獲得小計& 運費總額
$subtotal = WC()->cart->subtotal;
$shipping_total = WC()->cart->get_shipping_total()。
// Total
$total = $subtotal $shipping_total。
// result
$result = ( $total / 100 ) * $percent。
//輸出
echo '<tr class="my-class" >
<th>' . __( 'My text', 'woocommerce' ) . '</th>
$result ) . wc_price( $result ) . '</td>
</tr>'。
}
add_action( 'woocommerce_review_order_before_order_total', 'action_woocommerce_before_order_total', 10, 0 ) 。
//使用jQuery在結賬頁面顯示/隱藏表行。
function action_wp_footer() /span>{
//僅在結賬時使用。
if ( is_checkout() && ! is_wc_endpoint_url() ) :
? >
<script type="text/javascript">
jQuery( function($){
//選擇器。
var my_input = 'input[name=my_id]'/span>。
var my_class = '.my-class';
//span>顯示或隱藏
function show_or_hide() {
if ( $( my_input ).is(':checked') ) {
return $( my_class ).show()。
} else {
return $( my_class ).hide()。
}
}
//默認
$( document ).ajaxComplete(function() {
show_or_hide()。
});
// On change
$( 'form.checkout' ).change(function() {
show_or_hide()。
});
});
</script>
<?php?
endif;
}
add_action( 'wp_footer'/span>, 'action_wp_footer'/span>, 10, 0 ) 。
//如果需要,添加新的表行到電子郵件,訂單收到(感謝頁面)& 我的賬戶-> 查看訂單。
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
//獲得復選框的值。
$checkbox_value = $order->get_meta( '_my_checkbox_value' ) 。
//不等于yes,回傳。
if ( $checkbox_value != 'yes'/span> ) return $total_rows;
//初始化
$percent = 15;
//獲得小計& 運費總額
$subtotal = $order-> get_subtotal();
$shipping_total = $order-> get_shipping_total();
// Total
$total = $subtotal $shipping_total。
// result
$result = ( $total / 100 ) * $percent。
//保存要重新排序的值。
$order_total = $total_rows['order_total'/span>]。
//移除要重新排序的專案。
unset( $total_rows['order_total'] ) 。
//增加新的行。
$total_rows['my_text'] = array(
'label' => __( 'My text:', 'woocommerce' ) 。
'value' => wc_price( $result ) 。
);
//按正確的順序重新插入洗掉的內容。
$total_rows['order_total'] = $order_total。
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 ) 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/307826.html
標籤:

