2015-04-02 54 views
0

我已經設置了Woocommerce與變量產品。基於用戶角色的Woocommerce產品變體

這些產品的屬性可能有差異,可能的值爲1kg,2kg和5kg。我也創建了一個「Aziende」用戶組。我想要一些產品變體只顯示「Aziende」客戶。我不希望這些變化對其他客戶可見。

例如:Aziende客戶可以看到選項「1kg,2kg,5kg」,而其他客戶角色只能看到1kg選項。

這是可能的Woocommerce?

回答

0

是的。如果您覆蓋文件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>'; 
    } 
} 

此代碼是測試,所以我不知道它是否工作。但它應該給你一個正確方向的指針。