I want to check the category of a WooCommerce product post right after it's created(or updated) and then run some more code based on the category.
To check post on creation/update I used save_post
and for category has_category
. Something goes wrong with has_category
and it doesn't return anything at all. I tried replacing $post_id
with $post
and $post->ID
as suggested in other issues but that didn't change anything.
function doFruitStuff($post_id){ // Function in functions.php
$fruits = 'fruits';
if(has_category($fruits, $post_id)){
echo "<script type='text/javascript'>alert('has the category');</script>";
}else{
echo "<script type='text/javascript'>alert('doesnt have the category');</script>";
}}
add_action('save_post', 'doFruitStuff');
Am I using has_category
incorrectly or WooCommerce product categories work differently?
I'm used to debugging in javascript alerts, sorry about that. Any help greatly appreciated.