skip to Main Content

the task is fairly “simple”, but I am annoyed with it for whole today. In Woocommerce admin order preview, I want to change class, to the number, where quantity is greater than 1.

For this purpose, I have made following things.

  1. Loaded the script (if I open developer console, the pathway is correct):
add_action( 'admin_enqueue_scripts', 'add_my_script' );
function add_my_script() {
    wp_enqueue_script( 'jqueryforadmin', get_stylesheet_directory_uri().'/js/wp-admin.js');
}
  1. In the wp-admin.js I have added the jQuery code:
jQuery(document).ready(function($) {
    $('.wc-order-preview-table__column--quantity').filter(function(index){
    return parseInt(this.innerHTML) > 1;
}).css({'color':'blue', 'text-decoration':'underline'});
});

The problem is that the code doesn’t work. I remember, that the order preview is loaded via Ajax. Not even sure, if the code is right (because of the compatability mode I am not familiar with). Is there any way to make it work?

2

Answers


  1. Chosen as BEST ANSWER

    Finally figured it out

    1) I forgot to include jQuery as dependency as suggested by thingEvery.

    add_action( 'admin_enqueue_scripts', 'admin_jquery_script' );
    function admin_jquery_script() {
        wp_enqueue_script( 'jqueryforadmin', get_stylesheet_directory_uri().'/js/wp-admin.js', array('jquery'));
    }
    

    2) The trick was in using .ajaxComplete

    jQuery(document).ajaxComplete(function () {
        jQuery('.wc-order-preview-table__column--quantity').filter(function(index){
            return parseInt(this.innerHTML) > 1;
        }).css({'color':'red'});        
    });
    

  2. You need to include jquery as a dependency.

    wp_enqueue_script( 'jqueryforadmin', get_stylesheet_directory_uri().'/js/wp-admin.js', array( 'jquery' ) );

    wp_enqueue_script

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search