skip to Main Content

Currently having a listing site that uses Woocommerce and woo subscriptions. There are two customers – free and premium. In the user-dashboard, I would like to hide from the free users a specific area that is visits statistics. I would like to redirect this page to another one for the free members. Can someone help to solve this? I think I need a

Woocommerce function that checks if the user has a product "11344" (free product that is already purchased/owned)
Javascript redirecting specific page to another one if the user has the product
How to achieve this?

2

Answers


  1. normally woocommerce add some meta tags. I would recommend to check the database for those tags for premium and free users and code a "if else" statement in the template for both like:

    
    $premium = !empty( get_user_meta( $user_id, "meta_key", true) ) ? get_user_meta( $user_id, "meta_key", true) : false;
    
    
    if ( $premium ) {
     // display premium
    } else {
     // display free 
    }
    

    This way you are not forced to use javascript and reduce the loading time for the page.

    Login or Signup to reply.
  2. //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://siteurl.com/"); // Add redirect URL Here 
     exit;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search