Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Programmer095/6ca6e2aa76614d1df7434e9953818c25 to your computer and use it in GitHub Desktop.
Save Programmer095/6ca6e2aa76614d1df7434e9953818c25 to your computer and use it in GitHub Desktop.
add_filter('wp_all_import_set_post_terms', 'wpai_wp_all_import_set_post_terms', 10, 4);
function wpai_wp_all_import_set_post_terms($assign_terms, $tx_name, $pid, $import_id) {
	if ($tx_name == 'product_cat'){		
		$post = get_post($pid);
		if ($post->post_type == 'product_variation') {
			$parentID = $post->post_parent;				
			$term_ids = wp_get_object_terms($parentID, $tx_name, array( 'fields' => 'ids' ));							
			if (!empty($assign_terms)) {
				foreach ($assign_terms as $term_id) {
					if (!in_array($term_id, $term_ids)) {
						$term_ids[] = $term_id;
					}
				}
			}
			wp_set_object_terms($parentID, $term_ids, $tx_name);
		}
	}
	return $assign_terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment