WooCommerce: Site-wide cookie filter - wordpress

We need set site-wide cookie filter by brands.
WooComerce have build-in Brands taxonomy.
At the start landing page user will be select one from several brands. And after, user views products on site only by selected brand. If user will visit brand url (www.example.com/brands/brand-second), then need change filter to another brand, and shows products only by last selected brand. Others products not allowed to shows (another brand or without brand).
What any ideas for realize this?

I think you can try hold the visible brand in a cookie, and use woocommerce_product_query or pre_get_posts filters to filter according to that cookie value.
Each time the user visits that brand URL, change the cookie value.

I found solution for my question!
function filter_products_by_brand( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'brand',
'field' => 'slug',
'terms' => array( 'dobro' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'pre_get_posts', 'filter_products_by_brand' );

Related

woocommerce_product_query dont work on pages with shop (dont show specific product)

i have some categories, product can be in some of them but when its in cat with ID 46 its should not list anymore in the product list.
The following code work in the shop sites but not when i add the shop over the bb/shortshort , thanks
<!-- wp:woocommerce/product-category {"columns":4,"rows":6,"categories":[31]} /-->
add_action( 'woocommerce_product_query','custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => [46],
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
edit:
after some research i found a way, not the best but solved the problem.
Its seems like the data used for these pages are written in the options, so maybe here is a way to block adding the id of the unwanted
The working code is following and on this way it would be maybe a good hook for use same html/functions like used in the shop pages because all the product hooks of wc don't work usually on none shop pages.
Maybe WC adds someday direct a ignore product cats in the code, im sure im not the only one who need it
function my_product_block( $html, $data, $product ) {
if(in_array(46, $product->category_ids) || $product->stock_status === "outofstock"){
return "";
}
return $html;
}
add_filter( 'woocommerce_blocks_product_grid_item_html','my_product_block', 10,3)

Hide some products from woo-commerce products list

I have a specific product type that I want to hide from Product Listing page for admins. Each product is basically a dynamically created product by our customers. We want to receive orders against them but don't want them to populate our listing here.
In your screenshot, there is not specified particular product hide based on category/Tag-based. You can hide products using the "Catalog Visibility" setting. If the product creating dynamically then you should pass "catalog visibility" hidden value so that product is not listed
We can use the following code snippet added to the functions.php file to hide the product from the shop page:
/**
* Hide WooCommerce product by category from the shop page.
*
* #param $q
*/
add_action( 'woocommerce_product_query', 'njengah_hide_product_by_category_shop_page' );
function njengah_hide_product_by_category_shop_page( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'uncategorised','auto-parts'), // Don't display products in the 'auto parts' category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}

How to hide few product page until the User Login in Woocommerce?

I want to hide few products from Unregistered/non-login users. I have the following code. The code hide the whole category, but I want to hide a few products. If a user has the link to the product and tries to open a page, an error message should display.
function my_product_query( $q ) {
// Not logged in
if ( !$is_logged_in ) {
$q->set( 'tax_query',
array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat, // your category slug
'operator' => 'NOT IN'
)
)
);
}
}
add_action( 'woocommerce_product_query', 'my_product_query', 10, 1 );
First of all, you can add the custom field to all the products to find out whether the product is for logged-in users or non-logged-in users. Then, you can use below code for redirect non logged-in users.
add_action('template_redirect','custom_single_product_redirect');
function custom_single_product_redirect(){
if (class_exists('WooCommerce')){
if(is_product()){
$is_loggedin = get_post_meta(get_the_ID(), 'your_meta_field_key'); //get product meta key value, assuming boolean
if($is_loggedin == 1 && !is_user_logged_in()){ //if user is not logged in and meta key is set to only for logged in users
wp_redirect(home_url('/shop/'));
exit();
}
}
}
return;
}

Woocommerce URL to hide out of stock product

I create a catalog website using Woocommerce to display all the product. When the product is sell, i dont remove it from the website (because we dont have a lot of product and we want to show to the customer what we sell before).
So, when you go on "All the products" you see the Sell products and the products available. I want, on the sidebar create a button "Show only available product". I dont find a plugin who can do this..
Whis woocommerce, can i create a URL like "mywebsite.com/products&instock=true" for example or something like this ? or if you know another solution. Thanks
You can use pre_get_posts to achieve it. Set the tax_query to not get the term outofstock of the product_visibility taxonomy.
In addition to my code, you will of course need to create a link with the prefix_instock=true parameter. You also can store it in a cookie, so it will be easily persistent.
add_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
function prefix_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() || empty($_GET['prefix_instock'])) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) && $_GET['prefix_instock'] == 'true') {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
}

Hide 'out of stock' products in Woocommerce

Under "Products" and "Inventory" I have checked the following setting:
"Hide out of stock items from the catalog"
Now all sold out products are hidden in the archive/category view. So far so good.
The problem is that the hidden (out of stock) products are counted per page. So if there are 3 products that are sold out on the first page, only the ones in stock are showing (6).
It also seems that these "hidden" products still are searchable as well, and visible through the different widgets.
Any ideas how to fix this? I mean to REALLY hide products that are out of stock. Or do I need to manuallly remove them?
You can try adding this to your theme's functions.php file:
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'meta_query', array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
I modified the code from this URL: http://www.wptaskforce.com/how-to-exclude-one-or-more-category-in-woocommerce-shop-page/
Saved here again just in case that site goes offline: (this code excludes certain product categories)
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the membership category on the shop page . For multiple category , separate it with comma.
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Note to self: Always read the changelog from developer.
Found the answer here: http://develop.woothemes.com/woocommerce/2014/02/solving-common-issues-after-updating-to-woocommerce-2-1/#category-counts-incorrect
In case the product counts for categories are showing a too high or
too low number, after updating to WooCommerce 2.1 there is an easy
workaround.
Go to the ‘Tools’ tab inside the WooCommerce > System Status of your
WordPress administration panel. Here you first use the ‘Recount terms’
button and after that use the ‘Clear transients’ button. This will
force the system to recount all the products the next time a category
is loaded.
Update: Also remember that it is not enough to change stock quantity to 0. You must also set "Stock status" to "Out of stock". If not the product will be counted in the shop, even if there are no products in stock.
I found easier way, if anybody is still looking for hiding out of stock products in woocommerce, follow these easy steps without editing html !
Go to WooCommerce -> Settings
Go to Inventory
There's a checkbox that says something about our problem, however it goes in english :-) you'll find what you need
Save
that will only work if you are using the official woocommerce shortcodes , but if you creating a page with visual composer and using customized plugins or 3rd party plugins or shortcodes , the first step is to for the query that run from the loop then you modify it to something like this
$params = array(
'posts_per_page' => 5,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
the most important part that you have to be sure of is
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
Steps to Hide Out of Stock Products
Go to WooCommerce -> Settings submenu in the WordPress dashboard
Click on the Products Tab > Inventory sub-tab
Check the option Out Of Stock Visibility that hides the out of stock products
I know that this question was asked long time ago but the solution of the problem is now different, so I post this for people who had the same problem as I did. Tested on WooCommerce 5.3.0
SOLUTION:
First of all make sure that checkbox "Hide products that are out of stock" in Woocomerce > Settings is unchecked than add this PHP code to your child-theme functions.php file:
add_action('woocommerce_product_query', 'show_only_instock_products');
function show_only_instock_products($query) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key' => '_stock_status',
'compare' => '=',
'value' => 'instock'
);
$query->set( 'meta_query', $meta_query );
}
It works well in my store, which is integrated with a wholesaler, where inventory levels are updated every hour and there are thousands of products.
You can place PHP snippet at the bottom of your child theme functions.php file.
add_action('woocommerce_product_query', 'custom_woocommerce_product_query');
function custom_woocommerce_product_query($q)
{
if (!is_admin())
{
$oos_query = new WP_Query(['meta_query' => [['key' => '_stock_status', 'value' => 'outofstock', 'compare' => '=', ], ], 'post_type' => 'product', 'posts_per_page' => - 1, 'fields' => 'ids', ]);
$exclude_ids = $oos_query->posts;
$q->set('post__not_in', $exclude_ids);
}
}

Resources