This code is tested and works fine on a fresh WP and WooCommerce installation.
if ( !is_user_logged_in()) {
function exclude_product_from_users($q){
$current_user = wp_get_current_user();
$prodcuts_ids = array(648);
$q->set( 'post__not_in', $prodcuts_ids );
}
add_action( 'woocommerce_product_query', 'exclude_product_from_users' );
function return_from_product_page(){
global $post;
$prodcuts_ids = array(648);
if(in_array( $post->ID , $prodcuts_ids)){
wp_redirect(home_url("https://wordpress.stackexchange.com/"));
exit();
}
}
add_action('wp', 'return_from_product_page');
}
Related Posts:
- Woocommerce – Hide a Column in Cart Table
- How do I change the initial quantity of a product in woocommerce?
- how to add custom functionality after woocommerce place order button
- Remove an action by extending class and replacing it
- WooCommerce change Tax Class programmatically when Recalculating an existing Order [closed]
- how to retrieve post_id under woocommerce_add_to_cart_validation hook?
- woocommerce single product page hook not working
- Woocommerce: block user removing cart item
- Trigger a JavaScript function based on the data fetched from Woo commerce hook
- Add a new tab to WordPress Plugin install Listing
- Admin settings update updating every time home page is hit?
- Global Objects and Public Methods
- How do you use the plugin boilerplate loader class to hook actions and filters?
- How Do I Load My Action Earlier Enough?
- Does WP fire delete_post when trashed posts are automatically deleted?
- Why does do_action pass a blank string as the first parameter if no $arg is set?
- WooCommerce get Shipping Class of product from either the product id or the order after order is completed
- How to add a link to WordPress Plugin install Listing
- I can’t find where a hook is being defined in a plugin – Easy Digital Downloads
- Call activation hook multiple times
- Two-step login process – Is it possible?
- get all products of one category
- How can I hook into existing WordPress Bulk actions?
- Hook for post permalink update
- Get returned variable from a function to add_shortcode function
- Woocommerce add custom button to backend order table
- How do I hook a custom discount to change a WC_Order price total on WooCommerce?
- Earliest WP Hook to Safely and Reliably Redirect
- Define a function outside a class and call the function using action or filter hook
- Can I differentiate between “Delete Post Permanently” and “Empty Trash” and do something for each accordingly?
- Synchronize Custom post type tags to WordPress default posts tags
- unable to stop loop when using wp_insert_post with publish_post hook
- Print value of an array or variable in a payment plugin
- Hook into theme-switching later than `setup_theme`
- Is it possible to delay execution of shortcode output callback?
- WordPress Ajax callback function from plugin – OOP
- $wpdb -> Batch insert from XML File?
- Why my database table not created on activation?
- Act on user meta updated, but only once
- Is there any way to allow a plugin to write over a previous version?
- How do I add custom column to woocommerce cart?
- best practice for query string values – get_query_var always empty for my value supplied in query string
- get_current_screen() return null
- WooCommerce Conditional Tag inside plugin
- Custom attribute type not displaying terms in edit product – WooCommerce
- update woocommere total value on checkout based on selected payment method
- Modifying meta tags after doing ajax call in plugin
- Where to add hooks in a class
- Adding a brand column to WooCommerce Products
- Conditional hook based on the core function that is calling it
- Adding option to Gallery shortcode
- PHP – Extend WordPress Woocommerce Revenue Analytics with custom field
- I have 2 plugins using the same wp_login action hook and one is not working
- How dynamic action login_form_{action} is working
- How to get all queries’s results after they have executed?
- flush rewrite rules after plugin update?
- Odd map_meta_cap $args on post edit — sometimes integer, sometimes string
- How can I create a custom plugin that allows anonymous users to signup without registering?
- Associate multiple email addresses with the same user account, so they can log in with either
- Can woocommerse be used dynamically for billing from another plugin for payment processing?
- Custom filter not working
- How to call code when adding WooCommerce menu items via woocommerce_account_menu_items
- Configure WordPress to Generate Scheme-less Relative URLs
- Ajax +wordpress onClick link redirect to new page and create html content
- How to replace settings in WordPress plugin from a theme
- Creating posts with links from a txt file
- How to make every image title equal to alt text(wordpress/woocommerce)?
- Action / Hook when a new plugin is added
- apply_filters() and call_user_func() to define and call a function outside a class
- Getting Post ID at “stylesheet” and “template” hooks
- Trigger function on Remove block or add new block in Gutenberg JavaScript
- Limit get_next_post to posts from the same author
- Getting products information, in woocommerce based on products ID
- Any way, hook to add content right before the “read more” link?
- Is there any way to get post meta from publish_post hook?
- WC REST API Error Handling
- How do I ensure I can loop through every enqueued script and CSS?
- want to confirm popup with “Yes” and “No” button when user click on add to cart
- How to remove the WooCommerce Product->Category thumbnail from admin [closed]
- Override WooCommerce files from plugin
- woocommerce add a new bulk action in the bulk actions dropdown in the orders list
- Plugin Development – Call to undefined function comment_exists()
- Prevent plugin activation and add admin notice
- Handle changed Woocommerce function
- Making Woocommerce optimized for more than 500k products
- Does WordPress have something like timer hook?
- Declaring a new woo commerce product type i get this error
- Can I use a hook other than ‘init’ to handle form submissions?
- Template file structure , wordpress hook for altering the template
- How do I replace title with my plugin?
- Looking for Hook that is fired after a plugin or wp upgrade is installed/updated
- How to design WooCommerce-like admin tabs for plugin settings page?
- How can I get a plugin to hook ‘dbdelta_queries’ — a filter used during version update?
- Send retrieve password notification email with custom HTML email template
- WooCommerce adds a newline symbol (\n) between email recipients
- Ninja Forms: Front-End Forms, Post ID?
- Allow HTML in product attributes and variation for WooCommerce
- How do I add multiple custom menu Woo-commerce my account page?
- Execute callback after REST API request completes + Response is sent
- High-Performance Order Storage conflict action `manage_shop_order_posts_custom_column`