Created
August 1, 2023 10:41
-
-
Save elias1435/3dd9a42788761507088e158baa4bcab9 to your computer and use it in GitHub Desktop.
If you want to redirect after add to cart product only for selected category for this example category is 'main-category' use this below code to functions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function custom_add_to_cart_redirect( $url ) { | |
global $woocommerce; | |
// Retrieve the product ID from the 'add-to-cart' request parameter. | |
$product_id = isset( $_REQUEST['add-to-cart'] ) ? intval( $_REQUEST['add-to-cart'] ) : 0; | |
// Check if the product ID is valid and if the category name is 'main-product'. | |
if ( $product_id > 0 && has_term( 'main-product', 'product_cat', $product_id ) ) { | |
// The URL you want to redirect to for 'main-product'. | |
$redirect_url = 'https://yourdomain.com/add_your_url_here'; | |
return $redirect_url; | |
} | |
// If the category is not 'main-product', return the original URL. | |
return $url; | |
} | |
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment