Restrict a list of SKUs from being purchased by a specific group The site uses…

Request
This came in from a Stack Overflow question called WooCommerce Link Product Visibility To Day Of Week
Required Plugins
Code
- Copy the WooCommerce template file
content-product.php
to your theme’swoocommerce
directory - Change the area where it checks for visibility
FROM
// Ensure visibility if ( ! $product || ! $product->is_visible() ) return;
TO
// Ensure visibility // starting custom content $product_visible = check_for_product_allowed_days( $product ); if ( ! $product || ! $product->is_visible() || ! $product_visible ) return;
- Add the following to your functions.php file
// Allows for the display/non-display of products based off days allowed to be sold. // This only works in conjunction with editing // the woocommerce "content-product.php" template file function check_for_product_allowed_days ( $product ) { $product_id = $product->id; $product_terms = get_the_terms ( $product_id, 'product_tag' ); // remove the strtolower if you capitalized your tag names $current_day = strtolower ( date ( 'l' ) ); // $all_days value should be the name of the tag // that you want to be able to be ordered on all days $all_days = 'all days'; foreach ( $product_terms as $tag ) { if ( $tag->name == $current_day || $tag->name == $all_days ) { $product_is_visible = true; break; } else { $product_is_visible = false; } } return $product_is_visible; }
WooCommerce Admin Setup
- Add tags to all of your products
- “All Days” or “all days” (just be sure to change the value of
$all_days
above to whatever you set it to be – monday, tuesday, wednesday)
Thinking
I couldn’t find a method to break out of WooCommerce’s loop and set the product visibility before hand so a template update was necessary.
Result
The results aren’t updated with this option, but it shows in this example that there is more products than are displayed.
Disclaimer
Maje Media LLC cannot be held responsible for the functionality of this code. Please make sure you test it on a development site before adding the code to your production website. There is no support available for this (and other) code snippet(s) posted on this website. If you’d like Maje Media to do custom development to help with your custom implementation please send a contact request.
This Post Has 0 Comments