• Resolved igianni84

    (@igianni84)


    Hello everybody,

    an external wms sends the products to WooCommerce via API. The “brand” is passed on as a meta value.

    I need “brand” as an attribute, so I wrote the following function in the functions.php:

    add_action( 'save_post_product', 'get_meta_set_attribute' );
    function get_meta_set_attribute( $id_prodotto ) {
    	$tassonomia = "pa_marca";
    	
    	error_log(print_r($id_prodotto, true));
    	$brand = get_post_meta($id_prodotto, 'meta_product_producer', true);
    	error_log("Il brand è: " . $brand);
    	
    	$attributi_prodotto = get_post_meta( $id_prodotto, '_product_attributes', true);
    	$posizione = count($attributi_prodotto);
    	
    	error_log(print_r($attributi_prodotto, true));
    	
    	$attributi_prodotto[$tassonomia] = array(
    		'name' => $tassonomia,
    		'value' => $brand,
    		'position' => $posizione,
    		'is_visible' => 1,
    		'is_variation' => 0,
    		'is_taxonomy' => 1
    	);
    	
    	error_log(print_r($attributi_prodotto, true));
    	
    	try {
    		$result = update_post_meta($id_prodotto, '_product_attributes', $attributi_prodotto); 
    	} catch (Exception $e) {
          echo("<br />Errore: " . ($e->getMessage()));
      	}
    	
    	error_log("Il risultato della richiesta di aggiornamento è: " . $result);
    }

    Unfortunately there are two problems:

    1. When I print the attributes already present in the product (size and color), it returns them to me without values … even if these exist within the product (see screens -> https://imgur.com/a/iQnGKue)

    2. The update_post_meta function is executed successfully (I do not receive any errors), but the “brand” attribute (in Italian: marca) is not added to the product.

    Can you help me to undestrood the problem? thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi @igianni84 ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Thread Starter igianni84

    (@igianni84)

    Thanks for the reply Gabriel. I also asked in the community on facebook hoping to receive feedback. Hope to get it here too ??

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic. ??

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with add attributes via update_post_meta’ is closed to new replies.