В 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
Кемеровские программисты php общаются здесь
1748284010
Anonymous
В 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>
Но вариация не скрыта. Я не могу найти, почему это не работает. Любая помощь ценится.
Подробнее здесь: [url]https://stackoverflow.com/questions/77992546/hide-specific-woocommerce-product-variations-from-user-role-based-on-a-custom-fi[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия