Yudi
Yudi

Reputation: 191

Download Count on WooCommerce Free Product

I provide freebies using WooCommerce plugin.

I want to ask, is it possible to determine the number of downloads on the freebies on the product detail page downloads?

Upvotes: 1

Views: 1584

Answers (1)

Ratnakar - StoreApps
Ratnakar - StoreApps

Reputation: 4351

Use this code:

add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' );
function show_number_of_downloads() {
    global $wpdb, $product;
    if ( empty( $product->id ) ) return;
    if ( $product->product_type == 'variable' ) {
        $product_ids = $product->get_children();
    } else {
        $product_ids = array( $product->id );
    }
    $query = "SELECT SUM( download_count ) AS count
                    FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
                    WHERE product_id IN (".implode( ',', $product_ids ).")";
    $count = $wpdb->get_var( $query );
    if ( ! empty( $count ) ) {
        echo '<p><strong>' . __( 'Total downloads' ) . '</strong>: ' . $count . '</p>';
    }
}

Keep this code either in your theme's function.php file or any custom plugin such that you don't loose this code after upgrade of either WordPress, plugins or theme.

The result:

Download count in WooCommerce Simple Product

Download count in WooCommerce Variable Product

Upvotes: 5

Related Questions