i want to redirect users when they click single page product directly to cart without going to product description page for only one category products.
i used this code:
add_action( 'wp', 'ts_redirect_product_pages', 99 );
function ts_redirect_product_pages() {
if ( is_product() ) {
wp_safe_redirect( home_url('/cart/'));
exit;
}
}
it's redirecting to the cart page but all the other product categories also redirecting i only want one product category to be redirected, please guide me how to do this
Better to use template_redirect
hook. To target specific product category(ies), you can use WordPress has_term()
conditional function. For cart url, is better to use WooCommerce function wc_get_cart_url()
...
So in the code below define your product category(ies) term(s) name(s), slug(s) or Id(s):
add_action( 'template_redirect', 'ts_redirect_product_pages' );
function ts_redirect_product_pages() {
$categories = array('my-category-1', 'my-category-2');
if ( is_product() && has_term( $categories, 'product_cat' ) ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
Code goes in functions.php file of the active child theme (or active theme) . It should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.