在 woocommerce 产品管理页面中使自定义列可排序

时间:2021-05-17 18:31:13

标签: wordpress woocommerce

我有一个名为“状态”的自定义列,其中包含一些值,我希望能够进行排序。 我似乎无法弄清楚我做错了什么。有什么帮助吗?

我正在运行 wp 5.7 和 wc 5.2.2。

<?php

defined('ABSPATH') or die('Something wrong');

if (! class_exists('Product_Status')) :

class Product_Status {

    function __construct() {
        add_filter( 'manage_product_posts_columns', array($this, 'set_custom_column'));
        add_action( 'manage_product_posts_custom_column', array($this, 'populate_status'));
        add_filter('manage_edit-product_sortable_columns', array($this, 'set_sortable_columns'));
        add_action( 'pre_get_posts', array($this, 'set_orderby') );
    }

 
    function set_custom_column( $columns_array ) {
        $columns_array['product_status'] = 'Status';
        return $columns_array;
    }

    function set_sortable_columns( $columns_array ) {
        $columns_array['product_status'] = 'status';
        return $columns_array;
    }

    function set_orderby( $query ) {
        $orderby = $query->get( 'orderby');
    
        if( 'status' == $orderby ) {
            $query->set('meta_key','product_status');
            $query->set('orderby','meta_value');
        }
    }
    
    function populate_status( $column_name ) {
        if( $column_name  == 'product_status' ) {
            $product_id = get_the_ID();
            $price_offers = get_post_meta($product_id, '_' . 'wcj_price_offers', true);
            if (!empty($price_offers)) {
                $all_status = array_column($price_offers, 'status');
                $accepted_email = array_values(array_diff(array_column($price_offers, 'current_email'), array('')))[0];
                if (in_array('Waiting', $all_status)) {
                    echo '<div style="display:inline-block;width:10px;height:10px;border-radius:100%;background-color:yellow;"></div> Waiting';
                } elseif (in_array('Accepted', $all_status)) {
                    echo $accepted_email;
                } else {
                    echo '<div style="display:inline-block;width:10px;height:10px;border-radius:100%;background-color:red;"></div> No offer';
                }
            } else {
                echo '<div style="display:inline-block;width:10px;height:10px;border-radius:100%;background-color:red;"></div> No offer';
            }
        }
    }
}

endif;
return new Product_Status();

无论是按日期排序还是单击“状态”时都不会返回任何内容。 提前致谢。

0 个答案:

没有答案