Trigger custom code after inserting a new product using the REST API
-
Hi,
I have looked everywhere on the internet and in this forum but I couldn’t find a proper solution for my problem.
I have this action
add_action('save_post_product', 'add_brand_to_product_when_creating_a_product', 10, 3); function add_brand_to_product_when_creating_a_product( $post_id, $post, $update ) { if ( get_post_type( $post_id ) == 'product' ) { // we've been editing a product $product = wc_get_product( $post_id ); add_brand_to_product_if_missing($post_id); } }This code works when I add the product using the WordPress dashboard (admin area), but it doesn’t work when I create/add a product using the endpoint POST /wp-json/wc/v3/products
Which action/hook do I have to use to trigger a specific code after adding/creating a product using the REST API?
The custom code add_brand_to_product_if_missing I am trying to execute is working with terms. It actually calls the function
wp_set_object_terms($product_id, $productBrand,'product_brand');What I am trying to do is the following:
I have desktop application which call WooCommerce API to create products. The POST request has the product data including a product attribute “pa_brand”. For the brands in WooCommerce, I use the plugin “WooCommerce Brands“. Instead of assigning the brands manually to each product, I want to trigger a code which automatically takes the value of the “pa_brand” and save it as a brand (product_brand) and assign it to the product. As I have already mentioned, the code above works fine, when a product is added from withing the wordpress dashboard, but it doesn’t work when the product is added via REST API!I hope I could explain my situation well 🙂
Thank you very much for your time and attention 🙂
The topic ‘Trigger custom code after inserting a new product using the REST API’ is closed to new replies.