Created
July 5, 2020 17:14
-
-
Save DxDiagDx/8aaa3bf040db96bab73ccdab34cc5d88 to your computer and use it in GitHub Desktop.
WooCommerce: добавить произвольную вкладку
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Переименовать вкладки в карточке товара */ | |
add_filter( 'woocommerce_product_tabs', 'usota_woo_rename_tab', 98); | |
function usota_woo_rename_tab($tabs) { | |
$tabs['additional_information']['title'] = 'Характеристики'; | |
return $tabs; | |
} | |
add_filter('woocommerce_product_additional_information_heading', 'usota_product_additional_information_heading'); | |
function usota_product_additional_information_heading() { | |
echo '<h2>Характеристики</h2>'; | |
} | |
/* Удалить вкладку c атрибутами, если атрибутов нет */ | |
add_filter( 'woocommerce_product_tabs', 'delete_tab_additional_information', 98 ); | |
function delete_tab_additional_information( $tabs ) { | |
global $product; | |
$attributes = $product->get_attributes(); | |
if ( ! $attributes ) { | |
unset( $tabs['additional_information'] ); | |
} | |
return $tabs; | |
} | |
/* | |
* Создание вкладки в карточке товара WooCommerce бэкенд | |
*/ | |
add_filter('woocommerce_product_data_tabs', 'usota_new_tab_instructipons' ); | |
function usota_new_tab_instructipons ( $tabs ){ | |
$tabs[ 'tab_instructipons' ] = array( | |
'label' => 'Инструкции и сертификаты', | |
'target' => 'special_panel_instructipons', | |
'class' => array( 'class1', 'class2' ), | |
'priority' => 75, | |
); | |
return $tabs; | |
} | |
/* Вывод во вкладку текстового поля */ | |
add_action( 'woocommerce_product_data_panels', 'usota_added_tabs_panel' ); | |
function usota_added_tabs_panel() { | |
global $post; | |
?> | |
<div id="special_panel_instructipons" class="panel woocommerce_options_panel"> | |
<div class="options_group"> | |
<?php | |
wp_editor(get_post_meta( $post->ID, '_custom_desc', true ), 'custom_desc', array( | |
'wpautop' => 1, | |
'media_buttons' => 1, | |
'textarea_name' => 'custom_desc', | |
'textarea_rows' => 5, | |
'tabindex' => null, | |
'editor_css' => '<style>.quicktags-toolbar, .wp-editor-tools, .wp-editor-wrap, .wp-switch-editor {padding: 5px 10px;}</style>', | |
'editor_class' => 'form-field', | |
'teeny' => 0, | |
'dfw' => 0, | |
'tinymce' => 1, | |
'quicktags' => 1, | |
'drag_drop_upload' => false | |
) ); | |
?> | |
</div> | |
</div> | |
<?php | |
} | |
add_action( 'woocommerce_process_product_meta', 'art_woo_custom_fields_save', 10 ); | |
function art_woo_custom_fields_save( $post_id ) { | |
// Сохранение области текста | |
$woocommerce_textarea = $_POST['custom_desc']; | |
//if ( ! empty( $woocommerce_textarea ) ) { | |
if ( isset( $woocommerce_textarea ) ) { | |
update_post_meta( $post_id, '_custom_desc', $woocommerce_textarea ); | |
} | |
} | |
/* Добавляем новую вкладку в карточке товара фронтенд */ | |
add_filter('woocommerce_product_tabs','add_tabs'); | |
function add_tabs($tabs){ | |
$tabs['instructions'] = array( | |
'title' => 'Инструкции и сертификаты', | |
'priority' => 40, | |
'callback' => 'instructions_description_tab' | |
); | |
return $tabs; | |
}; | |
function instructions_description_tab() { | |
global $post; | |
$custom_desc = get_post_meta( $post->ID, '_custom_desc', true ); | |
echo '<h2>Инструкции и сертификаты</h2>'; | |
echo $custom_desc; | |
} | |
/* Удалить вкладку с инструкциями, если она пустая */ | |
add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 ); | |
function delete_tab( $tabs ) { | |
global $post; | |
$custom_desc = get_post_meta( $post->ID, '_custom_desc', true ); | |
if(empty($custom_desc)) { | |
unset( $tabs['instructions'] ); | |
} | |
return $tabs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment