• Hi,

    Thank you for such a great plugin!

    I need to rename the two WooCommerce product typessimple” and “external“:

    • “simple” –> “Document”
    • “external” –> “Link”

    I tried this:

    // Rename WC product types:
    function custom_rename_product_types( $types ) {
        $types['simple'] = __('Document', 'wc-ajax-product-filter');
        $types['external'] = __('Lien', 'wc-ajax-product-filter');
    
        return $types;
    }
    add_filter( 'product_type_selector', 'custom_rename_product_types', 10, 1 );

    It works in the WP back-end (list of WooCommerce products). But it doesn’t work on the front-end filters of WCAPF. Actually the plugin seems to display the raw values of ‘product_type’ (“simple” and external” in lowercase) instead of their display name, which brings another problem for non-English websites (my case), and won’t make much sense for end-users.

    Is my code above correct? Would it be possible to make it work with the plugin?

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Lorangeo

    (@lorangeo)

    Please, is there any solution for this? I really need to fix it.

    • This reply was modified 5 months, 2 weeks ago by Lorangeo.
    Plugin Author Mainul Hassan Main

    (@shamimmoeen)

    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!

    Thread Starter Lorangeo

    (@lorangeo)

    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).

    Plugin Author Mainul Hassan Main

    (@shamimmoeen)

    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!

    Thread Starter Lorangeo

    (@lorangeo)

    Hi, that’s great to hear! I will wait for the update! Thank you very much!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.