Role-based variable products
P粉174151913
2023-08-06 13:06:26
<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>
I don't quite understand/see the problem in the description and/or pictures. All I see is a drop down menu with the option to buy 4. Do you want to remove the drop-down menu? In this case, I don't think the problem lies with PHP?