在 WooCommerce 结帐中的订单总数之前添加交付单选按钮

我正在编写一个 WordPress 插件,我需要在 WooCommerce 订单审查部分的订单总数之前添加两个单选按钮。我想出了如何将自定义单选按钮添加到订单审查部分,但我不知道如何在订单总计之前移动交付选项。

请查看屏幕截图以了解我想要实现的目标。

http://img1.mukewang.com/648ed60f0001f23817190735.jpg

这是我的代码:


// Part 1 - Display Radio Buttons

add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_radio_choice' );

function custom_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', 'custom_checkout_radio_choice_fee', 20, 1 );

function custom_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', 'custom_checkout_radio_choice_set_session' );

function custom_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'] );

    }

}

这个你能帮我吗。


HUWWW
浏览 180回答 1
1回答

慕妹3242003

要在订单总计之前移动单选按钮,您需要使用另一个挂钩。但是你不能在费用总计行上有那个交付单选按钮......我已经简化并重新访问了代码:add_action( 'woocommerce_review_order_before_order_total', 'checkout_delivery_radio_buttons' );function checkout_delivery_radio_buttons() {&nbsp; &nbsp; echo '<tr class="delivery-radio">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>'.__("Delivery Options").'</th><td>';&nbsp; &nbsp; $chosen = WC()->session->get( 'delivery' );&nbsp; &nbsp; $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'delivery' ) : $chosen;&nbsp; &nbsp; $chosen = empty( $chosen ) ? '0' : $chosen;&nbsp; &nbsp; woocommerce_form_field( 'delivery',&nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; => 'radio',&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp;=> array( 'form-row-wide', 'update_totals_on_change' ),&nbsp; &nbsp; &nbsp; &nbsp; 'options'&nbsp; &nbsp;=> array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2.95'&nbsp; => '60 MINUTES: €2.95',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0'&nbsp; &nbsp; &nbsp;=> '24 - 48 HOURS',&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; ), $chosen );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo '</td></tr>';}add_action( 'woocommerce_cart_calculate_fees', 'checkout_delivery_fee', 20, 1 );function checkout_delivery_fee( $cart ) {&nbsp; &nbsp; if ( $radio = WC()->session->get( 'delivery' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart->add_fee( 'Delivery Fee', $radio );&nbsp; &nbsp; }}add_action( 'woocommerce_checkout_update_order_review', 'checkout_delivery_choice_to_session' );function checkout_delivery_choice_to_session( $posted_data ) {&nbsp; &nbsp; parse_str( $posted_data, $output );&nbsp; &nbsp; if ( isset( $output['delivery'] ) ){&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->set( 'delivery', $output['delivery'] );&nbsp; &nbsp; }}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP