woocommerce在每个类别结帐后自定义重定向



我试图有2个不同的感谢页面基于2类付款后。

我可以重定向到只有一个页面,如果产品是在类别项目,如果不是另一个。

但是我不能把这个分成2个不同的类别,我只是php的新手,如果有人能帮助我,那就太好了。

我的代码:


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;
}
}
}

下面的代码使用专用的template_redirect钩子和WordPresshas_term()条件函数(用于产品类别),将在结账后将客户重定向到我的帐户部分,如果他们的订单包含来自定义的产品类别的商品:

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
}
}
}

代码放在活动子主题(或活动主题)的function.php文件中。

您可以使用get_the_terms()接收所有产品术语,然后将它们推入数组。试试下面的代码:

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();
foreach( $order->get_items() as $item ){    
$product_cat = get_the_terms( $item->get_product_id() , 'product_cat' );
if( !empty( $product_cat ) ){
foreach ( $product_cat as $cat ) {
$product_categories[] = $cat->slug;
}
}
}
if( !empty( $product_categories ) && in_array('asesoria-personalizada', $product_categories ) ){
wp_redirect( 'https://artchitectsproductions.com/gracias-compra-asesoria/');
} elseif ( !empty( $product_categories ) && 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;
}
}
}

最新更新