亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查用戶是否購買了商品并重定向到特定頁面

檢查用戶是否購買了商品并重定向到特定頁面

PHP
慕后森 2023-07-08 20:54:59
我有這個功能,但它不能按需要工作。改成這樣會不會更好wc_customer_bought_product?目標是檢查用戶是否購買了免費訂閱產品“ 11344”,并且當該用戶嘗試訪問用戶帳戶中的特定頁面時,它會被重定向到另一個頁面。我有一個顯示訪問統計信息的頁面,它是www.website.com/my-account/。我想隱藏該頁面,以便當購買了上述產品的用戶嘗試去那里時,然后被重定向到另一個頁面www.website.com/my-account/my-listings/有人可以建議我需要在這段代碼中編輯什么嗎?目前陷入困境,因為這個沒有做任何事情。//You can make this code as function and call it where it is require$current_user = wp_get_current_user();if ( 0 == $current_user->ID ) return;// GET USER ORDERS (COMPLETED + PROCESSING)$customer_orders = get_posts( array(    'numberposts' => -1,    'meta_key'    => '_customer_user',    'meta_value'  => $current_user->ID,    'post_type'   => wc_get_order_types(),    'post_status' => array_keys( wc_get_is_paid_statuses() ),) );// LOOP THROUGH ORDERS AND GET PRODUCT IDSif ( ! $customer_orders ) return;$product_ids = array();foreach ( $customer_orders as $customer_order ) {    $order = wc_get_order( $customer_order->ID );    $items = $order->get_items();    foreach ( $items as $item ) {        $product_id = $item->get_product_id();        $product_ids[] = $product_id;    }}$product_ids = array_unique( $product_ids );if (in_array("11344", $product_ids)){    wp_redirect("https://website.com/account2"); // Add redirect URL Here     exit;} 
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

  • template_redirect- 在確定要加載的模板之前觸發。

  • wp_get_current_user- 檢索當前用戶對象。

  • wc_customer_bought_product- 檢查用戶(通過電子郵件或 ID 或兩者)是否購買了商品。

function action_template_redirect() {

? ? global $wp;

? ??

? ? // Retrieve the current user object.

? ? $current_user = wp_get_current_user();

? ? if ( $current_user->ID == 0 ) return;

? ??

? ? // DEBUG PURPOSES, UNCOMMENT IF NECESSARY

? ? //echo $wp->request;


? ? // On my-account page

? ? if( $wp->request == 'my-account' ) {

? ? ? ? // Set product ID

? ? ? ? $product_id = 11344;

? ? ? ??

? ? ? ? // If true

? ? ? ? if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {

? ? ? ? ? ? // Redirect

? ? ? ? ? ? wp_safe_redirect('/my-account/my-listings/');

? ? ? ? ? ? exit;

? ? ? ? }? ?

? ? }

}

add_action( 'template_redirect', 'action_template_redirect' );


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號