簡體   English   中英

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

[英]Hide COD payment based on chosen select field options in WooCommerce checkout

我正在使用 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 語句正常工作?


編輯:

自定義結帳字段 ID 是由插件生成的wc_billing_field_7789 ...

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

首先,為了測試,我這里有一個鈎子函數,它顯示一個帶有幾個選項的自定義結帳選擇字段:

// 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' ) );
}

見下方顯示:

在此處輸入圖像描述


現在要使這個工作正常,需要 jQuery 和 Ajax,以便能夠根據從這個自定義結帳選擇字段中選擇的選項值啟用或禁用“Cod”支付。

With this code when "Naeem" or "Other one" are be selected, it will hide "Cash on delivery" (cod) payment method... If another option is selected, "Cash on delivery" will be visible again.

這是這段代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM