Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

A solution can be found in the function woocommerce_add_to_cart_action. We have to chain two filters. In the first, we check for the product ID, if it's one of a list we add the redirect.

add_action( 'woocommerce_add_to_cart_validation', 'check_ids_wpse_119466', 10, 3 );

function check_ids_wpse_119466( $true, $product_id, $quantity ) 
{
    # Not our products, bail out
    # Adjust the products IDs
    if( !in_array( $product_id, array( 1306,1303 ) ) ) // <------ ADJUST LIST
        return $true;
        
    add_filter( 'add_to_cart_redirect', 'redirect_wpse_119466' );
    add_filter( 'allowed_redirect_hosts', 'whitelist_wpse_119466' );
    return $true;
}

function redirect_wpse_119466( $url )
{
    return 'http://paypal.com';
}

function whitelist_wpse_119466( $hosts )
{
    $hosts[] = 'paypal.com';
    return $hosts;
}

There's a third filter that we need to add, and it tells WordPress that PayPal is a safe host to redirect.

Yes, the list of products has to be made by hand. Or add an option somewhere in the theme, in a plugin or even WordPress settings to put this values (with a multiple selection field).

Related: Where do I put the code snippets I found here or somewhere else on the web?Where do I put the code snippets I found here or somewhere else on the web?

A solution can be found in the function woocommerce_add_to_cart_action. We have to chain two filters. In the first, we check for the product ID, if it's one of a list we add the redirect.

add_action( 'woocommerce_add_to_cart_validation', 'check_ids_wpse_119466', 10, 3 );

function check_ids_wpse_119466( $true, $product_id, $quantity ) 
{
    # Not our products, bail out
    # Adjust the products IDs
    if( !in_array( $product_id, array( 1306,1303 ) ) ) // <------ ADJUST LIST
        return $true;
        
    add_filter( 'add_to_cart_redirect', 'redirect_wpse_119466' );
    add_filter( 'allowed_redirect_hosts', 'whitelist_wpse_119466' );
    return $true;
}

function redirect_wpse_119466( $url )
{
    return 'http://paypal.com';
}

function whitelist_wpse_119466( $hosts )
{
    $hosts[] = 'paypal.com';
    return $hosts;
}

There's a third filter that we need to add, and it tells WordPress that PayPal is a safe host to redirect.

Yes, the list of products has to be made by hand. Or add an option somewhere in the theme, in a plugin or even WordPress settings to put this values (with a multiple selection field).

Related: Where do I put the code snippets I found here or somewhere else on the web?

A solution can be found in the function woocommerce_add_to_cart_action. We have to chain two filters. In the first, we check for the product ID, if it's one of a list we add the redirect.

add_action( 'woocommerce_add_to_cart_validation', 'check_ids_wpse_119466', 10, 3 );

function check_ids_wpse_119466( $true, $product_id, $quantity ) 
{
    # Not our products, bail out
    # Adjust the products IDs
    if( !in_array( $product_id, array( 1306,1303 ) ) ) // <------ ADJUST LIST
        return $true;
        
    add_filter( 'add_to_cart_redirect', 'redirect_wpse_119466' );
    add_filter( 'allowed_redirect_hosts', 'whitelist_wpse_119466' );
    return $true;
}

function redirect_wpse_119466( $url )
{
    return 'http://paypal.com';
}

function whitelist_wpse_119466( $hosts )
{
    $hosts[] = 'paypal.com';
    return $hosts;
}

There's a third filter that we need to add, and it tells WordPress that PayPal is a safe host to redirect.

Yes, the list of products has to be made by hand. Or add an option somewhere in the theme, in a plugin or even WordPress settings to put this values (with a multiple selection field).

Related: Where do I put the code snippets I found here or somewhere else on the web?

Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

A solution can be found in the function woocommerce_add_to_cart_action. We have to chain two filters. In the first, we check for the product ID, if it's one of a list we add the redirect.

add_action( 'woocommerce_add_to_cart_validation', 'check_ids_wpse_119466', 10, 3 );

function check_ids_wpse_119466( $true, $product_id, $quantity ) 
{
    # Not our products, bail out
    # Adjust the products IDs
    if( !in_array( $product_id, array( 1306,1303 ) ) ) // <------ ADJUST LIST
        return $true;
        
    add_filter( 'add_to_cart_redirect', 'redirect_wpse_119466' );
    add_filter( 'allowed_redirect_hosts', 'whitelist_wpse_119466' );
    return $true;
}

function redirect_wpse_119466( $url )
{
    return 'http://paypal.com';
}

function whitelist_wpse_119466( $hosts )
{
    $hosts[] = 'paypal.com';
    return $hosts;
}

There's a third filter that we need to add, and it tells WordPress that PayPal is a safe host to redirect.

Yes, the list of products has to be made by hand. Or add an option somewhere in the theme, in a plugin or even WordPress settings to put this values (with a multiple selection field).

Related: Where do I put the code snippets I found here or somewhere else on the web?