根据WooCommerce结帐中选择的选择字段选项隐藏COD付款

我正在使用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由插件生成...


饮歌长啸
浏览 153回答 1
1回答

小怪兽爱吃肉

已更新-处理多个不允许的目的地...首先,为了进行测试,我在这里提供了一个挂钩函数,该函数显示了一个自定义结帐选择字段,其中包含很少的选项:// Just for testingadd_action( 'woocommerce_after_checkout_billing_form', 'custom_select_field_after_checkout_billing_form', 10, 1 );function custom_select_field_after_checkout_billing_form ( $checkout ) {&nbsp; &nbsp; woocommerce_form_field( 'wc_billing_field_7378', array(&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; => 'select',&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp;=> __( "Destinations (custom select field)"),&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp;=> array( 'form-row-wide' ),&nbsp; &nbsp; &nbsp; &nbsp; 'options' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '' => __("Choose a destination"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'naeem'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> __("Naeem"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'saad-al-abdullah'&nbsp; => __("Saad Al Abdullah"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'other-one'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> __("Other one"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'last-one'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => __("Last one"),&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'required'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => true,&nbsp; &nbsp; ), $checkout->get_value( 'wc_billing_field_7378' ) );}参见下面的显示:现在,要使此功能正常运行,需要jQuery和Ajax,以便能够启用或禁用“鳕鱼”付款,具体取决于从此自定义结帐选择字段中选择的选项值。有了这个代码,当“纳伊姆”或者选择“另一个人”,它会隐藏“货到付款”(COD)付款方式......如果选择了其他选项,“货到付款”会再次出现。这是这段代码:// Jquery script that send the Ajax requestadd_action( 'wp_footer', 'custom_checkout_js_script' );function custom_checkout_js_script() {&nbsp; &nbsp; // Only on checkout&nbsp; &nbsp; if( is_checkout() && ! is_wc_endpoint_url() ) :&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; jQuery(function($){&nbsp; &nbsp; &nbsp; &nbsp; if (typeof wc_checkout_params === 'undefined')&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; var field = 'select[name="wc_billing_field_7378"]';&nbsp; &nbsp; &nbsp; &nbsp; $( 'form.checkout' ).on('change blur', field, function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: wc_checkout_params.ajax_url,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'action': 'checkout_chosen_destination',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'chosen_destination': $(this).val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document.body).trigger('update_checkout');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(result); // For testing only&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php&nbsp; &nbsp; endif;}// The Wordpress Ajax PHP receiveradd_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() {&nbsp; &nbsp; // Checking that the posted email is valid&nbsp; &nbsp; if ( isset($_POST['chosen_destination']) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Set the value in a custom Woocommerce session identifier&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->set('chosen_destination', esc_attr($_POST['chosen_destination']) );&nbsp; &nbsp; &nbsp; &nbsp; // Return the session value to jQuery&nbsp; &nbsp; &nbsp; &nbsp; echo json_encode(WC()->session->get('chosen_destination')); // For testing only&nbsp; &nbsp; }&nbsp; &nbsp; die(); // always use die at the end}// Show/Hide payment gatewaysadd_filter('woocommerce_available_payment_gateways', 'show_hide_cod_payment_method', 10, 1 );function show_hide_cod_payment_method( $available_gateways ) {&nbsp; &nbsp; // Not in backend (admin)&nbsp; &nbsp; if( is_admin() )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $available_gateways;&nbsp; &nbsp; // HERE below set the not allowed destinations in the array&nbsp; &nbsp; $not_allowed_destinations = array('naeem', 'other-one');&nbsp; &nbsp; if ( in_array( WC()->session->get('chosen_destination'), $not_allowed_destinations ) ) {&nbsp; &nbsp; &nbsp; &nbsp; unset($available_gateways['cod']);&nbsp; &nbsp; }&nbsp; &nbsp; return $available_gateways;}代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP