I’m trying to have 2 diferent thank you pages based on 2 categories after payment.
I can redirect to just 1 page if product is on category item and to another one if not.
But i can’t split this for 2 diferent categories, i’m just new on php could be nice if someone could help me with this.
My code:
add_action( ‘template_redirect’, ‘wc_custom_redirect_after_purchase’ );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars[‘order-received’] ) ) {
$cat_in_cart = false;
$order_id = isset( $wp->query_vars[‘order-received’] ) ? intval( $wp->query_vars[‘order-received’] ) : 0;
$order = new WC_Order( $order_id );
$product_categories = array( ‘mezcla-y-mastering-online’, ‘asesoria-personalizada’ );
foreach( $order->get_items() as $item ){
if( has_term( $product_categories, ‘product_cat’, $item->get_product_id() ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat_in_cart) {
if(in_array(«asesoria-personalizada», $product_categories)){
wp_redirect( «https://artchitectsproductions.com/gracias-compra-asesoria/»);
} elseif (in_array(«mezcla-y-mastering-online», $product_categories)){
wp_redirect(«https://artchitectsproductions.com/gracias-compra-mezcla-y-mastering-online/»);
}
} else {
wp_redirect(«https://artchitectsproductions.com/gracias-por-tu-compra/»);
}
exit;
}
}
}
2
Answers
The following code using dedicated
template_redirect
hook and WordPresshas_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:Code goes in the function.php file of your active child theme (or active theme). Tested and works.
You can receive all product terms using
get_the_terms()
and then push them to an array. Try the below code.