0
我已经设置了Woocommerce与变量产品。基于用户角色的Woocommerce产品变体
这些产品的属性可能有差异,可能的值为1kg,2kg和5kg。我也创建了一个“Aziende”用户组。我想要一些产品变体只显示“Aziende”客户。我不希望这些变化对其他客户可见。
例如:Aziende客户可以看到选项“1kg,2kg,5kg”,而其他客户角色只能看到1kg选项。
这是可能的Woocommerce?
我已经设置了Woocommerce与变量产品。基于用户角色的Woocommerce产品变体
这些产品的属性可能有差异,可能的值为1kg,2kg和5kg。我也创建了一个“Aziende”用户组。我想要一些产品变体只显示“Aziende”客户。我不希望这些变化对其他客户可见。
例如:Aziende客户可以看到选项“1kg,2kg,5kg”,而其他客户角色只能看到1kg选项。
这是可能的Woocommerce?
是的。如果您覆盖文件woocommerce/templates/single-product/add-to-cart/variable.php
,则可以找到变体选择框的代码。
在那里,你可以这样做:
function user_has_role($role, $user_id = null) {
if (is_numeric($user_id))
$user = get_userdata($user_id);
else
$user = wp_get_current_user();
if (empty($user))
return false;
return in_array($role, (array) $user->roles);
}
所以它可以作为:
if(user_has_role("Aziende")){
//do stuff
}
现在
首先与角色的工作时,我总是包含这个片段有这个功能,并知道哪个模板可以改变你应该能够在文件中做些什么。这可能是沿着这条:
// Get terms if this is a taxonomy - ordered
if (taxonomy_exists($name)) {
$terms = wc_get_product_terms($post->ID, $name, array('fields' => 'all'));
foreach ($terms as $term) {
if (! in_array($term->slug, $options)) {
continue;
}
if($name == 'pa_weight' && $term->slug != '1kg') { // or whatever your attribute is called, and whatever the attribute term is called.
if(!user_has_role('aziende'){
continue;
}
}
echo '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($selected_value), sanitize_title($term->slug), false) . '>' . apply_filters('woocommerce_variation_option_name', $term->name) . '</option>';
}
} else {
foreach ($options as $option) {
if($name == 'pa_weight' && $option != '1kg') { // or whatever your attribute is called, and whatever the attribute term is called.
if(!user_has_role('aziende'){
continue;
}
}
echo '<option value="' . esc_attr(sanitize_title($option)) . '" ' . selected(sanitize_title($selected_value), sanitize_title($option), false) . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>';
}
}
此代码是不测试,所以我不知道它是否工作。但它应该给你一个正确方向的指针。