4

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?

M6Gpower
  • 155
  • 1
  • 8

1 Answers1

4

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thanks for fast response, can i just copy paste it in my function.php? – M6Gpower Dec 08 '18 at 16:42
  • @M6Gpower Where, how and on what event (when) do you want to use that? you need to build a custom function including this code, that you will use. The code as it is can not be pasted on function.php – LoicTheAztec Dec 08 '18 at 16:43
  • Once a new product is added/updated and is it possible to clear the product category before setting the new category? – M6Gpower Dec 08 '18 at 16:46
  • @M6Gpower I have added an example for product creation (when adding a new product in backend… but not sure that it will work when importing a product) – LoicTheAztec Dec 08 '18 at 17:05
  • Unfortunately the updated code is not working and im not getting any error hmm.The Products are all already imported with the Platform value/term, it has only go through every product and clear the category and set the Platform value/term as category.The hook must be, if a new product is created the code goes through all products and skips everything that is Platform equal Category. – M6Gpower Dec 08 '18 at 17:07
  • @M6Gpower I have made a little change to it and it will work now when editing a product, but it will always replace the product categories each time you will edit a product. Now I can't test this code as it depend on specific settings. You should better do a bulk update for all products with a sql query. This is just an example that shows you the way. – LoicTheAztec Dec 08 '18 at 17:14