下载依靠 WooCommerce 免费产品 [英] Download Count on WooCommerce Free Product

查看:21
本文介绍了下载依靠 WooCommerce 免费产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WooCommerce 插件提供免费赠品.

I provide freebies using WooCommerce plugin.

我想问一下,商品详情页下载的freebies可以确定下载次数吗?

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

推荐答案

使用此代码:

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>';
    }
}

将此代码保留在您主题的 function.php 文件或任何自定义插件中,这样您就不会在升级 WordPress、插件或主题后丢失此代码.

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.

结果:

这篇关于下载依靠 WooCommerce 免费产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆