Add add to cart button anywhere

Hi, Tony,

What happens is that when you add the Side cart woocommerce plugin you are modifying the final url of your shopping cart, which causes that when you click on go add to cart in the custom buttons, it does not find the URL and generates the error.

An option to solve it is to add a redirection only to these products through their ID, for this you can use the following code which you can include in the function.php of your theme that is located in the wp-content/themes directory /your_theme or do it through a plugin that allows you to add custom codes ->

Within this plugin you just have to select Add new

Within this you must paste the following code and replace the numbers found within the array with the id of your products

function my_custom_add_to_cart_redirect( $url ) {

if ( ! isset( $_REQUEST ) || ! is_numeric( $_REQUEST ) ) {
return $url;
}

$product_id = apply_filters( ‘woocommerce_add_to_cart_product_id’, absint( $_REQUEST ) );

// Only redirect the product IDs inside the Array
if ( in_array( $product_id, array( 1, 2, 3, 4, 5, 6, 7, 8 ) ) ) {
$url = WC()->cart->get_cart_url();
}

return $url;

}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );

It should be for example as follows

Then you just have to save the changes in the snippet and check if adding any of these products redirects you to the cart correctly.

Check this and tell us

A greeting

AnswerQuote

Answered: 09/03/2020 2:28 pm

Loading Facebook Comments ...
Loading Disqus Comments ...