Title: Executing function when product category or title change
Last modified: January 1, 2023

---

# Executing function when product category or title change

 *  Resolved [termi](https://wordpress.org/support/users/termi/)
 * (@termi)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/)
 * I need help with two cases 
   1. how to execute function when product categories
   get added to product2. how to execute function when product title changes

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

 *  [Faisal Ahammad](https://wordpress.org/support/users/faisalahammad/)
 * (@faisalahammad)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/#post-16333671)
 * Hi [@termi](https://wordpress.org/support/users/termi/)
 * To execute a function when a product category is added to a product in WooCommerce,
   you can use the `wp_set_object_terms` hook. This hook is called when terms are
   set for a specific object (in this case, a product). You can use it to trigger
   a function when a product’s categories are updated.
 * **Here is an example of how you can use this hook:**
 *     ```
       function my_function_when_product_category_added( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
          // your code here
       }
   
       add_action( 'wp_set_object_terms', 'my_function_when_product_category_added', 10, 6 );
       ```
   
 * To execute a function when a product title changes in WooCommerce, you can use
   the `wp_insert_post` hook. This hook is called when a post is created or updated,
   so you can use it to trigger a function when a product’s title is updated.
 * **Here is an example of how you can use this hook:**
 *     ```
       function my_function_when_product_title_changed( $post_ID, $post, $update ) {
          // your code here
       }
   
       add_action( 'wp_insert_post', 'my_function_when_product_title_changed', 10, 3 );
       ```
   
 * These hooks should be placed in your child theme’s functions.php file or in a
   custom plugin.
 * I hope this helps! Let me know if you have any questions or need further assistance.
 *  Thread Starter [termi](https://wordpress.org/support/users/termi/)
 * (@termi)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/#post-16333704)
 * Thank you for your answers. From what I understand, in the case of `wp_insert_post`,
   this function will be triggered every time a post is saved. Is there any other
   way to execute this function without it taking a long time and causing delays
   every time a post is saved?
 *  Thread Starter [termi](https://wordpress.org/support/users/termi/)
 * (@termi)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/#post-16333757)
 * Oh and I have one more problem
 *     ```wp-block-code
       add_action( 'wp_set_object_terms', 'my_function_when_product_category_added', 10, 6 );
       ```
   
 * This function is triggered when any therm is added not just category and function
   it is executing adds tags to this post triggering new execution of function making
   big unnecessary loops.
   Is ther any way so this hook is trigered only by categories
   not by tags or custom taxonomy.
    -  This reply was modified 3 years, 5 months ago by [termi](https://wordpress.org/support/users/termi/).
 *  [Igor H](https://wordpress.org/support/users/ihereira/)
 * (@ihereira)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/#post-16335210)
 * Hello,
 * `it is executing adds tags to this post triggering new execution of function 
   making big unnecessary loops.`
 * Since this is a 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](https://developer.woocommerce.com/)
   for resources on developing for WooCommerce.
 * You can also visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
 * Thanks.
 *  [Faisal Ahammad](https://wordpress.org/support/users/faisalahammad/)
 * (@faisalahammad)
 * [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/#post-16335970)
 * Hi [@termi](https://wordpress.org/support/users/termi/)
 * It is impossible to use JavaScript to directly monitor changes to a product’s
   title or categories on a WooCommerce website, as the code would not have access
   to the server-side data stored in the database. However, when the product’s title
   or categories are updated, you can use JavaScript to detect changes to the DOM(
   Document Object Model). For instance, you can use JavaScript to listen for changes
   to the elements that display the product’s title or categories on the page and
   then trigger an alert or some other action when those elements are modified.
 * Here is an example of how you could use JavaScript to detect changes to the product’s
   title element and trigger an alert:
 *     ```
       // Select the product title element
       const productTitle = document.querySelector('.product_title');
   
       // Create a new MutationObserver to listen for changes to the product title element
       const observer = new MutationObserver(function(mutations) {
           // Loop through the list of mutations
           mutations.forEach(function(mutation) {
               // Check if the text content of the product title element has changed
               if (mutation.type === 'characterData') {
                   // Trigger the alert if the text content has changed
                   alert('The product title has changed!');
               }
           });
       });
   
       // Start observing the product title element
       observer.observe(productTitle, {
           characterData: true,
           subtree: true
       });
       ```
   
 * This code will create a MutationObserver that listens for changes to the product
   title element and will trigger an alert when the element’s text content is changed.
 * You can use a similar approach to listen for product category changes. Select
   the element that displays the categories, and use a MutationObserver to listen
   for changes to the element’s content.
 * **Issue 2:**
 * Yes, you can use the `$taxonomy` parameter passed to the `wp_set_object_terms`
   hook function to check the taxonomy of the added terms.
 * You can modify your hook function only to execute your code if the taxonomy is‘
   product_cat’, like this:
 *     ```
       function my_function_when_product_category_added( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
           if ( 'product_cat' === $taxonomy ) {
               // your code here
           }
       }
       add_action( 'wp_set_object_terms', 'my_function_when_product_category_added', 10, 6 );
       ```
   
 * This will ensure that your function is only triggered when categories are added
   to the product rather than when any terms are added.
 * I hope this helps! Let me know if you have any other questions.

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

The topic ‘Executing function when product category or title change’ is closed to
new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 5 replies
 * 3 participants
 * Last reply from: [Faisal Ahammad](https://wordpress.org/support/users/faisalahammad/)
 * Last activity: [3 years, 5 months ago](https://wordpress.org/support/topic/executing-function-when-product-category-or-title-change/#post-16335970)
 * Status: resolved