我正在嘗試在付款后根據 2 個類別創建 2 個不同的感謝頁面。
如果產品在類別專案上,我可以重定向到 1 頁,如果不是,則重定向到另一個頁面。
但是我不能把它分成 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;
}
}
}
uj5u.com熱心網友回復:
您可以使用接收所有產品條款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;
}
}
}
uj5u.com熱心網友回復:
以下代碼使用專用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 檔案中。測驗和作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/338759.html
標籤:php WordPress的
