Redirect based on custom conditions

PHOTO EMBED

Tue Mar 15 2022 14:15:16 GMT+0000 (Coordinated Universal Time)

Saved by @zildjianjaxolis #wordpress #php #woocommerce

function b2b_redirect_if_product_is_not_for_user() {
    if ( is_product() ) {
        $allowed_user = get_post_meta( get_the_ID(), '_allowed_user', true );
        if ( $allowed_user != get_current_user_id() ) {
            wp_redirect( site_url( '/shop' ), 301 );
        }
    }
}

add_action('template_redirect', 'b2b_redirect_if_product_is_not_for_user');
content_copyCOPY