WC_Product::set_category_ids PHP Method

set_category_ids() public method

Set the product categories.
Since: 2.7.0
public set_category_ids ( array $term_ids )
$term_ids array List of terms IDs.
    public function set_category_ids($term_ids)
    {
        $this->set_prop('category_ids', $this->sanitize_term_ids($term_ids, 'product_cat'));
    }

Usage Example

 /**
  * Save taxonomy terms.
  *
  * @param WC_Product $product  Product instance.
  * @param array      $terms    Terms data.
  * @param string     $taxonomy Taxonomy name.
  * @return WC_Product
  */
 protected function save_taxonomy_terms($product, $terms, $taxonomy = 'cat')
 {
     $term_ids = wp_list_pluck($terms, 'id');
     $term_ids = array_unique(array_map('intval', $term_ids));
     if ('cat' === $taxonomy) {
         $product->set_category_ids($term_ids);
     } elseif ('tag' === $taxonomy) {
         $product->set_tag_ids($term_ids);
     }
     return $product;
 }
WC_Product