Download Count on WooCommerce Free Product

后端 未结 1 1889
生来不讨喜
生来不讨喜 2021-01-07 08:49

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?

相关标签:
1条回答
  • 2021-01-07 09:13

    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:

    0 讨论(0)
提交回复
热议问题