In WooCommerce, I have 3 membership tiers (silver, gold and platinum) and I applied higher discount rate for higher membership tier.
I would like show the 4 different prices (non-member, silver, gold and platinum) to everyone such that they know how much they can save on each product if they join the membership. For example:
I tried the code below:
function bsc_wc_memberships_members_only_product_price() {
global $product;
$user = wp_get_current_user();
if ( !wc_memberships_is_user_active_member($user->ID, 'test') ) {
$id = $product->get_id();
$discount = get_post_meta( $id, 'member_price', true );
$price = $product->get_price();
echo 'Member price: $'.$total = $price - $discount;
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'bsc_wc_memberships_members_only_product_price' );
But it doesn't really work unfortunately... Any advice will be highly appreciated.
There is an obvious mistake in:
echo 'Member price: $'.$total = $price - $discount;
that should be instead just:
echo 'Member price: $'. $price - $discount;
or even better:
echo 'Member price: '. wc_price( $price - $discount );
But as this are displayed prices you need to use something a bit different and more complete like:
add_action( 'woocommerce_before_add_to_cart_button', 'bsc_wc_memberships_members_only_product_price' );
function bsc_wc_memberships_members_only_product_price() {
global $product;
if ( ! wc_memberships_is_user_active_member( get_current_user_id(), 'test' ) ) {
$discount = wc_get_price_to_display( $product, array('price' => $product->get_meta('member_price') ) );
$price = wc_get_price_to_display( $product );
$silver_price = $price - $discount;
echo '<span class="silver-price">' . __('Member price') . ': ' . wc_price( $silver_price ) . '</span>';
}
}
Checking that a custom field meta value really exist on member_price
meta key for this product under wp_postmeta
database table.
Code goes in function.php file of your active child theme (or active theme). It should better work.