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

WooCommerce中Webinar关联eLearning自动加购及校验功能实现求助

Solution for Webinar & eLearning Product Dependency

Step 1: Helper Function to Check Past Purchases

First, create a helper function to verify if the logged-in user has already purchased the eLearning product:

// Helper function to check if user has purchased eLearning product
function has_purchased_elearning( $user_id = null ) {
    if ( ! $user_id ) {
        $user_id = get_current_user_id();
    }
    if ( ! $user_id ) {
        return false; // User not logged in
    }
    
    $elearning_product_id = 123; // Replace with your eLearning product ID
    $orders = wc_get_orders( array(
        'customer_id' => $user_id,
        'status' => array( 'wc-completed', 'wc-processing' ),
        'limit' => -1,
    ) );
    
    foreach ( $orders as $order ) {
        foreach ( $order->get_items() as $item ) {
            if ( $item->get_product_id() == $elearning_product_id ) {
                return true;
            }
        }
    }
    return false;
}

Step 2: Auto-Add eLearning When Webinar is Added

This code checks if the user is adding a Webinar to their cart. If they haven't purchased eLearning before and it's not already in the cart, it automatically adds the eLearning product:

// Auto-add eLearning when Webinar is added to cart (if required)
add_action( 'woocommerce_add_to_cart', 'auto_add_elearning_with_webinar', 10, 6 );
function auto_add_elearning_with_webinar( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    $webinar_product_id = 456; // Replace with your Webinar product ID
    $elearning_product_id = 123; // Replace with your eLearning product ID
    
    // Exit if added product isn't Webinar
    if ( $product_id != $webinar_product_id && $variation_id != $webinar_product_id ) {
        return;
    }
    
    // Check if eLearning is already in cart
    $elearning_in_cart = false;
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( $item['product_id'] == $elearning_product_id || $item['variation_id'] == $elearning_product_id ) {
            $elearning_in_cart = true;
            break;
        }
    }
    
    $has_purchased = has_purchased_elearning();
    
    // Add eLearning to cart if needed
    if ( ! $elearning_in_cart && ! $has_purchased ) {
        WC()->cart->add_to_cart( $elearning_product_id );
        wc_add_notice( __( 'eLearning product has been added to your cart since Webinar requires it.', 'woocommerce' ), 'notice' );
    }
}

Step 3: Checkout Validation

This ensures that if the user removes the auto-added eLearning product, they can't proceed to checkout without meeting the dependency requirement:

// Validate cart at checkout for Webinar-eLearning dependency
add_action( 'woocommerce_check_cart_items', 'validate_webinar_elearning_dependency' );
function validate_webinar_elearning_dependency() {
    $webinar_product_id = 456; // Replace with your Webinar product ID
    $elearning_product_id = 123; // Replace with your eLearning product ID
    
    $webinar_in_cart = false;
    $elearning_in_cart = false;
    
    // Scan cart for relevant products
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( $item['product_id'] == $webinar_product_id || $item['variation_id'] == $webinar_product_id ) {
            $webinar_in_cart = true;
        }
        if ( $item['product_id'] == $elearning_product_id || $item['variation_id'] == $elearning_product_id ) {
            $elearning_in_cart = true;
        }
    }
    
    // Block checkout if dependency isn't met
    if ( $webinar_in_cart && ! $elearning_in_cart && ! has_purchased_elearning() ) {
        wc_add_notice( __( 'Webinar requires you to either purchase the eLearning product along with it, or have purchased it previously.', 'woocommerce' ), 'error' );
        add_filter( 'woocommerce_proceed_to_checkout_button', '__return_false' );
    }
}

Important Notes

  • Replace 123 and 456 with your actual eLearning and Webinar product IDs (find these in WooCommerce > Products).
  • Add this code to your child theme's functions.php file or a custom code plugin (avoid adding to parent theme files to prevent loss on updates).
  • Test all scenarios:
    • Logged-in user who has purchased eLearning adding Webinar alone
    • Logged-in user without past eLearning purchase adding Webinar
    • Guest user adding Webinar
    • Removing eLearning from cart and attempting checkout

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 01:50:20