Home > Enterprise >  WooCommerce Add to Cart custom redirection Variable Product
WooCommerce Add to Cart custom redirection Variable Product

Time:11-10

I have a code that works but it still adds the product to the cart. What should I change to achieve only redirection without adding a product to the cart?

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect', 10, 2 );
function custom_add_to_cart_redirect( $redirect_url ) {
$variation_id = absint( $_REQUEST['variation_id'] );

    // Only redirect the product IDs in the array to the checkout
    if ( in_array( $variation_id, array( 341 ) ) ) {
     wp_redirect( 'http://google.pl' );
             exit;
    }
    if ( in_array( $variation_id, array( 2992 ) ) ) {
     wp_redirect( 'http://google.pl' );
             exit;
    }
    return $redirect_url;
}

CodePudding user response:

Try this code:

add_filter('woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect', 10, 2);

function custom_add_to_cart_redirect($redirect_url)
{
    $variation_id = absint($_REQUEST['variation_id']);

    // Only redirect the product IDs in the array to the checkout
    if (in_array($variation_id, array(341))) {
        WC()->cart->empty_cart();
        wp_redirect('http://google.pl');
        exit;
    }
    return $redirect_url;
}
  • Related