$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Show special field when correct shipping is chosen|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Show special field when correct shipping is chosen

matteradmin11PV0评论

I tried this approach:

add_action( 'woocommerce_review_order_before_payment', array( $this, 'my_custom_checkout_field') );
add_action('updated_checkout',array( $this, 'my_custom_checkout_field'));

public function my_custom_checkout_field(){

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];
    if($chosen_shipping == 'zasilkovna'){
        include_once('special-field.php');
    }
}

However, the special field does not appear when user clicks the shipping method. User needs to manually refresh the page, which is not good.

I need to hook myself on the ajax and show the field when correct shipping is clicked.

I know the jqery is firing the action, but am not quite sure how to hook on it...

I tried this approach:

add_action( 'woocommerce_review_order_before_payment', array( $this, 'my_custom_checkout_field') );
add_action('updated_checkout',array( $this, 'my_custom_checkout_field'));

public function my_custom_checkout_field(){

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];
    if($chosen_shipping == 'zasilkovna'){
        include_once('special-field.php');
    }
}

However, the special field does not appear when user clicks the shipping method. User needs to manually refresh the page, which is not good.

I need to hook myself on the ajax and show the field when correct shipping is clicked.

I know the jqery is firing the action, but am not quite sure how to hook on it...

Share Improve this question asked Jan 24, 2019 at 17:47 Pavel JanicekPavel Janicek 2123 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1 +50

You need to setup the change event on the field:

$('select.shipping-field').on('change', function()) {
   $.post(
       ajaxurl, 
       {
           action: 'shipping_special',
           data: 'your data here..'
       }, 
       function(response){
           // The response should contain your special field HTML
       }
   );
});

You can pass the current shipping select option

Then your theme's functions.php file needs to have the action to return your special field, if the shipping is zasilkovna

add_action('wp_ajax_shipping_special', 'shipping_special_field' );
add_action('wp_ajax_nopriv_shipping_special', 'shipping_special_field' );

function shipping_special_field() {
    // Return your special field HTML here
}

See wp_ajax__requestaction, this tutorial from WPMUDev or this SO answer;

Post a comment

comment list (0)

  1. No comments so far