Firstly let me make it clear I have very little to no coding experience whatsoever, so here goes.
I am trying to create a custom PHP code for Woocommerce, the aim is to automatically assign products to product categories by matching a set keyword string that would be found in product titles. I would like this to execute either when importing, updating, changing, or saving a product.
I have found other methods of achieving this however I have not found a way to simply add object_terms.
Here is the code I have created from other codes I already have for other purposes, I have tried to edit the code to meet my requirement. Here is what I have compiled so far.
add_action( 'woocommerce_product_quick_edit_save', 'get_cat_from_title',150,1 );
// On admin single product creation/update
add_action( 'woocommerce_process_product_meta', 'get_cat_from_title',100,1);
// On admin single product variation creation/update
add_action( 'woocommerce_save_product_variation', 'get_cat_from_title' ,100,1);
add_action( 'save_post_product', 'get_cat_from_title' ,100,1);
add_action( 'save_product_category', 'get_cat_from_title' ,100,1);
// Update below // function get_cat_from_title($title,$defaultcat) {
// function get_cat_from_title($title) {
function get_cat_from_title( $product_id) {
// get an instance of the WC_Product Object
if (is_object($product_id)){
$product = $product_id;
$product_id=$product->get_id();
}else{
$product = wc_get_product( $product_id );
}
// keyword in title - 'test1'
if (strpos($title,'test1') !== false){
// category for found keyword 'test1'
$cat = "test1";
// category for found keyword 'test2'
} elseif(strpos($title,"test2") !== false);{
$cat = "test2";
switch ($cat) {
// assign category test1
case 'test1':
wp_add_object_terms ( $product_id,'test1', 'product_cat' );
break;
// assign category test2
case 'test2':
wp_add_object_terms ( $product_id,'test2', 'product_cat' );
break;
default:
# code...
break;
}
}
}
Unfortunately, the code is not working for me and throws the following errors inside my debug.log.
[27-Jan-2021 12:25:51 UTC] PHP Notice: Trying to get property 'data' of non-object in /home/pub/domain.com/wp-content/plugins/bdthemes-element-pack/base/element-pack-base.php on line 471
[27-Jan-2021 12:25:54 UTC] PHP Notice: Trying to get property 'data' of non-object in /home/pub/domain.com/wp-content/plugins/bdthemes-element-pack/base/element-pack-base.php on line 471
[27-Jan-2021 12:26:09 UTC] PHP Notice: Trying to get property 'data' of non-object in /home/pub/domain.com/wp-content/plugins/bdthemes-element-pack/base/element-pack-base.php on line 471
[27-Jan-2021 12:26:10 UTC] PHP Notice: Trying to get property 'data' of non-object in /home/pub/domain.com/wp-content/plugins/bdthemes-element-pack/base/element-pack-base.php on line 471
[27-Jan-2021 12:26:11 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function get_cat_from_title(), 1 passed in /home/pub/domain.com/wp-includes/class-wp-hook.php on line 289 and exactly 2 expected in /home/pub/domain.com/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:8
Stack trace:
#0 /home/pub/domain.com/wp-includes/class-wp-hook.php(289): get_cat_from_title(443439)
#1 /home/pub/domain.com/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#2 /home/pub/domain.com/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#3 /home/pub/domain.com/wp-includes/post.php(4298): do_action('save_post_produ...', 443439, Object(WP_Post), true)
#4 /home/pub/domain.com/wp-includes/post.php(4411): wp_insert_post(Array, false, true)
#5 /home/pub/domain.com/wp-a in /home/pub/domain.com/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 8
[27-Jan-2021 12:26:24 UTC] PHP Notice: Trying to get property 'data' of non-object in /home/pub/domain.com/wp-content/plugins/bdthemes-element-pack/base/element-pack-base.php on line 471
[27-Jan-2021 12:26:25 UTC] PHP Notice: Trying to get property 'data' of non-object in /home/pub/domain.com/wp-content/plugins/bdthemes-element-pack/base/element-pack-base.php on line 471
Admin usually tells me off for not writing my questions clearly or correctly, I am not a developer I do not understand all of the jargon however I really do hope that I have posted this correctly and explained my goal clearly enough! Maybe someone can tell me where I am going wrong and how to make this code work with some edits. I am open to comments and always willing to learn. Thank you :)
question from:
https://stackoverflow.com/questions/65919528/get-woocommerce-to-automatically-assigns-product-categories-using-keyword-in-pro