Last active
January 12, 2022 19:28
-
-
Save artikus11/1efc812e8e117f546e326d5771340591 to your computer and use it in GitHub Desktop.
Добавляем поле описания в форму редактирования атрибута и выводим само описание на странице товаров под тултипом
This file contains hidden or 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_action( 'woocommerce_after_add_attribute_fields', 'art_added_field_description', 20 ); | |
add_action( 'woocommerce_after_edit_attribute_fields', 'art_added_field_description', 20 ); | |
function art_added_field_description() { | |
$attribute_description = ''; | |
if ( ! empty( $_GET['edit'] ) ) { | |
$attribute_description = get_term_meta( $_GET['edit'], 'description', true ) ? : ''; | |
} | |
?> | |
<div class="form-field"> | |
<tr class="form-field form-required"> | |
<th scope="row" valign="top"> | |
<label>Описание атрибута</label> | |
</th> | |
<td> | |
<textarea | |
name="attribute_description" | |
id="attribute_description" | |
cols="30" rows="10"><?php echo wp_kses_post( $attribute_description ); ?></textarea> | |
<p class="description">Показывается на сайте при наведении на знак вопроса</p> | |
</td> | |
</tr> | |
</div> | |
<?php | |
} | |
// Сохраняем описание в терммету | |
add_action( 'woocommerce_attribute_added', 'art_save_field_description', 20 ); | |
add_action( 'woocommerce_attribute_updated', 'art_save_field_description', 20 ); | |
function art_save_field_description( $term_id ) { | |
if ( isset( $_POST['attribute_description'] ) ) { | |
update_term_meta( $term_id, 'description', sanitize_textarea_field( $_POST['attribute_description'] ) ); | |
} | |
} | |
// Добавляем данные в массив атрибутов | |
add_filter( 'woocommerce_display_product_attributes', function ( $product_attributes, $product ) { | |
$attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' ); | |
foreach ( $attributes as $attribute ) { | |
if ( $attribute->is_taxonomy() ) { | |
$attribute_name = sprintf( 'attribute_%s', sanitize_title_with_dashes( $attribute->get_name() ) ); | |
$product_attributes[ $attribute_name ]['description'] = get_term_meta( $attribute->get_id(), 'description', true ) ? : ''; | |
} | |
} | |
return $product_attributes; | |
}, 10, 2 ); |
This file contains hidden or 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
<?php | |
/** | |
* Product attributes | |
* | |
* Used by list_attributes() in the products class. | |
* | |
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-attributes.php. | |
* | |
* HOWEVER, on occasion WooCommerce will need to update template files and you | |
* (the theme developer) will need to copy the new files to your theme to | |
* maintain compatibility. We try to do this as little as possible, but it does | |
* happen. When this occurs the version of the template file will be bumped and | |
* the readme will list any important changes. | |
* | |
* @see https://docs.woocommerce.com/document/template-structure/ | |
* @package WooCommerce\Templates | |
* @version 3.6.0 | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
if ( ! $product_attributes ) { | |
return; | |
} | |
?> | |
<style> | |
.tooltip { | |
position: relative; | |
display: inline-block; | |
} | |
.tooltip .tooltiptext { | |
visibility: hidden; | |
text-align: center; | |
border-radius: 3px; | |
position: absolute; | |
color: #4a4a4a; | |
line-height: 1.5; | |
font-size: 0.725em; | |
font-weight: inherit; | |
box-shadow: 0 0 2px 1px #8a8a8a; | |
bottom: 40%; | |
transform: translate(-50%, 0); | |
width: 390px; | |
min-height: 40px; | |
max-height: 340px; | |
padding: 16px; | |
margin: 10px 0 16px; | |
background: #fff; | |
word-wrap: break-word; | |
overflow: hidden; | |
overflow-y: auto; | |
z-index: 5; | |
} | |
.tooltip:hover .tooltiptext { | |
visibility: visible; | |
} | |
.tooltip .question-sign{ | |
width: 18px; | |
height: 18px; | |
border-radius: 50%; | |
border: 1px solid; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
font-size: 0.825em; | |
} | |
</style> | |
<table class="woocommerce-product-attributes shop_attributes"> | |
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?> | |
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>"> | |
<th class="woocommerce-product-attributes-item__label"> | |
<?php echo wp_kses_post( $product_attribute['label'] ); ?> | |
<?php if ($product_attribute['description']):?> | |
<div class="tooltip"> | |
<span class="question-sign">?</span> | |
<span class="tooltiptext"><?php echo wp_kses_post( apply_filters( 'the_content', $product_attribute['description'] ) ); ?></span> | |
</div> | |
<?php endif;?> | |
</th> | |
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td> | |
</tr> | |
<?php endforeach; ?> | |
</table> |
Доброго! Да, для вывода придется файл менять
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Здравствуйте Артем,
правильно ли я понимаю, что для того, чтобы вывести подсказку с описанием атрибута нужно изменить файл product-attributes.php ?