phpwordpresswoocommercepolylangwoocommerce-memberships

Filter Membership Plan Title and Slug


I would like to filter the titles of my WooCommerce Memberships plans depending in the current language (using PolyLang). The following works on the back end, i.e. I can see that the membership plan title (for membership plan with post_id 3884) changes on /wp-admin/edit.php?post_type=wc_membership_plan, when I set PolyLang to a language different from English:

function my_the_title( $title, $id ) {
    if (pll_current_language( 'slug' ) == 'en') {
        return $title;
    }
    if ( $id == '3884' ) {
        return $title . ' - Not English';
    }
    return $title;
}
add_filter( 'the_title', 'my_the_title', 10, 2 );

The problem is that the above does not work on the front end under /my-account/members-area/. Here the original membership plan title is used no matter the language. Logging all calls to my_the_title, I can see that the post 3884 is coming through when browsing back end pages (and I presume that is why it works on the back end), but when browsing front end pages, I do not see post 3884 coming through. I guess the_title is wrong filter in this particular case, but I can't figure out why and what else to use in that case.

Thanks, Uwe

ps: WordPress 6.3, WooCommerce 8.0.2, WooCommerce Memberships 1.25, PolyLang Pro 3.4.5, PolyLang for WooCommerce 1.8


Solution

  • Note: The following requires a child theme (there is no other way).

    You will need to override the My Account related template files from WooCommerce Memberships plugin, via your child theme. Read the following threads:

    First in your child theme, add a "woocommerce" folder with a "myaccount" subfolder.

    So inside WooCommerce Memberships plugin from "templates" directory > "myaccount" subdirectory copy the file "my-memberships.php" to your child theme "myaccount" subfolder (located inside the "woocommerce" folder).

    Once done you can open/edit "my-memberships.php" template file from your child theme.

    Replace everywhere (2 times):

    $customer_membership->get_plan()->get_name()
    

    with:

    get_the_title($customer_membership->get_plan()->id)
    

    Then save.

    Now you can use your code snippet (with the_title filter hook) to make changes on those membership plan names.