Problem with add attributes via update_post_meta
-
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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Problem with add attributes via update_post_meta’ is closed to new replies.