Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display member pricing to a non member in woocommerce memberships WordPress

  • SOLVED

I am trying to display the members only price to non members on a website that I am building using the WooCommerce Membership plugin. The membership function is working appropriately in the sense that when a user buys the "subscription" product it grants them access to the members only price on other products for 1 year. What I am trying to accomplish though is displaying that members only price to non-members in an effort to show them how much money they could be saving if they bought the membership.

Storefront that I am working on can be found here: http://discountongroomingsupplies.com/product/furzone-7w-detachable-blade/

The above product has a separate price from subscribers of the 1 year membership. I want to display that price to non-members.

Answers (4)

2018-07-30

Kyle answers:

Try this in your functions.php file

<?php
function bsc_wc_memberships_members_only_product_price() {

global $product;
$id = $product->get_id();

if ( ! function_exists( 'wc_memberships' ) ) {
return;
}

if ( wc_memberships_is_product_purchasing_restricted() ) {

$discount = wc_memberships_get_member_product_discount($id);
$price = $product->get_price();
echo 'Member price: $'.$price - $discount;

}

}
add_action( 'woocommerce_before_add_to_cart_button', 'bsc_wc_memberships_members_only_product_price' );


User180161 comments:

Unfortunately, this did not change the pricing display on my site


Kyle comments:

It isn't changing it, your post just said to display it (it should appear above), I didn't realize you wanted to remove the existing price completely. Is that what you're saying?

That might create some problems because then they're going to try and add the product to cart at that price only to have a new amount shown.


Kyle comments:

Please make sure that when you are testing you are viewing a product as a non-member otherwise it will not show anything.


Kyle comments:

If you can add my code back to the functions.php file and let me know when you do I can view the product and see what it looks like


User180161 comments:

Hey Kyle - Thanks. You are correct, What I meant by "didn't change it" is that it didn't add the member price to the product page. (The placement of your code didn't do anything) - Placing the member price above or below the non-member price is exactly what I am looking to accomplish.

I added your code back in.


Kyle comments:

I have a membership license, just give me a minute to set this up on my install and I'll get something over in a minute


User180161 comments:

Sounds great, thanks.


Kyle comments:

Unfortunately, what you're asking for isn't really possible. All of the filter and action functions associated with the member plugin require a user ID of an active member to be able to pull discount data.


User180161 comments:

Hey Kyle - Thanks for looking into this. Would it be possible to enter in the member price as the sale price, turn the sale off, but pull in that sale price to display on the site? Here are the unique things with this site that make me think it could be possible:

1.) Products never actually go on sale
2.) There is only 1 level of membership

Thoughts?


Kyle comments:

Honestly, the truly easy thing so you don't have to worry about the sale price ever getting affected or showing unintentionally would be a simple custom field. Then in my original code we're just replacing the $discount function with a simple get_post_meta action and done.


User180161 comments:

How would I accomplish entering a custom field?


Kyle comments:

Top right of the screen when editing a product is a 'screen options' tab, click that and there will be a checkbox to show the custom field box. Then just scroll down and you can find the area to enter them. Left side is the 'key', right side is the value of the discount. The key will be the same for every product.


User180161 comments:

Got it. I have added a custom field to one of the products and named the key member_price and then entered the discount in the right area. Can you amend the original code you sent?


User180161 comments:

Hey Kyle - Thanks again for the help thus far. If you can send that amended code and it works, I'll mark this as resolved and get you the money on this.


Kyle comments:

Sorry for the delay

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: $'.$price - $discount;

}

}
add_action( 'woocommerce_before_add_to_cart_button', 'bsc_wc_memberships_members_only_product_price' );


You may want to add some HTML to the echo statement, like

<p>echo 'Member price: $'.$price - $discount;</p>

but that is up to you. Additionally, you can change the 'woocommerce_before_add_to_cart_button' hook as needed to move it around the product area. Here is a guide: https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/


Kyle comments:

Mistake on second code, meant this:

echo '<p>Member price: $'.$price - $discount.'</p>';


Kyle comments:

Oh, one more thing, you have to change the 'test' in this line:

if ( wc_memberships_is_user_active_member($user->ID, 'test') ) {

to the slug of your membership name


User180161 comments:

I added the amended code that you sent over, and changed 'test' to 'dogs-membership' which is the membership slug.

The product I added the custom field to is this one: http://discountongroomingsupplies.com/product/furzone-9-pc-attachment-comb-set/

Unfortunately, it is still not displaying that discounted price anywhere on the page. Am I doing something wrong here?


Kyle comments:

Oh wait, for testing I reversed it... this line needs a !

if ( !wc_memberships_is_user_active_member($user->ID, 'test') ) {

That changes it to mean if they aren't an active member


User180161 comments:

Definitely getting somewhere now. "-10" is now displaying just to the right of the add to cart button, so it doesn't appear that it is pulling in the original price an applying a discount.


User180161 comments:

Still looking at this product: http://discountongroomingsupplies.com/product/furzone-9-pc-attachment-comb-set/


Kyle comments:

That's embarrassing as a coder, in my defense I'm very jet lagged

echo 'Member price: $'.$total = $price - $discount;

That's your calc line


User180161 comments:

IT'S WORKING.

Now, to move it above the description but below the price - change the hook in the last line to add_action( 'woocommerce_single_product_summary' ?


Kyle comments:

There is no hook for that area, you need to add a filter and modify the price html


User180161 comments:

Is it possible to style the .$total = $price - $discount result that displays? For example, make it red or display it as <h5>? I seem to be able to do that for 'Member price: $' just by adding the <h5> to either side.


Kyle comments:

Yes, I posted that above, just replace the <p> tags from my example


User180161 comments:

echo '<h5>Member price: $' .$total = $price - $discount'</h5>';

I think this how it should be written but is causing a syntax error


Kyle comments:

echo '<h5>Member price: $' .$total = $price - $discount.'</h5>';

Missing a period after discount


User180161 comments:

Unreal. Kyle, you are a lifesaver. I'll mark this as closed and vote your answer 3 stars. I'd give it more if I could for all the help.

2018-07-30

Arnav Joy answers:

can you show me your site?


User180161 comments:

I just edited the question to reflect the site's URL. Thanks in advance!

2018-07-30

Mohamed Ahmed answers:

Hello,

Could you add this code to functions.php file of your theme


add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 99 );
function woocommerce_custom_sales_price( $price, $product ) {
global $post;
$pricing_rule_sets = get_option('_a_category_pricing_rules', array());
$terms = get_the_terms($post->ID,'product_cat');
//echo '<pre>';
//print_r($pricing_rule_sets);
//echo '</pre>';
if($terms){
foreach($terms as $term) {
$postterm = $term->term_id;
}
}
if(count($pricing_rule_sets)){
foreach ($pricing_rule_sets as $pricing_rule_set) {
if($pricing_rule_set['rules'][1][amount])
{
$cats = $pricing_rule_set[collector][args][cats];
if(in_array($postterm,$cats))
{
$percentage = $pricing_rule_set['rules'][1][amount];
}
}
}
}
if(!is_user_logged_in())
{
$regprice = $product->regular_price;
$saleprice = $product->sale_price;
if($percentage)
{
$gmprice = ($regprice- ($regprice * $percentage)/100);
return sprintf( __('Regular Price: %s <br />Gold Member Price: %s', 'woocommerce' ), $saleprice, $gmprice );
}
else
{
return sprintf( __('Price: %s', 'woocommerce' ), $saleprice );
}
}
else
{
return $price;
}
}
//add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
global $post;
$pricing_rule_sets = get_option('_a_category_pricing_rules', array());
$terms = get_the_terms($post->ID,'product_cat');
if($terms){
foreach($terms as $term) {
$postterms[] = $term->term_id;
}
}
if(count($pricing_rule_sets)){
foreach ($pricing_rule_sets as $pricing_rule_set) {
if($pricing_rule_set['rules'][1][amount])
{
$cats = $pricing_rule_set[collector][args][cats];
foreach ($postterms as $postterm) {
if(in_array($postterm,$cats))
{
$percentage = $pricing_rule_set['rules'][1][amount];
}
}
}
}
}
if(!is_user_logged_in())
{
$regprice = $product->get_variation_price( 'min', true );
if($percentage)
{
$gmprice = ($regprice- ($regprice * $percentage)/100);
return sprintf( __('Regular Price: %s <br />Gold Member Price: %s', 'woocommerce' ), wc_price($regprice), wc_price($gmprice) );
}
else
{
return sprintf( __('Price: %s', 'woocommerce' ), wc_price($regprice) );
}
}
else
{
return $price;
}
}


User180161 comments:

unfortunately, this did not change the pricing display on my site

2018-07-30

Reigel Gallarde answers:

what WooCommerce Membership plugin are you using?


User180161 comments:

I am using Woocommerce Memberships

https://woocommerce.com/products/woocommerce-memberships/