Hello i been searching for a while now but i cant get this to work, i cant get the category by name, and also i cant asign it to a product programatically

$product = new WC_Product(4);
write_log($product->get_title());

// Gets term object from Tree in the database. 
$category = get_term_by( 'name', 'bolsas', 'product_cat' );
echo "Cat: ".$category->name;

wp_set_object_terms($product->id, $cat->term_id, 'product_cat');

Im running this on functions.php file and reloading the page, i also have a function to log in console for debugging, but its getting me nowhere.

Please help!

Read more here: Get woocommerce category by name and asign to product


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: