根据WooCommerce结帐中选择的选择字段选项隐藏COD付款 [英] Hide COD payment based on chosen select field options in WooCommerce checkout

查看:108
本文介绍了根据WooCommerce结帐中选择的选择字段选项隐藏COD付款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WooCommerce,并且我有一个选择列表形式的自定义结帐字段.当客户在自定义结帐字段中选择特定选项(在这种情况下为"newyork")时,我正在尝试删除COD网关.

I am using WooCommerce and I have a custom checkout field in form of a selection list. I am trying to remove COD gateway, when customer select on a custom checkout field a specific option ("newyork" in this case).

以下是我的实际代码,其中我不知道如何使 IF 语句条件部分起作用:

Here below is my actual code where I don't know how to make the IF statement condition part working:


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语句正常工作?

How can I get the selected value of my custom checkout field In my code, to get my IF statement working?

自定义结帐字段ID是由插件生成的 wc_billing_field_7789 ...

The custom checkout field id is wc_billing_field_7789 generated by a plugin...

推荐答案

已更新-处理多个不允许的目的地…

首先,为了进行测试,我这里是一个挂钩函数,该函数显示了一个自定义结帐选择字段,其中包含几个选项:

First, for testing I here is a hooked function that displays a custom checkout select field with few options:

// 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,以便能够启用或禁用鳕鱼"付款,具体取决于从此自定义结帐选择字段中选择的选项值.

Now to get this working jQuery and Ajax are required, to be able to make "Cod" payment enabled or disabled depending on the selected option value from this custom checkout select field.

使用此代码选择"Naeem"或其他"时,将隐藏货到付款"(货到付款)付款方式...如果选择了其他选项,则货到付款"将再次显示

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于根据WooCommerce结帐中选择的选择字段选项隐藏COD付款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆