Custom column in WooCommerce subscription list

|
| By Webner

Adding a custom column in WooCommerce subscription list and search data according to its content using code

There are two ways to add a custom column in the WooCommerce lists i.e. either we can add them using the WordPress plugins or by adding our custom code by extending the existing WooCommerce hooks.

So, why do we need the custom code to extend the WooCommerce functionalities?
While using the plugin, we get the limited functionalities that can be changed or extended using the plugin features. Also, the plugin will take up its own space in the database and hence increase the project’s space.

But using the custom code, you can extend the WordPress functionalities in your own way without worrying about the things you cannot extend or change.
For example, add a custom column in WooCommerce subscription list and hence search data in it according to the content in it.

We are going to add a custom column in WooCommerce subscription list (refer above image) through the custom code.

These are the steps to follow while creating a column in the Woocommerce subscription list:
1. Create a column in the subscription list
2. Populate its content from the database
3. Add styling to this column
4. Filter data according to this column content from the search box of the list.

Let’s begin to understand the code that we need to write to implement all these steps.

1. Create a column in the subscription list
You can use the WooCommerce “edit subscription column” built-in hooks to modify its actual functionality i.e.
manage_edit-shop_subscription_columns

Here I’m adding a column named “Ownership Name”, in which we are going to populate the owner name in the next step.

function wc_new_ownership_column( $columns ) {  
   $new_columns = array();
   foreach ( $columns as $column_name => $column_info ) {
       $new_columns[ $column_name ] = $column_info;
       if ( 'order_title' === $column_name ) {
           $new_columns[ownership_name] = __( 'Ownership Name', 'my-textdomain' );
       }
   }
   return $new_columns;
}
add_filter( 'manage_edit-shop_subscription_columns', 'wc_new_ownership_column' );

In this function, we are checking if the column list has the “order_title” in it. If yes, then add the new column details in the array and return that array to the HOOK.

2. Populate its content from the database
Now we are going to populate data in the column we have added in step 1 from the database by customizing WooCommerce’s manage_shop_subscription_posts_custom_column hook.

function wc_add_ownership_name_column_content( $column ) {
   global $post;
   if ( “ownership_name”  === $column ) {
        $subscription = new WC_Subscription($post->ID);
        if($meta_data = $subscription->get_meta_data()){                
            foreach($meta_data as $item_meta_data) {
                if($item_meta_data->key == "Ownership Name"){
                    $company = $item_meta_data->value;
                    break;
                }
            }
        }   
        echo $company;      
   }
     	 }
add_action('manage_shop_subscription_posts_custom_column','wc_add_ownership_name_column_content' );

These lines of code, check for the “ownership_name” column and if it exists then create a WC subscription object of that particular post (using post id) and gets its metadata. Then, loops over the whole metadata check for the “Ownership Name” column key and gets its value and then echo that value (that will be populated in front of that particular subscription entry in the new column that we have added in 1st step).

3. Add styling to this column
Now it’s time for styling this column. Though you can also add the custom CSS in one of its CSS files, the better way to use it is using this WC hooks.

	function wc_add_ownership_name_column_content_style() {
   $css = '.widefat .column-ownership_name { width: 9%; }';
   wp_add_inline_style( 'woocommerce_admin_styles', $css );
}
add_action( 'admin_print_styles', 'wc_add_ownership_name_column_content_style' );

For styling, call wp_add_inline_style WordPress function in the “admin_print_styles” WordPress hook, by passing a CSS object along with it.

4. Filter data according to this column content from the search box of the list.
After following the above steps, now it’s time to add the search functionality on this column so that the subscriptions could be searched from the search box of this table (refer attached image).

For that, we will have to add a custom query in the WC ‘parse_query’ hook.

add_action( 'parse_query','shop_ownership_search_custom_fields');
function shop_ownership_search_custom_fields( $wp ) {
    global $pagenow, $wpdb;    
    if ( 'edit.php' !== $pagenow || empty( $_GET['s'] ) || 'shop_subscription' !== $wp->query_vars['post_type'] ) {	
        /*here we are checking for the subscriptions edit page, it isn’t then do nothing and exit from this function*/
        return;
    }
    
    $search = $_GET['s'];			//getting the search phrases from the URL
    $post_status = array( $_GET['post_status'] );   
    $query = "SELECT DISTINCT posts.ID as product_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = Ownership Name' AND postmeta.meta_value LIKE  '%" .$search. "%' AND posts.post_status IN ('" . implode( "','", $post_status ) . "')";	/* custom query */
    $search_results = $wpdb->get_results( $query );
    $post_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' )) );  
     /* Executing the query and returning the result in the following code*/  
    if ( ! empty( $post_ids ) ) {
        unset( $wp->query_vars['s'] );
        $wp->query_vars['dealership_name'] = true;
        $wp->query_vars['post__in'] = $post_ids;
    }
}

In the above code, we have added a few conditions in if statement which is checking for the subscriptions edit page. If the current page isn’t this one then it will exit from the code. After that, we are fetching the search phrases from the URL which is assigned to the “s” parameter in the URL. Then a custom query is written which will be getting the arguments dynamically from the URL, replacing it in the query, executing the query and finally returning the result to the HOOK.

So, by adding all these steps in the function.php file you will get a custom column in the WC subscription table and you can populate content in it. You can also search the data from the table based on this new column.

One comment

  1. This is really helpful and I’m thankful that it’s focused on Subscriptions and not just Woocommerce. I’m really challenged here, though. I cannot get the content to populate. I’m trying to pull in subscription Ship-to address, but the column is blank. Any thoughts?

Leave a Reply

Your email address will not be published. Required fields are marked *