Skip to content

Instantly share code, notes, and snippets.

@ictheblackc
Last active November 25, 2022 08:51
Show Gist options
  • Save ictheblackc/b9aa29eeac36125de56a318c07d3de1d to your computer and use it in GitHub Desktop.
Save ictheblackc/b9aa29eeac36125de56a318c07d3de1d to your computer and use it in GitHub Desktop.
Change membership badge in WooCommerce Memberships
<?php
/**
* Change member discount badge.
*/
function change_membership_badge( $badge, $the_post, $product ) {
if ( $product->is_type( 'variable' ) ) {
$variations = $product->get_available_variations();
foreach ( $variations as $variation ) {
$regular_price = $variation['display_regular_price'];
$sale_price = $variation['display_price'];
}
} else {
$regular_price = $product->get_regular_price();
if ( !$regular_price ) $regular_price = $product->get_price();
$sale_price = $product->get_sale_price();
}
// Change default badge text to discount percentage
$percent_dicount = round( (($regular_price - $sale_price) / $regular_price) * 100 );
$custom_badge = '<span class="onsale wc-memberships-member-discount">' . $percent_dicount . '%</span>';
return $custom_badge;
}
add_filter( 'wc_memberships_member_discount_badge', 'change_membership_badge', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment