WooCommerce实现多优惠券关联商品:验证后自动添加指定商品至购物车
Hey there! Let's fix that limited coupon setup and turn it into a scalable solution that lets you link any coupon to any product (or multiple products) right from the WooCommerce backend. Here's how to do it step by step:
Step 1: Add a Custom Field to Coupons for Linked Products
First, we need to add a field on the coupon edit page so you can specify which product should be added when the coupon is applied. This replaces the hardcoded product ID in your original code.
// Add linked product field to coupon edit screen add_action( 'woocommerce_coupon_options', 'wc_ninja_add_coupon_product_field', 10 ); add_action( 'woocommerce_coupon_options_save', 'wc_ninja_save_coupon_product_field', 10 ); function wc_ninja_add_coupon_product_field() { global $post; // Get saved linked product ID $linked_product_id = get_post_meta( $post->ID, '_wc_ninja_linked_product_id', true ); ?> <div class="options_group"> <?php woocommerce_wp_text_input( array( 'id' => '_wc_ninja_linked_product_id', 'label' => __( 'Linked Product ID', 'woocommerce' ), 'placeholder' => __( 'Enter product ID to link', 'woocommerce' ), 'desc_tip' => true, 'description' => __( 'When this coupon is applied, this product will be automatically added to the cart', 'woocommerce' ), 'value' => $linked_product_id ) ); ?> </div> <?php } function wc_ninja_save_coupon_product_field( $post_id ) { $linked_product_id = isset( $_POST['_wc_ninja_linked_product_id'] ) ? absint( $_POST['_wc_ninja_linked_product_id'] ) : ''; update_post_meta( $post_id, '_wc_ninja_linked_product_id', $linked_product_id ); }
This code adds a text input to the coupon edit page where you can type in the product ID you want to link. It saves that ID as custom metadata attached to the coupon.
Step 2: Modify Coupon Application Logic to Add Linked Products
Next, we'll update the coupon action to fetch the linked product ID from the coupon's metadata instead of checking for a single hardcoded coupon code. We'll also add some checks to avoid duplicate products in the cart.
// Auto add linked product to cart when coupon is applied add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon_add_product' ); function wc_ninja_apply_coupon_add_product( $coupon_code ) { // Get the coupon object from the code $coupon = new WC_Coupon( $coupon_code ); // Fetch the linked product ID from the coupon's metadata $linked_product_id = get_post_meta( $coupon->get_id(), '_wc_ninja_linked_product_id', true ); // Validate the product exists before adding if ( $linked_product_id && wc_get_product( $linked_product_id ) ) { // Check if the product is already in the cart to prevent duplicates $cart_item_key = WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $linked_product_id ) ); if ( ! $cart_item_key ) { // Add the product to the cart WC()->cart->add_to_cart( $linked_product_id ); // Show a success notice to the user wc_add_notice( sprintf( __( 'Coupon applied! Added "%s" to your cart.', 'woocommerce' ), get_the_title( $linked_product_id ) ), 'success' ); } } }
Bonus: Support Multiple Linked Products
If you want to link more than one product to a coupon, you can modify the field to accept comma-separated product IDs and update the logic to loop through them:
Updated Field Code (for multiple products):
// Add multiple linked products field to coupon edit screen add_action( 'woocommerce_coupon_options', 'wc_ninja_add_coupon_products_field', 10 ); add_action( 'woocommerce_coupon_options_save', 'wc_ninja_save_coupon_products_field', 10 ); function wc_ninja_add_coupon_products_field() { global $post; $linked_product_ids = get_post_meta( $post->ID, '_wc_ninja_linked_product_ids', true ); ?> <div class="options_group"> <?php woocommerce_wp_text_input( array( 'id' => '_wc_ninja_linked_product_ids', 'label' => __( 'Linked Product IDs', 'woocommerce' ), 'placeholder' => __( 'Enter product IDs separated by commas', 'woocommerce' ), 'desc_tip' => true, 'description' => __( 'When this coupon is applied, these products will be automatically added to the cart', 'woocommerce' ), 'value' => $linked_product_ids ) ); ?> </div> <?php } function wc_ninja_save_coupon_products_field( $post_id ) { $linked_product_ids = isset( $_POST['_wc_ninja_linked_product_ids'] ) ? sanitize_text_field( $_POST['_wc_ninja_linked_product_ids'] ) : ''; update_post_meta( $post_id, '_wc_ninja_linked_product_ids', $linked_product_ids ); }
Updated Cart Logic (for multiple products):
// Auto add multiple linked products to cart when coupon is applied add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon_add_multiple_products' ); function wc_ninja_apply_coupon_add_multiple_products( $coupon_code ) { $coupon = new WC_Coupon( $coupon_code ); $linked_product_ids = get_post_meta( $coupon->get_id(), '_wc_ninja_linked_product_ids', true ); if ( $linked_product_ids ) { // Split comma-separated IDs into an array $product_ids = array_map( 'absint', explode( ',', $linked_product_ids ) ); foreach ( $product_ids as $product_id ) { if ( wc_get_product( $product_id ) ) { $cart_item_key = WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product_id ) ); if ( ! $cart_item_key ) { WC()->cart->add_to_cart( $product_id ); wc_add_notice( sprintf( __( 'Added "%s" to your cart.', 'woocommerce' ), get_the_title( $product_id ) ), 'success' ); } } } } }
How to Use This
- Add the code to your theme's
functions.phpfile, or use a custom code plugin like Code Snippets (recommended to avoid losing code when updating your theme). - Go to WooCommerce > Coupons and edit any coupon. You'll see a new field labeled "Linked Product ID" (or "Linked Product IDs" for the multiple version).
- Enter the product ID(s) you want to link, save the coupon, and test it out! When a user applies the coupon, the linked product(s) will be added to their cart automatically.
This setup supports as many coupons as you need (1000+), each with their own linked products—no more hardcoding coupon codes or product IDs!
内容的提问来源于stack exchange,提问作者PelHout

