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

WooCommerce: Display the member price to non-members WordPress

  • SOLVED

I have the WooCommerce Dynamic Pricing Plugin:
http://www.woothemes.com/products/dynamic-pricing/

I have used it to set discounts for GOLD members....

So non-members get one price, and gold members get a lower price (usually about 10%, but changes based on the product category).

On both the shop pages, and the product detail page, I'd like to show both prices:
Regular Price: $20
Gold Member Price: $18

So that the user can see that lower prices are available if they upgrade to a gold membership. How can i do this? Preferably in a form that I won't loose with upgrades.

Thanks,

- Eric

Answers (3)

2014-04-04

Hariprasad Vijayan answers:

Hello Eric,

I think you are searching for a code like this.

<?php
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
if(!is_user_logged_in()) {
$gm_price = $price - ($price*10%);
$str = '<p>Regular Price: '.$price.'</p>';
$str. = '<p>'Gold Member Price: '.$gm_price.'</p>';
return $str;
}
else
{
return $price;
}
}
?>

Not tested. Collected from few threads. Check following link.
http://gerhardpotgieter.com/2013/09/06/woocommerce-percentage-saved-sale-price/

Hope it help you.


EricNeedsHelp comments:

Thanks, this is the closest to what I'm looking for so far.

The discount changes based on the product category though.

Also, it would be better if the discount wasn't hard-coded... It's specified in the Dyanmic Pricing Plugin, and I'd like to be able to let the store administrator update the discount categories / percentages without me needing to make code changes to reflect the update for him.


Hariprasad Vijayan comments:

Try this

<?php
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
global $woocommerce, $post;
if(!is_user_logged_in()) {
$array_rule_sets = get_post_meta($post->ID, '_pricing_rules', true);
foreach($array_rule_sets as $pricing_rule_sets)
{
foreach ( $pricing_rule_sets['rules'] as $key => $value ) {
$gm_price = $price - ($price/100)*$pricing_rule_sets['rules'][$key]['amount'];
if($key != count($pricing_rule_sets['rules'])-1) {
$str = '<p>Regular Price: '.$price.'</p>';
$str. = '<p>'Gold Member Price: '.$gm_price.'</p>';
return $str;
}
}
}

}
else
{
return $price;
}
}
?>

Code not tested. Collected from threads.


Hariprasad Vijayan comments:

Try this code in your functions.php,

add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
if(!is_user_logged_in()) {
$saleprice = extract_unit($price,'<ins>','</ins>');
$regprice = extract_unit($price,'<del>','</del>');
return 'Regular Price: ' . $regprice . '<br />Gold Member Price: ' . $saleprice;
}
else
{
return $price;
}
}
function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}


EricNeedsHelp comments:

That code displays the text "Gold member Price":
http://www.forforce.com.br/produto/im-coqueteleira-darkness/

But the value is wrong... It is displaying current sale price (Sale price available to all users). It is not showing the price defined by the Dynamic Pricing Plugin.


Hariprasad Vijayan comments:


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;
}
}
?>


EricNeedsHelp comments:

Wow, incredible work, thanks for this!, Works Perfect!

2014-04-04

John Cotton answers:

I can't tell you precisely as I don't use that plugin.

But I do a similar thing with the Ignite teired pricing plugin and use these filters to modify the HTML as I need to:

woocommerce_sale_price_html
woocommerce_price_html
woocommerce_empty_price_html

They each pass two parameters to your hook: $price and $_product

So you can use some logic (eg is_user_logged_in) and adjust $price (that's the html that gets displayed) by adding or amending it as you need to.

Hope that helps.

2014-04-04

Arnav Joy answers:

i think you have to write manually these code to files directly

to get regular price

<?php echo $price = get_post_meta( get_the_ID(), '_regular_price', true); ?>


to get sale price

<?php echo $sale = get_post_meta( get_the_ID(), '_sale_price', true); ?>

where get_the_ID() is the id of the product