• Hello,

    So i have a piece of code:

    for ($i=0; $i < $nblignes; $i++) { 
    
    		$niv = round($niveau[$i]);
    		$lib = $libelle[$i];
    		
    		if ($niv == 1) {
    			$id = substr($lib, 0, 4);
    			$parent = substr($lib, 0, 2);
    			$cat_name = substr($lib, 7);
    			$catarr = array(
    						    'term_id' => $id,
    							'name' => $cat_name,
    							'parent' => $parent,
    							'taxonomy' => 'product_cat',
    							'slug' => $cat_name,
    							'description' => $cat_name
    			);
    			update_term_meta( $id, 'product_cat', $catarr, $prev_value = ''  );
    			echo $id.'&nbsp;|&nbsp;'.$parent.'&nbsp;|&nbsp;'.$cat_name.'<br>';
    		}
    
    		if ($niv == 2) {
    			$id = substr($lib, 0, 2);
    			$parent = substr($lib, 0, 1);
    			$cat_name = substr($lib, 5);
    			$catarr = array(
    						    'term_id' => $id,
    							'name' => $cat_name,
    							'parent' => $parent,
    							'taxonomy' => 'product_cat',
    							'slug' => $cat_name,
    							'description' => $cat_name
    			);
    			update_term_meta( $id, 'product_cat', $catarr, $prev_value = ''  );
    			echo $id.'&nbsp;|&nbsp;'.$parent.'&nbsp;|&nbsp;'.$cat_name.'<br>';
    		}
    
    		if ($niv == 3) {
    			$id = substr($lib, 0, 1);
    			$parent = '';
    			$cat_name = substr($lib, 4);
    			$catarr = array(
    						    'term_id' => $id,
    							'name' => $cat_name,
    							'parent' => $parent,
    							'taxonomy' => 'product_cat',
    							'slug' => $cat_name,
    							'description' => $cat_name
    			);
    			update_term_meta( $id, 'product_cat', $catarr, $prev_value = ''   );
    			echo $id.'&nbsp;|&nbsp;'.$parent.'&nbsp;|&nbsp;'.$cat_name.'<br>';
    		}

    The thing is, it works (category are well created in wp_termmeta), but categories doesn’t appear in woocommerce categories area in wordpress. So i think update_term_meta is not a good way to do it … or am i missing something ?

    Thanks for your help !

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    $term = wp_insert_term( 'Custom category', 'product_cat', [
    	'description'=> 'Custom Category description',
    	'slug' => 'custom-category' ]
    );
    if ( is_wp_error( $term ) ) {
    	$term_id = $term->error_data['term_exists'] ?? null;
    } else {
    	$term_id = $term['term_id'];
    }
    Thread Starter hypopolol

    (@hypopolol)

    Oh nice, thank you !

    So this time it works but (there’s always a but ^^), i can’t decide for the term_id, so i can’t decide witch one is a cat, a subcat, a subsubcat etc..

    if ($niv == 2) {
    			$id = substr($lib, 0, 2);
    			$parent = substr($lib, 0, 1);
    			$cat_name = substr($lib, 5);
    			$catarr = array(
    						    'term_id' => $id,
    							'name' => $cat_name,
    							'parent' => $parent,
    							'taxonomy' => 'product_cat',
    							'slug' => $cat_name,
    							'description' => $cat_name
    			);
    			$term = wp_insert_term( $cat_name, 'product_cat', $catarr );
    			if ( is_wp_error( $term ) ) {
    				$term_id = $term->error_data['term_exists'] ?? null;
    			} else {
    				$term_id = $id;
    			}
    			echo $id.'&nbsp;|&nbsp;'.$parent.'&nbsp;|&nbsp;'.$cat_name.'<br>';
    		}

    in this exemple: $niv == 2 so it’s a subcat, her parent cat is the first digit of $lib. So for each cat i’ve tried, like above, to replace
    $term_id = $term['term_id'];
    by
    $term_id = $id;

    so this way can refound her to set her as parent of her subsubcat’s (i’m pretty sure it’s possible to use less ‘her’ in that sentence). So what happen her is, $niv == 1 category, witch are main cats, are well created since i don’t define parent cat, but $term_id = $id doesn’t work, (it’s maybe because wordpress don’t like id with only 1 digit ?), it’s replaced by 5 digits ids, so when $niv == 2 arrive they are not created at all because ‘parent’ => $parent fails.

    And finally i have tons of
    Notice: update_woocommerce_term_meta est obsolète depuis la version 3.6 ! Utilisez update_term_meta à la place. in /var/www/wordpress/wp-includes/functions.php on line 4440
    when using wp_insert_term() ^^.

    So i’m gonna lookup for these issues, but if someone got and idea <3

    Thanks again !

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create WooCommerce Category in PHP’ is closed to new replies.