Custom add to cart button URL for specific product in WooCommerce?



PHP Snippet 1:

add_filter( 'woocommerce_add_to_cart_redirect', 'productbase_redirect_on_add_to_cart' );
function productbase_redirect_on_add_to_cart() {

    //Get product ID
    if ( isset( $_POST['add-to-cart'] ) ) {
        $product_id = (int) $_POST['add-to-cart'] ;             
    }
    // set redirect with product id here

}

PHP Snippet 2:

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );

function custom_add_to_cart_redirect( $url ) {

    // The product ID you want a specific redirect for
    $matched_id = 999; 

    // If product ID matches, modify redirect
    if ( isset( $_POST['add-to-cart'] ) ) && $matched_id = (int) $_POST['add-to-cart'] ) {            
      $url = 'http://www.yourdomain.com/your-page/'; 
    }
    // always return a value
    return $url;

}