我正在写一个WordPress插件,在其中我需要在WooCommerce订单审查部分的订单总数之前添加两个单选按钮。我想出了如何在订单查看部分添加自定义单选按钮,但是我无法理解如何在订单总数之前移动交货选项。
请检查屏幕截图以了解我到底要实现什么。
这是我的代码:
// Part 1
// Display Radio Buttons
add_action( 'woocommerce_review_order_before_payment', 'bbloomer_checkout_radio_choice' );
function bbloomer_checkout_radio_choice() {
$chosen = WC()->session->get( 'radio_chosen' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'2.95' => '60 MINUTES: €2.95',
'0' => '24 - 48 HOURS',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h3>Delivery Options</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
// Part 2
// Add Fee and Calculate Total
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_checkout_radio_choice_fee', 20, 1 );
function bbloomer_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'radio_chosen' );
if ( $radio ) {
$cart->add_fee( 'Delivery Fee', $radio );
}
}
// Part 3
// Add Radio Choice to Session
add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_checkout_radio_choice_set_session' );
function bbloomer_checkout_radio_choice_set_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['radio_choice'] ) ){
WC()->session->set( 'radio_chosen', $output['radio_choice'] );
}
}
这个你能帮我吗。
提前致谢。
要在总订单前移动单选按钮,您将需要使用另一个挂钩。但是您不能在费用总额行上有该传递单选按钮…
我简化并重新审视了代码:
add_action( 'woocommerce_review_order_before_order_total', 'checkout_delivery_radio_buttons' );
function checkout_delivery_radio_buttons() {
echo '<tr class="delivery-radio">
<th>'.__("Delivery Options").'</th><td>';
$chosen = WC()->session->get( 'delivery' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'delivery' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
woocommerce_form_field( 'delivery', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'2.95' => '60 MINUTES: €2.95',
'0' => '24 - 48 HOURS',
),
), $chosen );
echo '</td></tr>';
}
add_action( 'woocommerce_cart_calculate_fees', 'checkout_delivery_fee', 20, 1 );
function checkout_delivery_fee( $cart ) {
if ( $radio = WC()->session->get( 'delivery' ) ) {
$cart->add_fee( 'Delivery Fee', $radio );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'checkout_delivery_choice_to_session' );
function checkout_delivery_choice_to_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['delivery'] ) ){
WC()->session->set( 'delivery', $output['delivery'] );
}
}
代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句