Role-based variable products
<p>I have a WooCommerce store selling variable products. <br /><br />Example: Blow Dart Variable Options: 4 in a box for a single item. <br /><br />I'm trying to make variable products not show the single item option when logged in. I've gone as far as hiding the option label text, but not completely removing the option. <br /><br />Variable options</p><p><br /></p>
<pre class="brush:php;toolbar:false;">add_filter('woocommerce_variation_option_name', 'custom_hide_single_item_option', 10, 1);
function custom_hide_single_item_option($term_name)
{
// Get the current user's roles
$user = wp_get_current_user();
$user_roles = (array)$user->roles;
// Define the roles to exclude
$roles_to_exclude = array('reseller', 'reseller 1', 'administrator');
// If the user has any of the excluded roles, remove the "Single Item (1pc)" option
if (array_intersect($user_roles, $roles_to_exclude) && $term_name === 'Single Item (1pc)') {
return false;
}
return $term_name;
}</pre>
<p>I've provided the PHP snippet I'm using and attached a screenshot of the result of that snippet. </p>