Skip to content

Instantly share code, notes, and snippets.

@renrax
Forked from artikus11/functions.php
Created January 10, 2022 15:14
Show Gist options
  • Save renrax/a72636385773b8df5cd001793f7bd039 to your computer and use it in GitHub Desktop.
Save renrax/a72636385773b8df5cd001793f7bd039 to your computer and use it in GitHub Desktop.
Добавляем поле описания в форму редактирования атрибута
// Добавляем поле описания в форму редактирования атрибута
add_action( 'woocommerce_after_edit_attribute_fields', 'scf_atts_fields', 20 );
function scf_atts_fields() {
$term_descr = get_term_meta( $_GET['edit'], 'descr', true ) ? get_term_meta( $_GET['edit'], 'descr', true ) : '';
?>
<tr class="form-field form-required">
<th scope="row" valign="top">
<label>Описание атрибута</label>
</th>
<td>
<textarea name="att_descr" id="att_descr" cols="30" rows="10"><?php echo $term_descr; ?></textarea>
<p class="description">Показывается на сайте при наведении на знак вопроса</p>
</td>
</tr>
<?php
}
// Сохраняем описание в терммету
add_action( 'woocommerce_attribute_updated', 'scf_save_atts_meta', 20 );
function scf_save_atts_meta( $term_id ) {
update_term_meta( $term_id, 'descr', $_POST['att_descr'] );
}
@renrax
Copy link
Author

renrax commented Jan 10, 2022


// 1. Отображение поля описания на странице "Добавления нового атрибута" / Display field on "Add new attribute" admin page

add_action( 'woocommerce_after_add_attribute_fields', 'scf_atts_fields_add', 10, 2 );
function scf_atts_fields_add() {
    ?>
        <div class="form-field term-description-wrap">
            <label for="attdescr"><?php echo __( 'Description', 'woocommerce' ); ?></label>
            
            <textarea name="attdescr" id="attdescr" cols="40" rows="5"><?php echo $term_descr; ?></textarea>
            
			<p class="description"><?php echo __( 'This is the description that is placed in the tooltip in the attribute table', 'woocommerce' ); ?></p>
        </div>
    <?php
}

// ---------------
// 2. Отображение поля описания на странице "Редактирование атрибута" / Display field on "Edit attribute" @ admin page

add_action( 'woocommerce_after_edit_attribute_fields', 'scf_atts_fields_edit', 20 );
function scf_atts_fields_edit($term) {
	$term_descr = get_term_meta( $_GET['edit'], 'attdescr', true ) ? get_term_meta( $_GET['edit'], 'attdescr', true ) : '';
	?>
	
	
	<tr class="form-field term-description-wrap">
	    <th scope="row" valign="top">
	        <label for="att-descr"><?php echo __( 'Description', 'woocommerce' ); ?></label>
	   </th>
        <td>
			<textarea name="attdescr" id="attdescr" cols="50" rows="5"><?php echo $term_descr; ?></textarea>
       
            <p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
        </td>
	</tr>
	<?php
}

// ---------------
// 3. Сохраняем поле описания в терммету / Save field @ admin page
 
add_action( 'woocommerce_attribute_added', 'scf_save_atts_meta', 10, 3 );
add_action( 'woocommerce_attribute_updated', 'scf_save_atts_meta', 20 );

function scf_save_atts_meta( $term_id ) {
    if ( isset( $_POST['attdescr'] )) {
	update_term_meta( $term_id, 'attdescr', esc_attr( $_POST['attdescr'] ) );
    }
}

// ---------------
// 4. Добавить Шорткод для отображения поля описания / Add shortcode for display field

/*add_shortcode( $attdescshow, $scf_atts_fields_show );

function scf_atts_fields_show( $atts ) {
    
    $term = get_queried_object();
      if ( $term && ! empty( get_term_meta( $term->term_id, 'attdescr', true ) ) ) {
         echo '<p class="term-description">' . wc_format_content( htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'attdescr', true ) ) ) . '</p>';
          
      }
}*/

/* Use shortcode
[attdescshow field="attdescr"]*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment