I created this woocommerce function that I inserted into the theme’s functions.php file. I want to take the term “value” referring to the “name” of a specific attribute of a single product and insert it as the brand term of that product ID.
The following code doesn’t work, it doesn’t perform the intended function. How to do? Thank you
// Check if the product is a WooCommerce product
if (get_post_type($product_id) !== 'product') {
return;
}
$product = wc_get_product($product_id);
if (!$product->has_attributes()) {
return;
}
$attributes = $product->get_attributes();
foreach ($attributes as $attribute) {
if ($attribute->get_name() === 'Manufacturer') {
$brand_value = $product->get_attribute('Manufacturer');
if (!empty($brand_value)) {
// Copy the value into a taxonomy called 'store'
wp_set_object_terms($product_id, sanitize_text_field($brand_value), 'store', true);
}
return;
}
}
}
add_action('woocommerce_update_product', 'copy_brand_value_in_taxonomy');