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
123and456with your actual eLearning and Webinar product IDs (find these in WooCommerce > Products). - Add this code to your child theme's
functions.phpfile 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
相关产品推荐
相关产品推荐

