Search code examples
phpwoocommercemetadatacounteradmin

Display Statistics from metadata in WooCommerce Admin Products list custom column


Based on Count Add To Cart clicks and display them in WooCommerce Admin Product list answer to my previous question, I'm trying to further modify the code to include not only clicks VS sales, but also to divide the two to get a better understanding on how the sales are going.

This is the code I can't manage to get it working:

function add_to_cart_click_counter( $product_id ) {

    $product = wc_get_product( $product_id );

    $count = (int) $product->get_meta( '_click_counter' );
    $count++;

    $product->update_meta_data( '_click_counter', $count );
    $product->save();
}

add_action( 'woocommerce_add_to_cart', 'count_add_to_cart_clicks', 90, 4 );
function count_add_to_cart_clicks( $cart_item_key, $product_id, $quantity, $variation_id = null ){

    if ( did_action( 'woocommerce_add_to_cart' ) === 1 ) {

        add_to_cart_click_counter( $product_id );

        if ( $variation_id > 0 ) {
        add_to_cart_click_counter( $variation_id );
        }
    }
}

add_filter('manage_edit-product_columns', 'product_click_count_column' );
function product_click_count_column( $columns ) {

    $columns['click_counter'] = __( 'Clicks & Sold', 'woocommerce' );
    return $columns;
}

add_action( 'manage_product_posts_custom_column', 'product_click_count_column_content', 10, 2 );
function product_click_count_column_content( $column, $post_id ) {

        global $product;
        
        if ( $column === 'click_counter' ) { 
        
            if ( $product->get_type() == 'auction' ) return;
            $sold_x_times = get_post_meta( $product->get_id(), 'total_sales', true );
            echo '<span>Sold </span>' . $sold_x_times . '</br><span>Clicks </span>' . (int) $product->get_meta( '_click_counter' );
            echo '<div style="margin-top:20px"></div>';
        
        // everything works except the line below = what I need is statistics based on clicks / sales
        // echo get_post_meta( $product->get_id(), '_click_counter' / 'total_sales', true );
    }
}

Solution

  • Replace your last function with the following revisited code function:

    add_action( 'manage_product_posts_custom_column', 'product_click_count_column_content', 10, 2 );
    function product_click_count_column_content( $column, $post_id ) {
        global $product;
    
        if ( $column === 'click_counter' && ! $product->is_type('auction') ) {
            $total_sales  = (int) $product->get_total_sales();
            $clicks_count = (int) $product->get_meta('_click_counter');
    
            printf('<span>%s</span> %d<br><span>%s</span> %d', __('Sold'), $total_sales, __('Clicks'), $clicks_count);
            
            if( $total_sales > 0 && $clicks_count > 0 ) {
                printf('<div style="margin-top:8px">%s %s<div>', __('Stats'), number_format($clicks_count / $total_sales, 2));
            }
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.