简体   繁体   中英

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?

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM