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

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

根據WooCommerce結帳中選擇的選擇字段選項隱藏COD付款

根據WooCommerce結帳中選擇的選擇字段選項隱藏COD付款

PHP
飲歌長嘯 2021-04-07 09:11:35
我正在使用WooCommerce,并且以選擇列表的形式有一個自定義結帳字段。當客戶在自定義結帳字段中選擇特定選項(在這種情況下為“ newyork”)時,我正在嘗試刪除COD網關。以下是我的實際代碼,其中我不知道如何使IF語句條件部分起作用:add_filter('woocommerce_available_payment_gateways', 'woocs_filter_gateways', 1);function woocs_filter_gateways($gateway_list){    if ( order_meta key="wc_billing_field_7378" value = newyork )    {        unset($gateway_list['cod']);    }    return $gateway_list;}如何在代碼中獲取自定義結帳字段的選定值,以使IF語句正常工作?編輯:自定義結帳字段IDwc_billing_field_7789由插件生成...
查看完整描述

1 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

已更新-處理多個不允許的目的地...


首先,為了進行測試,我在這里提供了一個掛鉤函數,該函數顯示了一個自定義結帳選擇字段,其中包含很少的選項:


// Just for testing

add_action( 'woocommerce_after_checkout_billing_form', 'custom_select_field_after_checkout_billing_form', 10, 1 );

function custom_select_field_after_checkout_billing_form ( $checkout ) {


    woocommerce_form_field( 'wc_billing_field_7378', array(

        'type'    => 'select',

        'label'   => __( "Destinations (custom select field)"),

        'class'   => array( 'form-row-wide' ),

        'options' => array(

            '' => __("Choose a destination"),

            'naeem'             => __("Naeem"),

            'saad-al-abdullah'  => __("Saad Al Abdullah"),

            'other-one'         => __("Other one"),

            'last-one'          => __("Last one"),

        ),

        'required'          => true,

    ), $checkout->get_value( 'wc_billing_field_7378' ) );

}

參見下面的顯示:

http://img1.sycdn.imooc.com//608273100001260a11000343.jpg

現在,要使此功能正常運行,需要jQuery和Ajax,以便能夠啟用或禁用“鱈魚”付款,具體取決于從此自定義結帳選擇字段中選擇的選項值。


有了這個代碼,當“納伊姆”或者選擇“另一個人”,它會隱藏“貨到付款”(COD)付款方式......如果選擇了其他選項,“貨到付款”會再次出現。


這是這段代碼:


// Jquery script that send the Ajax request

add_action( 'wp_footer', 'custom_checkout_js_script' );

function custom_checkout_js_script() {

    // Only on checkout

    if( is_checkout() && ! is_wc_endpoint_url() ) :

    ?>

    <script type="text/javascript">

    jQuery(function($){

        if (typeof wc_checkout_params === 'undefined') 

            return false;


        var field = 'select[name="wc_billing_field_7378"]';


        $( 'form.checkout' ).on('change blur', field, function() {

            $.ajax({

                type: 'POST',

                url: wc_checkout_params.ajax_url,

                data: {

                    'action': 'checkout_chosen_destination',

                    'chosen_destination': $(this).val(),

                },

                success: function (result) {

                    $(document.body).trigger('update_checkout');

                    console.log(result); // For testing only

                },

            });

        });

    });

    </script>

    <?php

    endif;

}


// The Wordpress Ajax PHP receiver

add_action( 'wp_ajax_checkout_chosen_destination', 'get_ajax_checkout_chosen_destination' );

add_action( 'wp_ajax_nopriv_checkout_chosen_destination', 'get_ajax_checkout_chosen_destination' );

function get_ajax_checkout_chosen_destination() {

    // Checking that the posted email is valid

    if ( isset($_POST['chosen_destination']) ) {


        // Set the value in a custom Woocommerce session identifier

        WC()->session->set('chosen_destination', esc_attr($_POST['chosen_destination']) );


        // Return the session value to jQuery

        echo json_encode(WC()->session->get('chosen_destination')); // For testing only

    }

    die(); // always use die at the end

}


// Show/Hide payment gateways

add_filter('woocommerce_available_payment_gateways', 'show_hide_cod_payment_method', 10, 1 );

function show_hide_cod_payment_method( $available_gateways ) {

    // Not in backend (admin)

    if( is_admin() ) 

        return $available_gateways;


    // HERE below set the not allowed destinations in the array

    $not_allowed_destinations = array('naeem', 'other-one');


    if ( in_array( WC()->session->get('chosen_destination'), $not_allowed_destinations ) ) {

        unset($available_gateways['cod']);

    }

    return $available_gateways;

}

代碼進入您的活動子主題(或活動主題)的function.php文件中。經過測試和工作。


查看完整回答
反對 回復 2021-04-23
  • 1 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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