简体   繁体   中英

WooCommerce Add to Cart custom redirection for multiple products Variations

i use woocomerce Add to Cart custom redirection for multiple products this is my working code:

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect', 10, 2 );
function custom_add_to_cart_redirect( $redirect_url ) {
    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );

    // Only redirect the product IDs in the array to the checkout
    if ( in_array( $product_id, array( 3008 ) ) ) {
     wp_redirect( 'www...' );
             exit;
    }
    if ( in_array( $product_id, array( 2992 ) ) ) {
     wp_redirect( 'www...' );
             exit;
    }
    return $redirect_url;
}

i need edit this code for variations product i try this but didn't work :

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect', 10, 2 );
function custom_add_to_cart_redirect( $redirect_url ) {
    $variation_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );

    // Only redirect the product IDs in the array to the checkout
    if ( in_array( $variation_id, array( 3008 ) ) ) {
     wp_redirect( 'www...' );
             exit;
    }
    if ( in_array( $variation_id, array( 2992 ) ) ) {
     wp_redirect( 'www...' );
             exit;
    }
    return $redirect_url;
}

You need to swap out this line

$variation_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );

With this

$variation_id = absint( $_REQUEST['variation_id'] );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM