Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter simotro

    (@simotro)

    Thank you!!!

    Thread Starter simotro

    (@simotro)

    In the end I used this code in function.php to autofill my woocommerce products short-description and tags by custom attributes:

    function autofill_desc_and_tags( $post_id ) {
    	global $post;
    	global $wpdb;
    	
    	$attribute_names_for_tags = array( 'pa_marca', 'pa_modello', 'pa_tipologia', 'pa_tipo-prodotto', 'pa_codice-oem', 'pa_codice-concorrenza', 'pa_stato' ); #get the taxonomies for autofill tags
     
    	foreach ( $attribute_names_for_tags as $attribute_name ) {
    		$taxonomy = get_taxonomy( $attribute_name );
     
    		if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
    			$terms = wp_get_post_terms( $post->ID, $attribute_name );
    			$terms_array = array();
     
    			if ( ! empty( $terms ) ) {
    				foreach ( $terms as $term ) {
    					$archive_link = get_term_link( $term->slug, $attribute_name );
    					$full_line = $term->name;
    					array_push( $terms_array, $full_line );
    				}
    				$tags .= implode( $terms_array, ', ' ).',';
    			}
    		}
    	}
    	wp_set_object_terms($post_id, $tags, 'product_tag');
     
    	$attribute_names_for_desc = array( 'pa_marca', 'pa_modello', 'pa_codice-oem', 'pa_codice-concorrenza' ); #get the taxonomies for autofill short-description
     
    	foreach ( $attribute_names_for_desc as $attribute_name ) {
    		$taxonomy = get_taxonomy( $attribute_name );
     
    		if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
    			$terms = wp_get_post_terms( $post->ID, $attribute_name );
    			$terms_array = array();
     
    			if ( ! empty( $terms ) ) {
    				foreach ( $terms as $term ) {
    					$archive_link = get_term_link( $term->slug, $attribute_name );
    					$full_line = $term->name;
    					array_push( $terms_array, $full_line );
    				}
    				$short_desc .= ucwords(ltrim(str_replace('-', ' ', $taxonomy->name), 'pa_')) . ': ' . implode( str_replace('>', '>', $terms_array), ', ' ).'\n';
    			}
    		}
    	}   
        $wpdb->query( $wpdb->prepare("UPDATE iri_posts 
                    SET post_excerpt = '$short_desc'
    				WHERE ID = $post_id")
        );
    }
    add_action( 'woocommerce_process_product_meta', 'autofill_desc_and_tags' );

    Thank you all for your attention and suggestions!

    • This reply was modified 5 years, 3 months ago by simotro.
    Thread Starter simotro

    (@simotro)

    Hi, shure, that’s the code:

    `//CREATE BUTTON IN THE BACKEND FOR AUTOFILL SHORT DESCRIPTION AND TAGS
    add_action( ‘post_submitbox_misc_actions’, function() { #add a button near the publish/update button
    global $post;

    $attribute_names_for_tags = array( ‘pa_marca’, ‘pa_modello’, ‘pa_tipologia’, ‘pa_tipo-prodotto’, ‘pa_codice-oem’, ‘pa_codice-concorrenza’, ‘pa_stato’ ); #get the taxonomies for autofill tags

    foreach ( $attribute_names_for_tags as $attribute_name ) {
    $taxonomy = get_taxonomy( $attribute_name );

    if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
    $terms = wp_get_post_terms( $post->ID, $attribute_name );
    $terms_array = array();

    if ( ! empty( $terms ) ) {
    foreach ( $terms as $term ) {
    $archive_link = get_term_link( $term->slug, $attribute_name );
    $full_line = $term->name;
    array_push( $terms_array, $full_line );
    }
    $tags .= implode( $terms_array, ‘, ‘ ).’,’;
    }
    }
    }

    $attribute_names_for_desc = array( ‘pa_marca’, ‘pa_modello’, ‘pa_codice-oem’, ‘pa_codice-concorrenza’ ); #get the taxonomies for autofill short-description

    foreach ( $attribute_names_for_desc as $attribute_name ) {
    $taxonomy = get_taxonomy( $attribute_name );

    if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
    $terms = wp_get_post_terms( $post->ID, $attribute_name );
    $terms_array = array();

    if ( ! empty( $terms ) ) {
    foreach ( $terms as $term ) {
    $archive_link = get_term_link( $term->slug, $attribute_name );
    $full_line = $term->name;
    array_push( $terms_array, $full_line );
    }
    $short_desc .= ucwords(ltrim(str_replace(‘-‘, ‘ ‘, $taxonomy->name), ‘pa_’)) . ‘: ‘ . implode( $terms_array, ‘, ‘ ).'<br>’;
    }
    }
    }

    ?>
    <div class=”options_group”>
    <p class=”form_field” style=”text-align:center;”>
    <input type=”button” class=”button button-primary” value=”AUTOFILL DESCRIZIONE BREVE & TAGS” onclick=”document.getElementById(‘new-tag-product_tag’).value='<?php echo substr($tags, 0, strlen($tags)-1); ?>’;document.querySelector(‘.tagadd’).click();document.getElementById(‘excerpt_ifr’).contentWindow.document.getElementById(‘tinymce’).innerHTML='<?php echo $short_desc; ?>’;”>
    </p>
    </div>
    <?php
    } );

Viewing 3 replies - 1 through 3 (of 3 total)