Home > database >  WooCommerce Add to Cart custom redirection for multiple products Variations
WooCommerce Add to Cart custom redirection for multiple products Variations

Time:06-27

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;
}

CodePudding user response:

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'] );
  • Related