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

根据已购产品自定义字段替换WooCommerce加入购物车按钮

问题修复方案

1. 修复感谢页崩溃问题

你的custom_display_google_meet_link函数错误处理了woocommerce_thankyou钩子的参数——该钩子传递的是订单ID,而非订单对象。直接将其当作对象调用方法会触发致命错误,导致页面崩溃。

修正后的核心代码:

// Display Google Meet link on order details and thank you page (clickable)
function custom_display_google_meet_link($param) {
    // 区分钩子参数:订单对象(订单详情页)/订单ID(感谢页)
    if (is_numeric($param)) {
        $order = wc_get_order($param);
        if (!$order) return; // 订单不存在时直接退出,避免报错
    } else {
        $order = $param;
    }

    $items = $order->get_items();

    foreach ($items as $item) {
        $product_id = $item->get_product_id();
        $google_meet_link = get_post_meta($product_id, 'google_meet_link', true);

        if (!empty($google_meet_link)) {
            echo '<h3>' . __('Google Meet Link:', 'text-domain') . '</h3>';
            echo '<p><a href="' . esc_url($google_meet_link) . '" target="_blank">' . esc_html($google_meet_link) . '</a></p>';
        }
    }
}
add_action('woocommerce_order_details_after_order_table', 'custom_display_google_meet_link', 10, 1);
add_action('woocommerce_thankyou', 'custom_display_google_meet_link', 10, 1);

关键调整:

  • 增加参数类型判断,针对感谢页的订单ID先获取合法的订单对象
  • 增加订单存在性检查,防止无效ID引发的报错

2. 修复购买后按钮未替换问题

原代码仅根据产品类型修改按钮文本,但未校验用户是否已购买该产品,也未改变按钮的加购行为。需要添加用户购买状态检查,并将按钮替换为直接跳转至Google Meet的链接。

补充完整代码(可直接追加到原有代码后):

// 辅助函数:检查当前用户是否已购买指定产品
function custom_user_purchased_product($product_id) {
    if (!is_user_logged_in()) return false;

    $current_user = wp_get_current_user();
    // 获取用户的有效订单(已完成/处理中)
    $customer_orders = wc_get_orders(array(
        'customer_id' => $current_user->ID,
        'status' => array('completed', 'processing'),
        'limit' => -1,
    ));

    foreach ($customer_orders as $order) {
        if ($order->has_product($product_id)) {
            return true;
        }
    }

    return false;
}

// 单产品页:替换加购按钮为"加入研讨会"
function custom_replace_single_add_to_cart_button() {
    global $product;

    if (!$product || $product->get_type() !== 'google_meet_webinar') return;

    if (custom_user_purchased_product($product->get_id())) {
        $google_meet_link = get_post_meta($product->get_id(), 'google_meet_link', true);
        if (!empty($google_meet_link)) {
            // 移除默认加购按钮
            remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
            // 输出自定义跳转按钮
            echo '<a href="' . esc_url($google_meet_link) . '" target="_blank" class="button alt">' . __('Join Webinar', 'text-domain') . '</a>';
        }
    }
}
add_action('woocommerce_single_product_summary', 'custom_replace_single_add_to_cart_button', 25);

// 产品存档页(如Shop页):替换加购按钮文本与链接
function custom_replace_archive_add_to_cart_button($link, $product) {
    if ($product->get_type() === 'google_meet_webinar' && custom_user_purchased_product($product->get_id())) {
        $google_meet_link = get_post_meta($product->get_id(), 'google_meet_link', true);
        if (!empty($google_meet_link)) {
            $link = '<a href="' . esc_url($google_meet_link) . '" target="_blank" class="button">' . __('Join Webinar', 'text-domain') . '</a>';
        }
    }
    return $link;
}
add_filter('woocommerce_loop_add_to_cart_link', 'custom_replace_archive_add_to_cart_button', 10, 2);

关键调整:

  • 新增custom_user_purchased_product函数,校验当前登录用户是否已购买目标产品
  • 单产品页移除默认加购按钮,替换为跳转至Meet链接的自定义按钮
  • 产品存档页直接修改加购链接的HTML,实现文本与行为的双重替换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 21:24:55