В WordPress я создал роли пользователя «дилер» и «оптовые». Я добавил флажок в изменение продукта, чтобы выборочно скрыть или отобразить вариацию.// Here is where I add my custom user roles- Wholesaler and Dealer
function add_custom_roles() {
add_role(
'wholesaler',
__( 'Wholesaler' ),
get_role( 'customer' )->capabilities
);
add_role(
'dealer',
__( 'Dealer' ),
get_role( 'customer' )->capabilities
);
}
add_action( 'init', 'add_custom_roles' );
// Here i add custom fields to product variations
add_action( 'woocommerce_variation_options', 'add_custom_fields_to_variations', 10, 3 );
function add_custom_fields_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_checkbox(
array(
'id' => '_hide_variation_dealer',
'wrapper_class' => 'show_if_variation',
'label' => __('Hide for dealers', 'woocommerce' ),
'description' => '',
'value' => get_post_meta( $variation->ID, '_hide_variation_dealer', true ) ? 'yes' : 'no',
)
);
}
// Saving custom fields for product variations
add_action( 'woocommerce_save_product_variation', 'save_custom_fields_for_variations', 10, 2 );
function save_custom_fields_for_variations( $variation_id, $i ) {
$hide_variation_dealer = isset( $_POST['_hide_variation_dealer'][$i] ) ? 'yes' : 'no';
update_post_meta( $variation_id, '_hide_variation_dealer', $hide_variation_dealer );
}
// Here I filtering function to read custom field values - logged in and dealer.
add_filter( 'woocommerce_variation_is_visible', 'hide_specific_product_variation', 10, 4 );
function hide_specific_product_variation( $is_visible, $variation_id, $variable_product, $variation ) {
// Check if user is logged in and has dealer role
if( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( in_array( 'dealer', (array) $user->roles ) ) {
// For dealer user role, hide variations with the custom field set to 'yes'
$hide_variation_dealer = get_post_meta( $variation->ID, '_hide_variation_dealer', true );
if ( $hide_variation_dealer === 'yes' ) {
return false;
}
}
}
return $is_visible;
}
< /code>
Но вариация не скрыта. Я не могу найти, почему это не работает. Любая помощь ценится.
Подробнее здесь: https://stackoverflow.com/questions/779 ... -custom-fi
Скрыть конкретные вариации продукта WooCommerce от роли пользователя на основе пользовательского поля ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Отключить множественные вариации продукта условно на основе роли пользователя в WooCommerce
Anonymous » » в форуме Php - 0 Ответы
- 3 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Отключить множественные вариации продукта условно на основе роли пользователя в WooCommerce
Anonymous » » в форуме Php - 0 Ответы
- 3 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Отключить множественные вариации продукта условно на основе роли пользователя в WooCommerce
Anonymous » » в форуме Php - 0 Ответы
- 1 Просмотры
-
Последнее сообщение Anonymous
-