Make columns sortable in admin products list panel in Woocommerce Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!WooCommerce: Add new column in products list view and make it sortableWordpress Search Serialized Meta Data with Custom QueryWordpress - Fatal error: Call to a member function get_var() on a non-object $wpdbHow to convert a row value to column header in mysql for Wordpress databaseHow to add / remove columns in Woocommerce admin product listSetting Tax Query in pre_get_posts still showing products with the wrong taxonomyWooCommerce: Moving column position in admin product listWordpress/MySQL -> get post ids together with a count of total posts written by an author of a given postAdd custom column product visibility to admin product list in Woocommerce 3Add a custom “Sale Price” column to admin products list in WoocommerceWooCommerce: Add new column in products list view and make it sortable

Why does BitLocker not use RSA?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

Weaponising the Grasp-at-a-Distance spell

Marquee sign letters

Is a copyright notice with a non-existent name be invalid?

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

Can gravitational waves pass through a black hole?

How does TikZ render an arc?

Why not use the yoke to control yaw, as well as pitch and roll?

Keep at all times, the minus sign above aligned with minus sign below

Is this Half dragon Quaggoth Balanced

Derived column in a data extension

An isoperimetric-type inequality inside a cube

In musical terms, what properties are varied by the human voice to produce different words / syllables?

How to name indistinguishable henchmen in a screenplay?

How to ask rejected full-time candidates to apply to teach individual courses?

Short story about astronauts fertilizing soil with their own bodies

What criticisms of Wittgenstein's philosophy of language have been offered?

How to achieve cat-like agility?

How do Java 8 default methods hеlp with lambdas?

Diophantine equation 3^a+1=3^b+5^c

Table formatting with tabularx?

Statistical analysis applied to methods coming out of Machine Learning

Where did Ptolemy compare the Earth to the distance of fixed stars?



Make columns sortable in admin products list panel in Woocommerce



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!WooCommerce: Add new column in products list view and make it sortableWordpress Search Serialized Meta Data with Custom QueryWordpress - Fatal error: Call to a member function get_var() on a non-object $wpdbHow to convert a row value to column header in mysql for Wordpress databaseHow to add / remove columns in Woocommerce admin product listSetting Tax Query in pre_get_posts still showing products with the wrong taxonomyWooCommerce: Moving column position in admin product listWordpress/MySQL -> get post ids together with a count of total posts written by an author of a given postAdd custom column product visibility to admin product list in Woocommerce 3Add a custom “Sale Price” column to admin products list in WoocommerceWooCommerce: Add new column in products list view and make it sortable



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I added the column with the highest value of the offer. And then I would like to make it sortable. I've prepared this:



// asl this will add extra column in the product list



add_filter( 'manage_edit-product_columns', array($this,'show_product_offers_amounts'),15 );
add_action( 'manage_product_posts_custom_column', array($this,'show_product_offers_amount_max'), 10, 2 );


// asl show the column



function show_product_offers_amounts($columns)
$columns['orig_offer_amount'] = 'Amount';
return $columns;



// asl add the datas to column



function show_product_offers_amount_max( $column, $postid ) 
global $wpdb;
if ( $column == 'orig_offer_amount' )
$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
echo '<mark style="color: #3973aa;font-size: 13px;font-weight: 500;background: 0 0;line-height: 1;">'.$offers_max.'</mark>';

else
echo '<span class="na">–</span>';






// asl register the column as sortable



function price_column_register_sortable( $columns ) 
$columns['orig_offer_amount'] = 'orig_offer_amount';

return $columns;

add_filter( 'manage_edit-product_sortable_columns', 'price_column_register_sortable' );
function price_column_orderby( $vars )
if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] )

$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
$offers_max = $offers_max;

else
$offers_max = 0;;



$vars = array_merge( $vars, array(
'meta_key' => 'orig_offer_amount',
'orderby' => $offers_max
) );


return $vars;

add_filter( 'request', 'price_column_orderby' );


This code makes that wordpress recognize the column as sortable. But it doesn't sort properly.



Any idea?



Using LoicTheAztec suggestion I prepared something like this:



add_action( 'save_post', 'new_postmeta_to_products' );
function new_postmeta_to_products($post_id)
$post_type = get_post_type($post_id);

if($post_type == 'products')

global $wpdb;
$maxId = $wpdb->get_var( " select post_id
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=".$post_id.")
order by meta_value desc limit 1"
);

add_post_meta($post_id,'offer_max_id',$maxId);




But it doesn't work :( maybe because $post_id










share|improve this question
























  • To get this sortable, you should register the product offers amount max as product meta data value… Without that you will not be able to make it sortable. Also this will make your code much more lighter as your code actually is generating a lot of requests to database.

    – LoicTheAztec
    Feb 2 at 0:49











  • That's you again. You must be good :))) You have probably right. I'll think about how to do this.

    – yoomla
    Feb 14 at 9:54











  • I prepare some snippet and I would like to show you. But I can't put it to comment. How do this in a proper way?

    – yoomla
    Feb 14 at 15:15











  • I edited my question and I added it at the end.

    – yoomla
    Feb 14 at 15:24

















1















I added the column with the highest value of the offer. And then I would like to make it sortable. I've prepared this:



// asl this will add extra column in the product list



add_filter( 'manage_edit-product_columns', array($this,'show_product_offers_amounts'),15 );
add_action( 'manage_product_posts_custom_column', array($this,'show_product_offers_amount_max'), 10, 2 );


// asl show the column



function show_product_offers_amounts($columns)
$columns['orig_offer_amount'] = 'Amount';
return $columns;



// asl add the datas to column



function show_product_offers_amount_max( $column, $postid ) 
global $wpdb;
if ( $column == 'orig_offer_amount' )
$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
echo '<mark style="color: #3973aa;font-size: 13px;font-weight: 500;background: 0 0;line-height: 1;">'.$offers_max.'</mark>';

else
echo '<span class="na">–</span>';






// asl register the column as sortable



function price_column_register_sortable( $columns ) 
$columns['orig_offer_amount'] = 'orig_offer_amount';

return $columns;

add_filter( 'manage_edit-product_sortable_columns', 'price_column_register_sortable' );
function price_column_orderby( $vars )
if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] )

$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
$offers_max = $offers_max;

else
$offers_max = 0;;



$vars = array_merge( $vars, array(
'meta_key' => 'orig_offer_amount',
'orderby' => $offers_max
) );


return $vars;

add_filter( 'request', 'price_column_orderby' );


This code makes that wordpress recognize the column as sortable. But it doesn't sort properly.



Any idea?



Using LoicTheAztec suggestion I prepared something like this:



add_action( 'save_post', 'new_postmeta_to_products' );
function new_postmeta_to_products($post_id)
$post_type = get_post_type($post_id);

if($post_type == 'products')

global $wpdb;
$maxId = $wpdb->get_var( " select post_id
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=".$post_id.")
order by meta_value desc limit 1"
);

add_post_meta($post_id,'offer_max_id',$maxId);




But it doesn't work :( maybe because $post_id










share|improve this question
























  • To get this sortable, you should register the product offers amount max as product meta data value… Without that you will not be able to make it sortable. Also this will make your code much more lighter as your code actually is generating a lot of requests to database.

    – LoicTheAztec
    Feb 2 at 0:49











  • That's you again. You must be good :))) You have probably right. I'll think about how to do this.

    – yoomla
    Feb 14 at 9:54











  • I prepare some snippet and I would like to show you. But I can't put it to comment. How do this in a proper way?

    – yoomla
    Feb 14 at 15:15











  • I edited my question and I added it at the end.

    – yoomla
    Feb 14 at 15:24













1












1








1








I added the column with the highest value of the offer. And then I would like to make it sortable. I've prepared this:



// asl this will add extra column in the product list



add_filter( 'manage_edit-product_columns', array($this,'show_product_offers_amounts'),15 );
add_action( 'manage_product_posts_custom_column', array($this,'show_product_offers_amount_max'), 10, 2 );


// asl show the column



function show_product_offers_amounts($columns)
$columns['orig_offer_amount'] = 'Amount';
return $columns;



// asl add the datas to column



function show_product_offers_amount_max( $column, $postid ) 
global $wpdb;
if ( $column == 'orig_offer_amount' )
$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
echo '<mark style="color: #3973aa;font-size: 13px;font-weight: 500;background: 0 0;line-height: 1;">'.$offers_max.'</mark>';

else
echo '<span class="na">–</span>';






// asl register the column as sortable



function price_column_register_sortable( $columns ) 
$columns['orig_offer_amount'] = 'orig_offer_amount';

return $columns;

add_filter( 'manage_edit-product_sortable_columns', 'price_column_register_sortable' );
function price_column_orderby( $vars )
if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] )

$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
$offers_max = $offers_max;

else
$offers_max = 0;;



$vars = array_merge( $vars, array(
'meta_key' => 'orig_offer_amount',
'orderby' => $offers_max
) );


return $vars;

add_filter( 'request', 'price_column_orderby' );


This code makes that wordpress recognize the column as sortable. But it doesn't sort properly.



Any idea?



Using LoicTheAztec suggestion I prepared something like this:



add_action( 'save_post', 'new_postmeta_to_products' );
function new_postmeta_to_products($post_id)
$post_type = get_post_type($post_id);

if($post_type == 'products')

global $wpdb;
$maxId = $wpdb->get_var( " select post_id
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=".$post_id.")
order by meta_value desc limit 1"
);

add_post_meta($post_id,'offer_max_id',$maxId);




But it doesn't work :( maybe because $post_id










share|improve this question
















I added the column with the highest value of the offer. And then I would like to make it sortable. I've prepared this:



// asl this will add extra column in the product list



add_filter( 'manage_edit-product_columns', array($this,'show_product_offers_amounts'),15 );
add_action( 'manage_product_posts_custom_column', array($this,'show_product_offers_amount_max'), 10, 2 );


// asl show the column



function show_product_offers_amounts($columns)
$columns['orig_offer_amount'] = 'Amount';
return $columns;



// asl add the datas to column



function show_product_offers_amount_max( $column, $postid ) 
global $wpdb;
if ( $column == 'orig_offer_amount' )
$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
echo '<mark style="color: #3973aa;font-size: 13px;font-weight: 500;background: 0 0;line-height: 1;">'.$offers_max.'</mark>';

else
echo '<span class="na">–</span>';






// asl register the column as sortable



function price_column_register_sortable( $columns ) 
$columns['orig_offer_amount'] = 'orig_offer_amount';

return $columns;

add_filter( 'manage_edit-product_sortable_columns', 'price_column_register_sortable' );
function price_column_orderby( $vars )
if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] )

$offers_max = $wpdb->get_var( " select max(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=".$postid.")"
);

if($offers_max > 0)
$offers_max = $offers_max;

else
$offers_max = 0;;



$vars = array_merge( $vars, array(
'meta_key' => 'orig_offer_amount',
'orderby' => $offers_max
) );


return $vars;

add_filter( 'request', 'price_column_orderby' );


This code makes that wordpress recognize the column as sortable. But it doesn't sort properly.



Any idea?



Using LoicTheAztec suggestion I prepared something like this:



add_action( 'save_post', 'new_postmeta_to_products' );
function new_postmeta_to_products($post_id)
$post_type = get_post_type($post_id);

if($post_type == 'products')

global $wpdb;
$maxId = $wpdb->get_var( " select post_id
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=".$post_id.")
order by meta_value desc limit 1"
);

add_post_meta($post_id,'offer_max_id',$maxId);




But it doesn't work :( maybe because $post_id







php wordpress sorting woocommerce product






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 12:47









LoicTheAztec

98.4k1472114




98.4k1472114










asked Jan 30 at 12:56









yoomlayoomla

337




337












  • To get this sortable, you should register the product offers amount max as product meta data value… Without that you will not be able to make it sortable. Also this will make your code much more lighter as your code actually is generating a lot of requests to database.

    – LoicTheAztec
    Feb 2 at 0:49











  • That's you again. You must be good :))) You have probably right. I'll think about how to do this.

    – yoomla
    Feb 14 at 9:54











  • I prepare some snippet and I would like to show you. But I can't put it to comment. How do this in a proper way?

    – yoomla
    Feb 14 at 15:15











  • I edited my question and I added it at the end.

    – yoomla
    Feb 14 at 15:24

















  • To get this sortable, you should register the product offers amount max as product meta data value… Without that you will not be able to make it sortable. Also this will make your code much more lighter as your code actually is generating a lot of requests to database.

    – LoicTheAztec
    Feb 2 at 0:49











  • That's you again. You must be good :))) You have probably right. I'll think about how to do this.

    – yoomla
    Feb 14 at 9:54











  • I prepare some snippet and I would like to show you. But I can't put it to comment. How do this in a proper way?

    – yoomla
    Feb 14 at 15:15











  • I edited my question and I added it at the end.

    – yoomla
    Feb 14 at 15:24
















To get this sortable, you should register the product offers amount max as product meta data value… Without that you will not be able to make it sortable. Also this will make your code much more lighter as your code actually is generating a lot of requests to database.

– LoicTheAztec
Feb 2 at 0:49





To get this sortable, you should register the product offers amount max as product meta data value… Without that you will not be able to make it sortable. Also this will make your code much more lighter as your code actually is generating a lot of requests to database.

– LoicTheAztec
Feb 2 at 0:49













That's you again. You must be good :))) You have probably right. I'll think about how to do this.

– yoomla
Feb 14 at 9:54





That's you again. You must be good :))) You have probably right. I'll think about how to do this.

– yoomla
Feb 14 at 9:54













I prepare some snippet and I would like to show you. But I can't put it to comment. How do this in a proper way?

– yoomla
Feb 14 at 15:15





I prepare some snippet and I would like to show you. But I can't put it to comment. How do this in a proper way?

– yoomla
Feb 14 at 15:15













I edited my question and I added it at the end.

– yoomla
Feb 14 at 15:24





I edited my question and I added it at the end.

– yoomla
Feb 14 at 15:24












1 Answer
1






active

oldest

votes


















1














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?






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54441145%2fmake-columns-sortable-in-admin-products-list-panel-in-woocommerce%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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?






    share|improve this answer





























      1














      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?






      share|improve this answer



























        1












        1








        1







        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?






        share|improve this answer















        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?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 20 at 19:06

























        answered Feb 20 at 19:00









        yoomlayoomla

        337




        337





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54441145%2fmake-columns-sortable-in-admin-products-list-panel-in-woocommerce%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현