Vishal Chodvadiya
Vishal Chodvadiya

Reputation: 39

Direct product Download in Woocommerce and Count Downloadable producta Woocommerce

I am building Woocommerce WordPress Site. I am adding direct download product functionality. I added in my site by using following code`

        <?php
        foreach( $downloads as $key => $each_download ) {
            $actions = array(
                'download'  => array(
                    'url'  => $each_download["file"],
                    'name' => __( 'Download', 'demo' )
                    )
                );
        }

        if ( $actions = apply_filters( 'woocommerce_account_download_actions', $actions, $download ) ) {
            foreach ( $actions as $key => $action ) {
                echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-theme btn-rounded btn-theme-lg btn-buy-template woocommerce-Button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
            }
            //var_dump(apply_filters( 'woocommerce_account_download_actions', $actions, $download ));
        }
        ?>  `

I am getting Download link,but I can't get counter of Downloadable product Would you like to tell me how can i Update My WordPress Downloadable product table.

Upvotes: 0

Views: 1618

Answers (1)

Dannie Herdyawan
Dannie Herdyawan

Reputation: 11

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, so that you don't lose this code after an upgrade of either WordPress, plugins or theme.

source: https://stackoverflow.com/a/39383269/6574214

Upvotes: 1

Related Questions