I have this code:
add_action('template_redirect', 'woo_custom_redirect');
function woo_custom_redirect( $redirect ) {
if (
! is_user_logged_in()
&& (is_checkout())
) {
wp_redirect( home_url( '/my-account/edit-account/' ) );
return $redirect;
}
}
How can I replace the redirection condition when I make an order to purchase a product from a certain category?
For example, if a user purchases a product from a certain category, then when he attempts to place an order, he redirects to registration and back, after successful registration.
The following code will redirect to my account page the non logged user in checkout page when they have an item from a specific product category. You will have to define in the code your specific product category:
add_action('template_redirect', 'woo_custom_redirect');
function woo_custom_redirect( $redirect ) {
// HERE set your product category (can be term IDs, slugs or names)
$category = 'posters';
$found = false;
// CHECK CART ITEMS: search for items from our product category
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
$found = true;
break;
}
}
if ( ! is_user_logged_in() && is_checkout() && $found ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.