I need to make it so when people press place order on our webshop, they will get redirectet to My Account, but only if the categories is XXX , XXX , XXX
But i can't seem to get it working unfortunally
I have tried using && is_product_category('Category x','Category x','Category x')
// REDIRECT AFTER PLACE ORDER BUTTON!
add_action( 'woocommerce_thankyou', 'KSVS_redirect_custom');
function KSVS_redirect_custom( $order_id ){
$order = new WC_Order( $order_id );
$url = 'https://kanselvvilselv.dk/min-konto/';
if ( $order->status != 'failed' ) {
wp_redirect($url);
exit;
}
}
It is working without putting in && is_product_category('Category x','Category x','Category x')
, But then it is working on categories where it should not work.
The following code using dedicated template_redirect
hook and WordPress has_term()
conditional function (to be used with product categories) , will redirect customers after checkout to my account section, when their order contain items from defined product categories:
add_action( 'template_redirect', 'order_received_redirection_to_my_account' );
function order_received_redirection_to_my_account() {
// Only on "Order received" page
if( is_wc_endpoint_url('order-received') ) {
global $wp;
// HERE below define your product categories in the array
$categories = array('Tshirts', 'Hoodies', 'Glasses');
$order = wc_get_order( absint($wp->query_vars['order-received']) ); // Get the Order Object
$category_found = false;
// Loop theough order items
foreach( $order->get_items() as $item ){
if( has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
$category_found = true;
break;
}
}
if( $category_found ) {
// My account redirection url
$my_account_redirect_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
wp_redirect( $my_account_redirect_url );
exit(); // Always exit
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Updated, this should loop through all ordered products and if it matches 1 product with a category then it will redirect to your URL:
add_action( 'woocommerce_thankyou', 'KSVS_redirectcustom');
function KSVS_redirectcustom( $order_id ){
$order = wc_get_order( $order_id );
$url = get_permalink( get_option('woocommerce_myaccount_page_id') );
if ( $order->status != 'failed' ) {
$product_cats = array('product-cat1', 'product-cat', 'product-cat3');
foreach ($order->get_items() as $item) {
if ( has_term( $product_cats, 'product_cat', $product->id) ) {
$cat_check = true;
break;
}
}
if ( $cat_check ) {
wp_redirect($url);
exit;
}
}
}
Note: i'm not familiar with woocommerce at all, please take my answer with ease. Seems like function is_product_category
have different purpouse, by quick overview I came with this, give it a try:
$redirectWhenCategoryIs = ['cat x', 'cat y', 'cat z'];
$categories = [];
foreach($order->get_items() as $item) {
foreach(get_the_terms($item['product_id'], 'product_cat') as $term){
$categories[] = $term->slug;
}
}
if(count(array_intersect($redirectWhenCategoryIs, $categories))){
wp_redirect($url);
}
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.