Answers for "wc term_exists category"

0

wc term_exists category

// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );

$term_names = $product->get_attribute( 'Platform' ); // Can have many term names (coma separated)

$term_names = explode( ',', $term_names);
$term_ids   = [];

// Loop through the terms
foreach( $term_names as $term_name ) {
    // Get the term ID and check if it exist
    if( $term_id = term_exists( $term_name, 'product_cat' ) ) {
        // Add each term ID in an array
        $term_ids[] = $term_id; 
    } 
}
// Append the product category terms in the product 
if( sizeof($term_ids) > 0 ) {
    $product->set_category_ids( $term_ids );
    $product->save();
}
Posted by: Guest on April-23-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language