Skip to content

Instantly share code, notes, and snippets.

@webtoffee-git
Last active January 20, 2022 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webtoffee-git/85965ac1225beaa136330dc98736b555 to your computer and use it in GitHub Desktop.
Save webtoffee-git/85965ac1225beaa136330dc98736b555 to your computer and use it in GitHub Desktop.
Filter export data using custom taxonomy - Product import export plugin for WooCommerce by WebToffee (https://www.webtoffee.com/product/product-import-export-woocommerce/)
<?php // Please do not copy this line
// generalize filter by custom taxonomy
$GLOBALS['wt_custom_taxonomy_name'] = 'product_brand';
add_filter('wt_iew_exporter_alter_filter_fields', 'wt_add_additional_exporter_alter_filter_fields', 10, 3);
if(!function_exists('wt_add_additional_exporter_alter_filter_fields')){
function wt_add_additional_exporter_alter_filter_fields($fields, $base, $filter_form_data){
if ('product' != $base) {
return $fields;
}
$fields[$GLOBALS['wt_custom_taxonomy_name']] = array(
'label' => __('Product '. ucwords(str_replace('_', ' ', $GLOBALS['wt_custom_taxonomy_name'])), 'wt-import-export-for-woo'),
'placeholder' => __('All '.ucwords(str_replace('_', ' ', $GLOBALS['wt_custom_taxonomy_name'])), 'wt-import-export-for-woo'),
'field_name' => $GLOBALS['wt_custom_taxonomy_name'],
'sele_vals' => get_custom_taxonomy(),
'help_text' => __('Filter products by their '.ucwords(str_replace('_', ' ', $GLOBALS['wt_custom_taxonomy_name'])).'. You can export multiple together.', 'wt-import-export-for-woo'),
'type' => 'multi_select',
'css_class' => 'wc-enhanced-select',
'validation_rule' => array('type'=>'text_arr')
);
return $fields;
}
}
if(!function_exists('get_custom_taxonomy')){
function get_custom_taxonomy(){
$out = array();
$product_categories = get_terms(array(
'taxonomy' => $GLOBALS['wt_custom_taxonomy_name'],
'hide_empty' => false,
) );
if (!is_wp_error($product_categories)) {
$version = get_bloginfo('version');
foreach ($product_categories as $category) {
$out[$category->slug] = (( $version < '4.8') ? $category->name : get_term_parents_list($category->term_id, $GLOBALS['wt_custom_taxonomy_name'], array('separator' => ' -> ')));
}
}
return $out;
}
}
add_filter('woocommerce_csv_product_export_args','wt_woocommerce_csv_product_export_args');
if(!function_exists('wt_woocommerce_csv_product_export_args')){
function wt_woocommerce_csv_product_export_args($args){
$form_data=(isset($_POST['form_data']) ? Wt_Import_Export_For_Woo_Common_Helper::process_formdata(maybe_unserialize(($_POST['form_data']))) : array());
$prod_brand = !empty($form_data['filter_form_data']['wt_iew_'.$GLOBALS['wt_custom_taxonomy_name']]) ? $form_data['filter_form_data']['wt_iew_'.$GLOBALS['wt_custom_taxonomy_name']] : array();
if(!empty($prod_brand)){
$args['filter_by_custom_taxonomy'] = $prod_brand;
}
return $args;
}
}
if(!function_exists('wt_handle_custom_query_var')){
function wt_handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['filter_by_custom_taxonomy'] ) ) {
$query['tax_query'][] = array(
'taxonomy' => $GLOBALS['wt_custom_taxonomy_name'],
'field' => 'slug',
'terms' => $query_vars['filter_by_custom_taxonomy'],
);
}
return $query;
}
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'wt_handle_custom_query_var', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment