[ad_1]
When I try to insert a database entry in the term_relationships table as soon as a product is saved, I cannot use the product ID. When I use any other number, I can insert an entry but as soon as I try to use the product ID of the product created, nothing is inserterted. Am i doing something wrong or why can’t i insert a new relationship?
<?php
add_action('woocommerce_new_product', 'sync_on_product_save', 10, 1);
add_action('woocommerce_update_product', 'sync_on_product_save', 10, 1);
function sync_on_product_save($product_id)
{
global $wpdb;
global $current_user;
global $product;
get_currentuserinfo();
$product = wc_get_product($product_id);
$product_id = $product->get_id();
$int = (int)filter_var($product_id, FILTER_SANITIZE_NUMBER_INT);
$user_id = $current_user->ID;
$term = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->termmeta WHERE user_id = %d", $user_id));
$term_id = $term->term_id;
$term_tax = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id));
$term_tax_id = $term_tax->term_taxonomy_id;
$termmeta = $wpdb->prefix . 'term_relationships';
$wpdb->insert($termmeta, array(
"object_id" => $int,
"term_taxonomy_id" => $term_tax_id,
"term_order" => '0'
));
}
?>
[ad_2]