Please, is there any solution for this? I really need to fix it.
-
This reply was modified 3 months, 1 week ago by Lorangeo.
Hi,
Thank you for your kind words and for using the plugin!
You’re on the right track with your code. To ensure that the product types are correctly renamed on both the back-end and the front-end filters, you can use the following code snippet in your child theme’s functions.php
file. This code ensures that the product types are renamed everywhere they appear, including the front-end filters of WCAPF.
// Rename WC product types:
function custom_rename_product_types( $types ) {
$types['simple'] = __( 'Simple', 'wc-ajax-product-filter' );
$types['external'] = __( 'External', 'wc-ajax-product-filter' );
$types['grouped'] = __( 'Grouped', 'wc-ajax-product-filter' );
$types['variable'] = __( 'Variable', 'wc-ajax-product-filter' );
return $types;
}
add_filter( 'product_type_selector', 'custom_rename_product_types' );
add_filter( 'wcapf_taxonomy_terms', 'wcapf_rename_product_types_in_filter_options', 10, 2 );
/**
* Rename product types when showing in the filter options.
*
* @param array $terms The array of taxonomy terms.
* @param WCAPF_Field_Instance $field The field instance.
*
* @return array
*/
function wcapf_rename_product_types_in_filter_options( $terms, $field ) {
if ( 'taxonomy' === $field->type && 'product_type' === $field->taxonomy ) {
$new_terms = array();
foreach ( $terms as $_term ) {
$term = $_term;
if ( 'simple' === $term['slug'] ) {
$term['name'] = __( 'Simple', 'wc-ajax-product-filter' );
} elseif ( 'external' === $term['slug'] ) {
$term['name'] = __( 'External', 'wc-ajax-product-filter' );
} elseif ( 'grouped' === $term['slug'] ) {
$term['name'] = __( 'Grouped', 'wc-ajax-product-filter' );
} elseif ( 'variable' === $term['slug'] ) {
$term['name'] = __( 'Variable', 'wc-ajax-product-filter' );
}
$new_terms[] = $term;
}
return $new_terms;
}
return $terms;
}
add_filter( 'wcapf_active_taxonomy_filter_data', 'wcapf_rename_product_types_in_active_filters', 10, 2 );
/**
* Rename product types for active filters.
*
* @param array $filter_data The filter data.
* @param WCAPF_Field_Instance $field_instance The field instance.
*
* @return array
*/
function wcapf_rename_product_types_in_active_filters( $filter_data, $field_instance ) {
if ( 'taxonomy' === $field_instance->type && 'product_type' === $field_instance->taxonomy ) {
$new_filter_data = array();
foreach ( $filter_data as $term_id => $term_name ) {
$new_name = $term_name;
if ( 'Blue' === $term_name ) {
$new_name = 'hello';
}
if ( 'simple' === $term_name ) {
$new_name = __( 'Simple', 'wc-ajax-product-filter' );
} elseif ( 'external' === $term_name ) {
$new_name = __( 'External', 'wc-ajax-product-filter' );
} elseif ( 'grouped' === $term_name ) {
$new_name = __( 'Grouped', 'wc-ajax-product-filter' );
} elseif ( 'variable' === $term_name ) {
$new_name = __( 'Variable', 'wc-ajax-product-filter' );
}
$new_filter_data[ $term_id ] = $new_name;
}
return $new_filter_data;
}
return $filter_data;
}
If you have any further questions or need additional assistance, feel free to ask!
Hi!
Thank you taking the time to answer my question!
Interestingly it works for the filters options (on the side), but not for the active filters (that show on the top).
I edited both the wcapf_rename_product_types_in_filter_options
and wcapf_rename_product_types_in_active_filters
to add the new names. But it only works for the filter options.
However: Don’t you think you should verify and maybe change how your plugin is displaying these “product type” terms? Because (sorry if I’m wrong), it looks like the plugin displays the raw slug version of the product types (“simple”, “external”…) instead of their display name (“Simple, “External”) for this particular filter (product type). Because for the other filters, it shows the correct name (and not the slug).
Hi,
Thank you for your feedback and for pointing that out!
To resolve the issue with the active filters, you can use the following code snippet and load wcapf_active_taxonomy_filter_data
using the after_setup_theme
hook:
add_action( 'after_setup_theme', function() {
add_filter( 'wcapf_active_taxonomy_filter_data', 'wcapf_rename_product_types_in_active_filters', 10, 2 );
/**
* Rename product types for active filters.
*
* @param array $filter_data The filter data.
* @param WCAPF_Field_Instance $field_instance The field instance.
*
* @return array
*/
function wcapf_rename_product_types_in_active_filters( $filter_data, $field_instance ) {
if ( 'taxonomy' === $field_instance->type && 'product_type' === $field_instance->taxonomy ) {
$new_filter_data = array();
foreach ( $filter_data as $term_id => $term_name ) {
$new_name = $term_name;
if ( 'Blue' === $term_name ) {
$new_name = 'hello';
}
if ( 'simple' === $term_name ) {
$new_name = __( 'Simple', 'wc-ajax-product-filter' );
} elseif ( 'external' === $term_name ) {
$new_name = __( 'External', 'wc-ajax-product-filter' );
} elseif ( 'grouped' === $term_name ) {
$new_name = __( 'Grouped', 'wc-ajax-product-filter' );
} elseif ( 'variable' === $term_name ) {
$new_name = __( 'Variable', 'wc-ajax-product-filter' );
}
$new_filter_data[ $term_id ] = $new_name;
}
return $new_filter_data;
}
return $filter_data;
}
} );
Regarding your suggestion about how the plugin displays product type terms, you’re absolutely right. The current implementation does show the raw slug version, and I agree that it should display the proper names. I’ll work on updating the plugin to make this the default behavior.
Thank you again for your valuable feedback!
Hi, that’s great to hear! I will wait for the update! Thank you very much!