Mời anh chị theo dõi video:
Hook filter:manage_{tên post type}_posts_columns được dùng việc này
Code chi tiết:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
add_filter( 'manage_product_posts_columns', 'product_columns_2' ); function product_columns_2( $existing_columns ) { if ( empty( $existing_columns ) && ! is_array( $existing_columns ) ) { $existing_columns = array(); } unset( $existing_columns['title'], $existing_columns['comments'], $existing_columns['date'], $existing_columns['product_tag'] ); $columns = array(); $columns['ordering'] = 'Thứ tự<a href="#" class="saveorderingdddd"><span class="dashicons dashicons-filter"></span></a>'; return array_merge( $columns, $existing_columns ); } |
Sử dụng hook action có tên:manage_{tên post type}_posts_custom_column
Code chi tiết như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//show ordering add_action( 'manage_product_posts_custom_column', 'render_product_columns_2' , 3); function render_product_columns_2( $column ) { global $post, $the_product; if ( empty( $the_product ) || $the_product->id != $post->ID ) { $the_product = wc_get_product( $post ); } switch ( $column ) { case 'ordering' : ?> <input type="text" style="width:50px; text-align:center;" value="<?php echo get_post_meta($post->ID,'_product_ordering',true); ?>" name="ordering[]" /> <?php break; default : break; } } |
Code chi tiết như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// Display Fields add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); // Save Fields add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); function woo_add_custom_general_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Text Field woocommerce_wp_text_input( array( 'id' => '_product_ordering', 'label' => __( 'Thứ tự', 'woocommerce' ), 'placeholder' => '', 'desc_tip' => 'true', 'description' => __( '', 'woocommerce' ) , //'value' => 0 ) ); echo '</div>'; } function woo_add_custom_general_fields_save( $post_id ){ // Text Field $woocommerce_text_field = $_POST['_product_ordering']; if( !empty( $woocommerce_text_field ) ) update_post_meta( $post_id, '_product_ordering', esc_attr( $woocommerce_text_field ) ); } |
Đăng ký file js với admin backend
1 2 3 4 5 6 |
function my_enqueue($hook) { wp_enqueue_script( 'smn-ordering', get_template_directory_uri() . '/js/ordering.js' ); wp_localize_script( 'smn-ordering', 'ordering_ob', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ))); } add_action( 'admin_enqueue_scripts', 'my_enqueue' ); |
File xử lý js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
jQuery(function($){ //alert('sdfsd;'); $(".saveorderingdddd").click(function(){ var post=document.getElementsByName('post[]'); var post_ids =''; for(key=0; key < post.length; key++) { if(key == 0){ post_ids =post[key].value; }else{ post_ids = post_ids+","+post[key].value; } //your code goes here } var order=document.getElementsByName('ordering[]'); var orderings =''; for(key=0; key < order.length; key++) { if(key == 0){ orderings =order[key].value; }else{ orderings = orderings +","+order[key].value; } //your code goes here } $.ajax({ type: 'POST', url: ordering_ob.ajaxurl, data:{action: 'ordering',post_ids:post_ids,orderings:orderings}, success: function(data){ console.log(data); if(data == "data"){ location.reload(); } } }); return false; }); }); |
Hook action và function xử lý ajax
1 2 3 4 5 6 7 8 9 10 11 |
add_action('wp_ajax_ordering','ordering_function'); add_action('wp_ajax_nopriv_ordering','ordering_function'); function ordering_function(){ $post_ids = explode(",",$_POST['post_ids']); $orderings = explode(",",$_POST['orderings']); for($i=0;$i<count($post_ids);$i++): update_post_meta($post_ids[$i], "_product_ordering", $orderings[$i]); endfor; echo "data"; die(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//resort list product add_filter( 'parse_query', 'fb_custom_post_sort' ); function fb_custom_post_sort($query) { if ( ! is_admin() ) return $query; global $current_screen; if ( isset( $current_screen ) && 'product' === $current_screen->post_type ) { $query->query_vars['orderby'] = 'meta_value_num'; $query->query_vars['meta_key'] = '_product_ordering'; $query->query_vars['order'] = 'ASC'; } } |
1 2 3 4 5 6 7 8 9 10 |
$args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'meta_key' => '_product_ordering', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'post_status' => 'publish' ); //var_dump($args); $wp_query = new WP_Query( $args ); |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress