user1276919
user1276919

Reputation: 558

How to add checkbox column in WooCommerce product listing in WordPress admin panel to update meta value with ajax

I have to add an extra column with checkbox to WordPress/WooCommerce product listing in the admin panel.

Its purpose is to quickly update extra meta parameter of the product without entering quick edit.

I added a checkbox column, but I have a problem with placing the column before the featured star column as on the attached image. How to update meta with ajax in such case?

enter image description here

Current code:

add_action( 'manage_product_posts_custom_column', 'print_extra_columns', 15, 3 );
add_filter( 'manage_product_posts_columns', 'add_extra_columns', 15 );



function print_extra_columns( $value, $column_name )
{ 
   if(  $value  == "extra" ) {
        $checkbox ='<input type="checkbox" name="extra" />';
        echo  $checkbox;
}


}

function add_extra_columns( $defaults )
{
    $defaults['extra'] = 'Extra';
    return $defaults;
}

Upvotes: 2

Views: 1702

Answers (1)

Misha Rudrastyh
Misha Rudrastyh

Reputation: 883

Actually this task can be implemented in 4 steps:

  1. Add a column with manage_edit-product_columns (You did it!)
  2. Populate column with a checkbox using manage_posts_custom_column (You did it!)
  3. jQuery/JavaScript code to send async request to wp_ajax_ action hook (To do!)
  4. Process the AJAX request and save the value to post meta (To do!)

By the way, you can find the complete code for your task in this tutorial: https://rudrastyh.com/woocommerce/columns.html#checkbox_column_with_ajax

Upvotes: 1

Related Questions