• Resolved simotro

    (@simotro)


    Hi everyone, I’m looking for a way to save the attributes before updating the product. In this way, using the get_taxonomy() function, I could retrieve them and use a javascript button to autofill the description field and the tags. Currently it only works if I update the product first, even if the product is already in the database and has its ID, but it really takes a lot of time and I’d rather be able to do it without the update. Has anyone tried it successfully before me?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    Can you show us your code of what you have currently?

    Kind regards,

    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
    } );

    Plugin Support con

    (@conschneider)

    Engineer

    How do you currently try to save the values you get?

    My first approach would be to save my values as post meta: update_post_meta coupled with an ajax call to avoid reloading.

    However digging into core, why not make the call: https://github.com/woocommerce/woocommerce/blob/a871d36380bb4cfd4f94bf952af754301433d67b/includes/class-wc-ajax.php#L556-L574 – possibly prepared by using: https://github.com/woocommerce/woocommerce/blob/18873ac9a7372c75eea6eee2d49e65c64a0e027d/includes/admin/meta-boxes/class-wc-meta-box-product-data.php#L217-L269

    For custom data you can try and check out this post: https://remicorson.com/mastering-woocommerce-products-custom-fields/

    Kind regards,

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Howdy!

    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.

    Cheers!

    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.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Save attributes befor update product’ is closed to new replies.