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

WooCommerce如何配置可下载商品与外部联盟购买按钮共存

WooCommerce外部/联盟商品兼容可下载属性实现方案

WooCommerce默认对商品类型做了属性互斥,外部/联盟商品默认不开放可下载配置入口,通过以下两种方案都可以实现「同商品售卖可下载PDF电子书+展示平装本外部购买跳转按钮」的需求,不需要修改WooCommerce核心文件。

方案一:代码片段实现(推荐,无额外插件开销)

将以下代码插入当前主题的functions.php文件,或通过代码片段管理插件插入,避免主题更新后配置丢失。

  • 第一步:解除类型互斥,给外部商品开放可下载配置入口
// 为外部/联盟商品类型开放可下载属性选项
add_filter('product_type_options', function($options) {
    $options['downloadable']['wrapper_class'] = str_replace(
        'show_if_simple show_if_variable',
        'show_if_simple show_if_variable show_if_external',
        $options['downloadable']['wrapper_class']
    );
    return $options;
});
  • 第二步:让外部商品支持保存可下载相关配置
// 保存外部商品的可下载参数(下载文件、次数限制、有效期等)
add_action('woocommerce_process_product_meta_external', function($post_id) {
    $product = wc_get_product($post_id);
    $is_downloadable = isset($_POST['_downloadable']) ? 'yes' : 'no';
    $product->set_downloadable($is_downloadable);
    
    if (isset($_POST['_downloadable_files'])) {
        $download_files = array_filter((array) json_decode(wp_unslash($_POST['_downloadable_files']), true));
        $product->set_downloads($download_files);
    }
    if (isset($_POST['_download_limit'])) {
        $product->set_download_limit(absint($_POST['_download_limit']));
    }
    if (isset($_POST['_download_expiry'])) {
        $product->set_download_expiry(absint($_POST['_download_expiry']));
    }
    $product->save();
});
  • 第三步:给购买可下载外部商品的用户正常授予下载权限
// 订单付款完成后自动发放下载权限
add_action('woocommerce_order_status_processing', 'grant_external_product_download_perm');
add_action('woocommerce_order_status_completed', 'grant_external_product_download_perm');
function grant_external_product_download_perm($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return;
    foreach ($order->get_items() as $item) {
        $product = $item->get_product();
        if ($product->is_type('external') && $product->is_downloadable()) {
            wc_downloadable_product_permissions($order_id, $product->get_id(), $item->get_quantity());
        }
    }
}
  • 第四步:自定义外部跳转按钮文案,兼容下载入口展示
// 自定义外部跳转按钮文案,可根据需求修改文字内容
add_filter('woocommerce_product_single_add_to_cart_text', function($text, $product) {
    if ($product->is_type('external') && $product->is_downloadable()) {
        return __('购买平装本', 'woocommerce');
    }
    return $text;
}, 10, 2);

// 确保已购买用户在商品页能看到下载入口
add_action('woocommerce_after_add_to_cart_button', function() {
    global $product;
    if ($product->is_type('external') && $product->is_downloadable()) {
        if (is_user_logged_in() && wc_customer_bought_product(get_current_user()->user_email, get_current_user_id(), $product->get_id())) {
            wc_get_template('single-product/downloads.php');
        }
    }
});

方案二:无代码可视化配置方案

如果不想修改代码,可以通过商品自定义字段类插件实现:

  • 新建商品时选择简单可下载商品类型,正常上传PDF电子书、配置定价、下载规则
  • 给商品新增自定义按钮字段,填入亚马逊联盟跳转链接,设置按钮文案为「购买平装本」
  • 将自定义按钮的展示位置设置在「加入购物车」按钮旁,匹配主题按钮样式即可

配置注意事项

  • 代码方案部署完成后,编辑商品选择「外部/联盟商品」类型,即可看到和简单商品一致的可下载勾选框
  • 外部链接栏直接填写亚马逊平装本购买地址即可,按钮文案会自动应用代码里的自定义内容
  • 上线前走完整测试下单流程,确认付款完成后用户中心、订单详情页的电子书下载入口正常可用,外部跳转按钮无报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 18:36:51