M6Gpower
M6Gpower

Reputation: 165

Set a product category term in a product on Woocommerce

In Woocommerce I have a Product Attribute called "Platform" the Value of the Attribute is "Steam":

and then the Attribute and

So I am bulk importing the products and the Attributes are already there.

But now I have to set for every product manually the category. Is it possible to set the Value automatically as Product Category in a function?

enter image description here

This Function is returning me the Attribute value right?

function get_attribute_value_from_name( $name ){
  global $wpdb;
   $name = 'Platform';
    $attribute_value = $wpdb->get_var("SELECT attribute_value
     FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
  WHERE attribute_name LIKE '$name'");
 return $attribute_value;
}

And now how to set the value for product category?

EDIT:

$product = wc_get_product($id); //LOAD PRODUCT
global $product_attribute; //VARIABLE
$product_attribute = $product->get_attribute( 'Platform' ); //GET ATTRIBUTE OF PLATFORM
wp_set_object_terms( $post_id, $product_attribute, 'product_cat' ); //WRITE IT AS CATEGORY
$product->save();  //SAVE PRODUCT

does this make sense?

Upvotes: 4

Views: 4185

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254448

Update 2 - To set an existing product category term in a product (using a defined product ID):

// 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();
}

Here below is an example of a hooked function that will auto set the product category terms on product edit.

Note: the product category terms need to exist in woocommerce

// Backend product creation
add_action( 'woocommerce_admin_process_product_object', 'add_product_category_terms_to_product', 100, 1 );
function add_product_category_terms_to_product( $product ){
    global $pagenow;

    // Only on product Edit
    if( $pagenow != 'post.php' ) return; // Exit

    if( $term_names = $product->get_attribute( 'Platform' ) ) 
        $term_names = explode( ',', $term_names);
    else
        return; // Exit

    $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; 
        }
    }
    // replace the product categories terms in the product 
    if( sizeof($term_ids) > 0 ) {
        $product->set_category_ids( $term_ids );
    }
    // save is not needed in the function as this hook does that
}

Code goes in function.php file of your active child theme (or active theme). It should works.

Upvotes: 5

Related Questions