我用可变产品设置了Woocommerce .
这些产品的属性都有变化,可能的值为1kg,2kg和5kg .
我还创建了一个“Aziende”用户组 . 我想要一些产品变化仅显示“Aziende”客户 . 我不希望其他客户看到这些变化 .
例如:Aziende客户看到选项“1kg,2kg,5kg”,而其他客户角色只能看到1kg选项 .
这在Woocommerce中可行吗?
是 . 如果您覆盖文件 woocommerce/templates/single-product/add-to-cart/variable.php ,则会找到变体选择框的代码 .
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>'; } }
这段代码经过了测试,所以我不知道它是否有效 . 但它应该给你一个正确方向的指针 .
1 回答
是 . 如果您覆盖文件
woocommerce/templates/single-product/add-to-cart/variable.php
,则会找到变体选择框的代码 .在那里你可以做类似的事情:
首先,在处理角色时,我总是包含此代码段:
所以它可以用作:
现在有了这个功能,知道要更改哪个模板,你应该可以在该文件中做一些事情 . 它可能是这样的:
这段代码经过了测试,所以我不知道它是否有效 . 但它应该给你一个正确方向的指针 .