Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Save category meta as IDs #3

Open
helgatheviking opened this issue Sep 22, 2021 · 0 comments
Open

Save category meta as IDs #3

helgatheviking opened this issue Sep 22, 2021 · 0 comments

Comments

@helgatheviking
Copy link
Collaborator

In an ideal world , we'd save the category IDs. This will make it 100x easier to translate.

However, Woo core's category enhanced select saves SLUGS. that's why we have saved slugs now. 🙄

We could write our own custom JS for the enhanced select. If we start saving as IDs we'll technically need to run an update routine... though this is an unsupported plugin and since it doesn't update automagically we coudl require folks, re-save their meta.

wc_get_products() also doesn't yet support querying by term ID so the two things are kinda linked.

However, we can add support for querying by IDs like so:

function kia_query_by_ids( $wp_query_args, $query_vars ) {
	// Handle product categories by ID.
	if ( ! empty( $query_vars[ 'category_id' ] ) ) {
		unset( $wp_query_args['category_id'] );
		$wp_query_args['tax_query'][] = array(
			'taxonomy' => 'product_cat',
			'field'    => 'term_id',
			'terms'    => $query_vars['category_id'],
		);
	}

	// Handle product tags by ID.
	if ( ! empty( $query_vars['tag_id'] ) ) {
		unset( $wp_query_args['tag_id'] );
		$wp_query_args['tax_query'][] = array(
			'taxonomy' => 'product_tag',
			'field'    => 'term_id',
			'terms'    => $query_vars['tag_id'],
		);
	}

	return $wp_query_args;
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'kia_query_by_ids', 10, 2 );
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant