WC_Post_Data::force_default_term()public staticWC 3.3.0

Ensure default category gets set.

Method of the class: WC_Post_Data{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_Post_Data::force_default_term( $object_id, $terms, $tt_ids, $taxonomy, $append );
$object_id(int) (required)
Product ID.
$terms(array) (required)
Terms array.
$tt_ids(array) (required)
Term ids array.
$taxonomy(string) (required)
Taxonomy name.
$append(true|false) (required)
Are we appending or setting terms.

Changelog

Since 3.3.0 Introduced.

WC_Post_Data::force_default_term() code WC 8.7.0

public static function force_default_term( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
	if ( ! $append && 'product_cat' === $taxonomy && empty( $tt_ids ) && 'product' === get_post_type( $object_id ) ) {
		$default_term = absint( get_option( 'default_product_cat', 0 ) );
		$tt_ids       = array_map( 'absint', $tt_ids );

		if ( $default_term && ! in_array( $default_term, $tt_ids, true ) ) {
			wp_set_post_terms( $object_id, array( $default_term ), 'product_cat', true );
		}
	}
}