我在Woocommerce/Wordpress上工作,我是新来的,我想通过在其中添加使用过的优惠券来定制我的Woocommerce订单列表管理面板,但我在点击promo字段进行排序时遇到了一个小问题,它返回一个空结果。
在我的功能中。php
function sv_wc_cogs_add_order_promo_column_header($columns)
{
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if (\'order_total\' === $column_name) {
$new_columns[\'order_promo\'] = __(\'Promo\', \'my-textdomain\');
}
}
return $new_columns;
}
add_filter(\'manage_edit-shop_order_columns\', \'sv_wc_cogs_add_order_promo_column_header\', 20);
/**
* Adds \'Profit\' column content to \'Orders\' page immediately after \'Total\' column.
*
* @param string[] $column name of column being displayed
*/
add_action( \'manage_shop_order_posts_custom_column\', \'sv_wc_cogs_add_order_promo_column_content\',20 ,4 );
function sv_wc_cogs_add_order_promo_column_content( $column, $post ) {
global $post;
if ( \'order_promo\' === $column ) {
$order = wc_get_order( $post->ID );
$coupons = $order->get_used_coupons();
foreach($coupons as $coupon){
echo $coupon .\'<br>\';
}
}
}
add_filter( "manage_edit-shop_order_sortable_columns", \'custom_woo_admin_sort\' );
function custom_woo_admin_sort( $columns )
{
$custom = array(
\'order_promo\' => \'_order_promo\',
);
$sortable_columns[ \'_order_promo\' ] = \'order_promo\';
return wp_parse_args( $custom, $columns );
}
// Here is the problem cause the sort is not good ! It sort with strange value
add_action(\'pre_get_posts\', \'custom_promocode_orderby\');
function custom_promocode_orderby( $query ) {
if ( !is_admin() ){ return; }
$orderby = $query->get(\'orderby\');
if (\'_order_promo\' == $orderby){
$meta_query = array(
\'relation\' => \'OR\',
array(
\'key\' => \'id\',
\'compare\' => \'NOT EXISTS\',
),
array(
\'key\' => \'_order_promo\',
),
);
$query->set(\'order\', \'ASC\' );
$query->set(\'orderby\', $meta_query);
}
}
非常感谢您阅读我的问题和您未来的答案。我不能在周末测试答案,因为我没有这个项目。
抱歉,如果我的代码不是一个好的做法!我是wordpress的noob。。。