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 product
2. how to execute function when product title changes
-
Hi @termi
To execute a function when a product category is added to a product in WooCommerce, you can use the
wp_set_object_termshook. 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_posthook. 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.
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?Oh and I have one more problem
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, 4 months ago by
termi.
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 for resources on developing for WooCommerce.
You can also visit the WooCommerce Facebook group or the
#developerschannel of the WooCommerce Community Slack.Thanks.
Hi @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
$taxonomyparameter passed to thewp_set_object_termshook 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.
-
This reply was modified 3 years, 4 months ago by
The topic ‘Executing function when product category or title change’ is closed to new replies.