dongtang5057 2019-04-14 21:09
浏览 82
已采纳

根据WooCommerce结帐中选择的选择字段选项隐藏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).

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;
}

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


Edit:

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

  • 写回答

1条回答 默认 最新

  • dongxing7530 2019-04-14 22:55
    关注

    Updated - Handling multiple not allowed destinations…

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

    See below the display:

    enter image description here


    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.

    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.

    Here is this code:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?