亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 結帳中的訂單總數之前添加交付單選按鈕

在 WooCommerce 結帳中的訂單總數之前添加交付單選按鈕

PHP
HUWWW 2023-06-18 18:01:30
我正在編寫一個 WordPress 插件,我需要在 WooCommerce 訂單審查部分的訂單總數之前添加兩個單選按鈕。我想出了如何將自定義單選按鈕添加到訂單審查部分,但我不知道如何在訂單總計之前移動交付選項。請查看屏幕截圖以了解我想要實現的目標。這是我的代碼:// Part 1 - Display Radio Buttonsadd_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 Totaladd_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 Sessionadd_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'] );    }}這個你能幫我嗎。
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

要在訂單總計之前移動單選按鈕,您需要使用另一個掛鉤。但是你不能在費用總計行上有那個交付單選按鈕......


我已經簡化并重新訪問了代碼:


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 文件。測試和工作。


查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 242 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號