Automattic\WooCommerce\Internal\Admin

CategoryLookup::update()protectedWC 1.0

Updates lookup table data for a category by ID.

Method of the class: CategoryLookup{}

No Hooks.

Return

null. Nothing.

Usage

// protected - for code of main (parent) or child class
$result = $this->update( $category_id );
$category_id(int) (required)
Category ID to update.

CategoryLookup::update() code WC 7.1.0

protected function update( $category_id ) {
	global $wpdb;

	$ancestors    = get_ancestors( $category_id, 'product_cat', 'taxonomy' );
	$children     = get_term_children( $category_id, 'product_cat' );
	$inserts      = array();
	$inserts[]    = $this->get_insert_sql( $category_id, $category_id );
	$children_ids = array_map( 'intval', array_unique( array_filter( $children ) ) );

	foreach ( $ancestors as $ancestor ) {
		$inserts[] = $this->get_insert_sql( $category_id, $ancestor );

		foreach ( $children_ids as $child_category_id ) {
			$inserts[] = $this->get_insert_sql( $child_category_id, $ancestor );
		}
	}

	$insert_string = implode( ',', $inserts );

	$wpdb->query( "INSERT IGNORE INTO $wpdb->wc_category_lookup (category_id, category_tree_id) VALUES {$insert_string}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}