You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

登录状态下WordPress/WooCommerce将已购商品的添加购物车按钮替换为下载按钮

问题描述

我正在搭建一个售卖3D模型等数字产品的WooCommerce网站,希望实现以下功能:当已登录客户进入其已购买的产品页面时,页面显示下载按钮而非添加购物车按钮。我曾尝试将以下代码添加到functions.php中,但未取得预期效果:

function wc_custom_replace_add_to_cart_button( $button, $product ) {
if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && $product->is_downloadable() ) {
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {
$button_text = __("Download", "woocommerce");
$button = '' . $button_text . '';
}
}
return $button;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'wc_custom_replace_add_to_cart_button', 10, 2 );
解决方案

你的代码存在两个核心问题:

  • 钩子使用错误:woocommerce_loop_add_to_cart_link仅作用于商品列表页的按钮,而你需要修改的是单个产品详情页的按钮,需用对应钩子替换。
  • 下载按钮无有效链接:仅替换文本未关联用户的专属下载地址,导致按钮无法使用。

以下是修正后的代码,添加到主题的functions.php文件即可实现需求:

// 替换已购买数字产品详情页的添加购物车按钮为下载按钮
function wc_replace_add_to_cart_with_download_button() {
    global $product;

    // 仅针对已登录用户、可下载简单产品、且用户已购买该产品
    if ( is_user_logged_in() && $product->is_type('simple') && $product->is_downloadable() ) {
        $current_user = wp_get_current_user();
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {
            // 隐藏原添加购物车按钮
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            
            // 获取用户的可用下载链接
            $downloads = wc_get_customer_available_downloads( $current_user->ID );
            foreach ( $downloads as $download ) {
                if ( $download['product_id'] == $product->get_id() ) {
                    // 输出带有效链接的下载按钮
                    echo '<a href="' . esc_url( $download['download_url'] ) . '" class="button alt">' . __( '下载产品', 'woocommerce' ) . '</a>';
                    break;
                }
            }
        }
    }
}
add_action( 'woocommerce_single_product_summary', 'wc_replace_add_to_cart_with_download_button', 25 );

代码说明

  1. 先校验用户登录状态、产品类型、购买记录三个核心条件
  2. 符合条件时,移除原添加购物车按钮的渲染动作
  3. 通过wc_get_customer_available_downloads获取用户的专属下载链接,匹配当前产品后输出可点击的下载按钮
  4. 优先级设为25,确保在原添加购物车按钮(优先级30)执行前完成替换

如果需要同时修改商品列表页的按钮,可以使用以下修正后的代码:

// 修改商品列表页中已购买数字产品的添加购物车按钮为下载按钮
function wc_custom_replace_loop_add_to_cart_button( $button, $product ) {
    if ( is_user_logged_in() && $product->is_type('simple') && $product->is_downloadable() ) {
        $current_user = wp_get_current_user();
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) {
            // 获取用户的可用下载链接
            $downloads = wc_get_customer_available_downloads( $current_user->ID );
            foreach ( $downloads as $download ) {
                if ( $download['product_id'] == $product->get_id() ) {
                    $button = '<a href="' . esc_url( $download['download_url'] ) . '" class="button alt">' . __( '下载', 'woocommerce' ) . '</a>';
                    break;
                }
            }
        }
    }
    return $button;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'wc_custom_replace_loop_add_to_cart_button', 10, 2 );

内容的提问来源于stack exchange,提问作者Teodor Radu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 15:37:02