1

I have this function, but it is not working as needed. Would it be better to change it to wc_customer_bought_product?

The goal is to check if the user has bought a free subscription product "11344" and when this user tries to access a specific page in the user account, it gets redirected to another page.

I have a page where visit statistics are displayed and it is www.website.com/my-account/. I want to hide that page so that when the user that has bought the mentioned product tries to go there, then gets redirected to another page which is www.website.com/my-account/my-listings/

Can someone suggest what I need to edit in this code? Currently stuck, as this one is not doing anything.

//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 IDS
if ( ! $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;
} 
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Sanfe
  • 21
  • 4
  • _"...The goal is to check if the user has bought a free subscription product ("11344") and when this user tries to access a specific page in the user account, it gets redirected to another page..."_ -- It is from that page that you have to run the above code. The code you have now is generally applied. Which page is it about? is it a default page in WooCommerce? – 7uc1f3r Jul 04 '20 at 09:28
  • I have a page where visit statistics are displayed and it is `www.website.com/my-account/`. I want to hide that page so that when the user that has bought the mentioned product tries to go there, then gets redirected to another page which is `www.website.com/my-account/my-listings/` – Sanfe Jul 04 '20 at 09:57

1 Answers1

0
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' );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Thank you so much! Works just as expected! May I ask, is it possible to hide a specific tab ( from user menu) based on the same product id? Is it possible with css or it's javascript? – Sanfe Jul 04 '20 at 10:43
  • CSS or JS is not recommended. The hook you can use is `woocommerce_account_menu_items`. See a similar question: [WooCommerce - Remove downloads from menu in my account page](https://stackoverflow.com/questions/39287308/woocommerce-remove-downloads-from-menu-in-my-account-page) (Answer from LoicTheAztec), then it is only a matter of adapting the code to your wishes. Know, however, that you are not supposed to ask a new question in the comment section. For additional or new questions, please open a new topic – 7uc1f3r Jul 04 '20 at 10:53