I want to check for example Brand “Apple” Exist as a term in Brands Attribute, then setup the brand apple to this product as taxonomy type.
$product_id = 214246;
$product = wc_get_product($product_id);
check if the product exist
if ($product) {
$attributes = $product->get_attributes();
if (!empty($attributes)) {
// Loop through each attribute
foreach ($attributes as $attribute) {
// Get the attribute label
$attribute_label = wc_attribute_label($attribute->get_name());
// Get the attribute value
$attribute_value = '';
// Check if the attribute is a taxonomy type
if ($attribute->is_taxonomy()) {
$attribute_value = implode(', ', wc_get_product_terms($product->get_id(), $attribute->get_name(), array('fields' => 'names')));
}
// Display the attribute label and value
echo '<p><strong>' . $attribute_label . ':</strong> ' . $attribute_value . '</p>';
}
} else {
// I want to add if the brands Apple exist if not then add apple as a brand then set this brand to this product.
}
}
Thank You