WC_Install::create_terms()public staticWC 1.0

Add the default terms for WC taxonomies - product types and order statuses. Modify this at your own risk.

Method of the class: WC_Install{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_Install::create_terms();

WC_Install::create_terms() code WC 9.4.2

public static function create_terms() {
	$taxonomies = array(
		'product_type'       => array(
			'simple',
			'grouped',
			'variable',
			'external',
		),
		'product_visibility' => array(
			'exclude-from-search',
			'exclude-from-catalog',
			'featured',
			'outofstock',
			'rated-1',
			'rated-2',
			'rated-3',
			'rated-4',
			'rated-5',
		),
	);

	foreach ( $taxonomies as $taxonomy => $terms ) {
		foreach ( $terms as $term ) {
			if ( ! get_term_by( 'name', $term, $taxonomy ) ) { // @codingStandardsIgnoreLine.
				wp_insert_term( $term, $taxonomy );
			}
		}
	}

	$woocommerce_default_category = (int) get_option( 'default_product_cat', 0 );

	if ( ! $woocommerce_default_category || ! term_exists( $woocommerce_default_category, 'product_cat' ) ) {
		$default_product_cat_id   = 0;
		$default_product_cat_slug = sanitize_title( _x( 'Uncategorized', 'Default category slug', 'woocommerce' ) );
		$default_product_cat      = get_term_by( 'slug', $default_product_cat_slug, 'product_cat' ); // @codingStandardsIgnoreLine.

		if ( $default_product_cat ) {
			$default_product_cat_id = absint( $default_product_cat->term_taxonomy_id );
		} else {
			$result = wp_insert_term( _x( 'Uncategorized', 'Default category slug', 'woocommerce' ), 'product_cat', array( 'slug' => $default_product_cat_slug ) );

			if ( ! is_wp_error( $result ) && ! empty( $result['term_taxonomy_id'] ) ) {
				$default_product_cat_id = absint( $result['term_taxonomy_id'] );
			}
		}

		if ( $default_product_cat_id ) {
			update_option( 'default_product_cat', $default_product_cat_id );
		}
	}
}