Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::init_default_taxonomies()publicWC 1.0

Set default taxonomies for the order.

Note: This is re-implementation of part of WP core's wp_insert_post Since the code block that set default taxonomies is not filterable, we have to re-implement it.

Method of the class: OrdersTableDataStore{}

No Hooks.

Return

Array. Sanitized tax input with default taxonomies.

Usage

$OrdersTableDataStore = new OrdersTableDataStore();
$OrdersTableDataStore->init_default_taxonomies( $order, $sanitized_tax_input );
$order(\WC_Abstract_Order) (required)
Order object.
$sanitized_tax_input(array) (required)
Sanitized taxonomy input.

OrdersTableDataStore::init_default_taxonomies() code WC 9.3.3

public function init_default_taxonomies( \WC_Abstract_Order $order, array $sanitized_tax_input ) {
	if ( 'auto-draft' === $order->get_status() ) {
		return $sanitized_tax_input;
	}

	foreach ( get_object_taxonomies( $order->get_type(), 'object' ) as $taxonomy => $tax_object ) {
		if ( empty( $tax_object->default_term ) ) {
			return $sanitized_tax_input;
		}

		// Filter out empty terms.
		if ( isset( $sanitized_tax_input[ $taxonomy ] ) && is_array( $sanitized_tax_input[ $taxonomy ] ) ) {
			$sanitized_tax_input[ $taxonomy ] = array_filter( $sanitized_tax_input[ $taxonomy ] );
		}

		// Passed custom taxonomy list overwrites the existing list if not empty.
		$terms = wp_get_object_terms( $order->get_id(), $taxonomy, array( 'fields' => 'ids' ) );
		if ( ! empty( $terms ) && empty( $sanitized_tax_input[ $taxonomy ] ) ) {
			$sanitized_tax_input[ $taxonomy ] = $terms;
		}

		if ( empty( $sanitized_tax_input[ $taxonomy ] ) ) {
			$default_term_id = get_option( 'default_term_' . $taxonomy );
			if ( ! empty( $default_term_id ) ) {
				$sanitized_tax_input[ $taxonomy ] = array( (int) $default_term_id );
			}
		}
	}
	return $sanitized_tax_input;
}