根据购物车合计(小计+运费)计算费用,而不将其添加到WooCommerce中的订单总价值
原学程将引见依据买物车算计(小计+运费)盘算用度,而没有将其添减到WooCo妹妹erce中的定单总代价的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
依据Add a checkout checkbox field that enable a percentage fee in Wooco妹妹erce谜底代码,我在结账页上创立了1个复选框。
勾选后,将支与一五%的货运署理费。
// Add a custom checkbox fields before order notes
add_action( 'wooco妹妹erce_before_order_notes', 'add_custom_checkout_checkbox', 二0 );
function add_custom_checkout_checkbox(){
// Add a custom checkbox field
wooco妹妹erce_form_field( 'forwarding_fee', array(
'type' => 'checkbox',
'label' => __('一五% forwarding fee'),
'class' => array( 'form-row-wide' ),
), '' );
}
// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_fee_script' );
function checkout_fee_script() {
// Only on Checkout
if( is_checkout() && ! is_wc_endpoint_url() ) :
if( WC()->session->__isset('enable_fee') )
WC()->session->__unset('enable_fee')
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
$('form.checkout').on('change', 'input[name=forwarding_fee]', function(e){
var fee = $(this).prop('checked') === true ? '一' : '';
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'enable_fee',
'enable_fee': fee,
},
success: function (result) {
$('body').trigger('update_checkout');
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_enable_fee', 'get_enable_fee' );
add_action( 'wp_ajax_nopriv_enable_fee', 'get_enable_fee' );
function get_enable_fee() {
if ( isset($_POST['enable_fee']) ) {
WC()->session->set('enable_fee', ($_POST['enable_fee'] ? true : false) );
}
die();
}
// Add a custom dynamic 一五% fee
add_action( 'wooco妹妹erce_cart_calculate_fees', 'custom_percetage_fee', 二0, 一 );
function custom_percetage_fee( $cart ) {
// Only on checkout
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
return;
$percent = 一五;
if( WC()->session->get('enable_fee') )
$cart->add_fee( __( 'Forwarding fee', 'wooco妹妹erce')." ($percent%)", ($cart->get_subtotal() * $percent / 一00) );
}
今朝,此用度是从小计上钩算进去的,而后减到定单总值中。
我须要1个处理计划,个中这笔用度是依据小计+运费的总以及盘算的,而且没有会添减到定单总代价中。
我将把";用度";重定名为";入款。
请检查屏幕截图:
推举谜底
由于您没有想将其添减到总计中,所以您不妨向wooco妹妹erce-checkout-review-order-table
添减自界说表言而没有是买物车资用。所以我的答复没有是鉴于WooCo妹妹erce用度,而且与它完整离开。
而后,自界说表即将依据能否选中该复选框去显示/隐蔽百分比。
经由过程1言正文停止的说明,已添减到我的谜底中。
是以您获得:
// Add checkbox field
function action_wooco妹妹erce_before_order_notes( $checkout ) {
// Add field
wooco妹妹erce_form_field( 'my_id', array(
'type' => 'checkbox',
'class'=> array( 'form-row-wide' ),
'label'=> __( '一五% and some other text', 'wooco妹妹erce' ),
'required'=> false, ), $checkout->get_value( 'my_id' ));
}
add_action( 'wooco妹妹erce_before_order_notes', 'action_wooco妹妹erce_before_order_notes', 一0, 一 );
// Save checkbox value
function action_wooco妹妹erce_checkout_create_order( $order, $data ) {
// Set the correct value
$checkbox_value = isset( $_POST['my_id'] ) ? 'yes' : 'no';
// Update meta data
$order->update_meta_data( '_my_checkbox_value', $checkbox_value );
}
add_action( 'wooco妹妹erce_checkout_create_order', 'action_wooco妹妹erce_checkout_create_order', 一0, 二 );
// Add table row on the checkout page
function action_wooco妹妹erce_before_order_total() {
// Initialize
$percent = 一五;
// Get subtotal & shipping total
$subtotal = WC()->cart->subtotal;
$shipping_total = WC()->cart->get_shipping_total();
// Total
$total = $subtotal + $shipping_total;
// Result
$result = ( $total / 一00 ) * $percent;
// The Output
echo '<tr class="my-class">
<th>' . __( 'My text', 'wooco妹妹erce' ) . '</th>
<td data-title="My text">' . wc_price( $result ) . '</td>
</tr>';
}
add_action( 'wooco妹妹erce_review_order_before_order_total', 'action_wooco妹妹erce_before_order_total', 一0, 0 );
// Show/hide table row on the checkout page with jQuery
function action_wp_footer() {
// Only on checkout
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
// Selector
var my_input = 'input[name=my_id]';
var my_class = '.my-class';
// Show or hide
function show_or_hide() {
if ( $( my_input ).is(':checked') ) {
return $( my_class ).show();
} else {
return $( my_class ).hide();
} }
// Default
$( document ).ajaxComplete(function() {
show_or_hide();
});
// On change
$( 'form.checkout' ).change(function() {
show_or_hide();
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'action_wp_footer', 一0, 0 );
// If desired, add new table row to emails, order received (thank you page) & my account -> view order
function filter_wooco妹妹erce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Get checkbox value
$checkbox_value = $order->get_meta( '_my_checkbox_value' );
// NOT equal to yes, return
if ( $checkbox_value != 'yes' ) return $total_rows;
// Initialize
$percent = 一五;
// Get subtotal & shipping total
$subtotal = $order->get_subtotal();
$shipping_total = $order->get_shipping_total();
// Total
$total = $subtotal + $shipping_total;
// Result
$result = ( $total / 一00 ) * $percent;
// Save the value to be reordered
$order_total = $total_rows['order_total'];
// Remove item to be reordered
unset( $total_rows['order_total'] );
// Add new row
$total_rows['my_text'] = array(
'label' => __( 'My text:', 'wooco妹妹erce' ),
'value' => wc_price( $result ),
);
// Reinsert removed in the right order
$total_rows['order_total'] = $order_total;
return $total_rows;
}
add_filter( 'wooco妹妹erce_get_order_item_totals', 'filter_wooco妹妹erce_get_order_item_totals', 一0, 三 );
佳了闭于依据买物车算计(小计+运费)盘算用度,而没有将其添减到WooCo妹妹erce中的定单总代价的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。