-1

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sanfe
  • 21
  • 4
  • We expect you to provide your code attempt (effort) in your question as StackOverFlow is not a free coding service. Also it seems that you didn't search a bit before asking, see [*"How much research effort is expected of Stack Overflow users?"*](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)… – LoicTheAztec Jul 03 '20 at 11:00
  • Additionally Javascript is absolutely not needed for that. – LoicTheAztec Jul 03 '20 at 11:32

2 Answers2

0

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.

0
//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;
}
  • 1
    [wc_customer_bought_product](https://docs.woocommerce.com/wc-apidocs/function-wc_customer_bought_product.html) is more suitable. -- Checks if a user (by email or ID or both) has bought an item. – 7uc1f3r Jul 03 '20 at 10:49