Make columns sortable in admin products list panel in Woocommerce

烂漫一生 提交于 2019-12-02 05:35:31

Thanks to LoicTheAztec's hints I've prepared some snippet. First I've added new postmeta for product, when is created:

add_post_meta($post_id, 'offers_max_id', null);
add_post_meta($post_id, 'offers_max_value', null);
add_post_meta($post_id, 'offers_count', null);

Then, a few lines to set the new postmeta when an offer is created in www/wp-content/plugins/offers-for-woocommerce/public/class-offers-for-woocommerce.php in function new_offer_form_submit() :

global $post;
$_product = get_post_meta($parent_post_id, 'offer_product_id', true);

global $wpdb;
$offerMax = $wpdb->get_results( " select post_id, meta_value
                        from ".$wpdb->postmeta."
                        where meta_key='orig_offer_amount'
                            and meta_value!=''
                            and post_id in(
                                select p.post_id
                                from ".$wpdb->postmeta." as p
                                where p.meta_key='orig_offer_product_id' and
                                    p.meta_value=".$_product.")
                            order by meta_value desc limit 1" 
                   ,ARRAY_N);    
update_post_meta($_product, 'offers_max_id', $offerMax[0][0]);
update_post_meta($_product, 'offers_max_value', $offerMax[0][1]);

 $offerCount = $wpdb->get_var( "select count(post_id) 
                                 from ".$wpdb->postmeta." 
                                 where meta_key='orig_offer_product_id' and
                                     meta_value=".$_product
                             );
 update_post_meta($_product, 'offers_count', $offerCount);

Then, we must add new columns for products' admin panel in function.php:

// asl show the column
function my_cpt_columns( $columns ) {
    $columns["offermaxid"] = "OfferId";
    $columns["offercount"] = "Offers";
    $columns["offermaxvalue"] = "Amount";
    $columns["dateofend"] = "EndTime";
    return $columns;
}
add_filter('manage_edit-product_columns', 'my_cpt_columns');

// asl take a data to the column
function my_cpt_column( $colname, $cptid ) {
     if ( $colname == 'offermaxid')
          echo get_post_meta( $cptid, 'offers_max_id', true );
     if ( $colname == 'offercount')
          echo get_post_meta( $cptid, 'offers_count', true );       
     if ( $colname == 'offermaxvalue')
          echo get_post_meta( $cptid, 'offers_max_value', true );        
     if ( $colname == 'dateofend')
          echo date("j M y H:i:s", (get_post_meta( $cptid, '_sale_price_dates_to', true )+60*60) );
     if ( $colname == 'offerlefttime') {
          $atime = time();
          $d = (get_post_meta( $cptid, '_sale_price_dates_to', true ) - $atime)/(60*60*24);
          $h = ( get_post_meta( $cptid, '_sale_price_dates_to', true ) - $atime - ( (60*60*24) * explode(".", $d)[0] ) )/(60*60);
          $m = ( get_post_meta( $cptid, '_sale_price_dates_to', true ) - $atime - ( (60*60*24) * explode(".", $d)[0] ) - ( (60*60) * explode(".", $h)[0] ) )/(60);
          echo floor($d) . "d " . floor($h) . "h " . floor($m) . "m";
     }
}
add_action('manage_product_posts_custom_column', 'my_cpt_column', 10, 2);

// asl sortowanie
add_filter('manage_edit-product_sortable_columns', 'my_cpt_columns');
function my_sort_metabox( $vars ) {
      if( array_key_exists('orderby', $vars )) {
           if('OfferId' == $vars['orderby']) {
                $vars['orderby'] = 'meta_value';
                $vars['meta_key'] = 'offers_max_id';
           }
           if('Offers' == $vars['orderby']) {
                $vars['orderby'] = 'meta_value';
                $vars['meta_key'] = 'offers_count';
           }      
           if('Amount' == $vars['orderby']) {
                $vars['orderby'] = 'meta_value';
                $vars['meta_key'] = 'offers_max_value';
           }              
          if('EndTime' == $vars['orderby']) {
                $vars['order'] = 'ASC';
                $vars['orderby'] = 'meta_value';
                $vars['meta_key'] = '_sale_price_dates_to';
           }    
      }
      return $vars;
}
add_filter('request', 'my_sort_metabox');

// asl dodanie kolumny wynikowej z czasem do końca
function my_cpt_column_time( $columns ) {
    $columns["offerlefttime"] = "LeftTime";
    return $columns;
}
add_filter('manage_edit-product_columns', 'my_cpt_column_time');

And everything nice but one thing doesn't work. I want to have the products sort by column 'EndTime' ASC. Any suggestions?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!