• Resolved miguelitolaparra

    (@miguelitolaparra)


    Thanks for this great plugin, I’m just testing it and it works fine.

    I have a question and I come looking for help.

    In the price that is shown to all users in the store, I have added a text after the price: P.V.P.
    I have achieved this with a function that a friend made, I show you this function below.

    My intention is to achieve that in the price that is displayed to the users created with the Wholesale Customer Rool, in the products, behind the price that I have created with Wholesale Customer, instead of showing the text that I have now ( P.VP. ) ), show the following text (P.V.M. ) WHOLESALE PRICE.

    This is a problem for me, since I haven’t mastered php yet how to create a function to achieve this.

    Can you give me an idea to achieve this ?

    Have other users already asked about this?

    Thank you

    function:
    

    function custom_price_message( $price ) {
    global $post;
    $product_id = $post->ID;
    $textafter = ‘ P.V.P.’; //texto que vamos agregar
    return $price . ” . $textafter . ”; //class textafter para el CSS
    }
    add_filter( ‘woocommerce_get_price_html’, ‘custom_price_message’ );

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Josh Kohlbach

    (@jkohlbach)

    Hi @miguelitolaparra thanks for writing in!

    The easiest way would be to use the Wholesale Prices Premium plugin which is the addon to this free plugin. It has an option under the Tax settings called Wholesale Price Suffix that lets you do exactly what you’re needing:

    View post on imgur.com

    If you wanted to custom code this, you could modify your snippet there to check the user’s role and if it’s a wholesale role on your site, change the $textafter to use a different value.

    Hope this gives you a path to follow up!

    Thread Starter miguelitolaparra

    (@miguelitolaparra)

    Thank you for your response and I apologize for the delay in responding.

    I really like the plugin, but unfortunately at the moment we can’t afford to buy the premium version.
    It is a small company that is starting.
    I will look for another solution.
    You tell me that I can modify $textafter, but I can’t find this, in what file is this instruction exactly
    Thanks for spend your time on me

    Plugin Author Josh Kohlbach

    (@jkohlbach)

    Hi @miguelitolaparra no problem, we’re happy to welcome you when you’re at that stage ??

    The $textafter I spoke about is in your own snippet you provided above.

    I don’t know any more about the context of that snippet, but I guess you could modify it to grab the user role and check if that is one of your wholesale roles.

    Something along the lines of this before the return:

    
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = (array)$user->roles;
        $current_user_role = isset( $roles[0] ) ? $roles[0] : 'guest';
    
        if ( 'wholesale_customer' === $current_user_role ) {
            $textafter = 'P.V.M.';
        }
    }
    

    Note I haven’t tested this, so you’ll need to adapt this yourself.

    Thread Starter miguelitolaparra

    (@miguelitolaparra)

    Thank you for your attention and sorry for the delay in my response, I was testing.

    I’m not doing things right because now, it always shows P.V.M.

    in the store and had a function that changed the text behind the price, and displayed this text behind the price: P.V.P.
    I did it with the following function:

    //funcion para colocar P.V.P
    function custom_price_message( $price ) { 
        global $post;
        $product_id = $post->ID;
        $textafter = '   P.V.P.'; //texto que vamos agregar
        return $price . '' . $textafter . ''; //class textafter para el CSS
    }
    add_filter( 'woocommerce_get_price_html', 'custom_price_message' );

    Now I have installed woocommerce-wholesale-prices and I would like that in the normal view the text “P.V.P” should be displayed behind the price, and when the user is registered with the Wholesaler role, the text should be the following: P.V.M )

    This is the function that I have modified.

    
    function my_get_current_user_roles() {
      if( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
        return $roles; // This will returns an array
      } else {
        return array();
      }
    }
    
    function custom_price_message( $price ) { 
     global $post; 
     $product_id = $post->ID; 
     $roles = my_get_current_user_roles();
     $textafter = "P.V.M.";
     if(!empty($roles)) { 
    	if (in_array("Wholesale Customer", $roles)) {
    		$textafter = 'P.V.M.'; //texto que vamos agregar 
    	}
    	if (in_array("Cliente", $roles)) {
    		$textafter = 'P.V.P.'; //texto que vamos agregar 
    	}
     } 
     return $price . '' . $textafter . ''; //class textafter para el  
    } 
    add_filter('woocommerce_get_price_html', 'custom_price_message');

    However, this does not work and as you can see in the first screenshot (https://ibb.co/N690HDz ), it always shows the Wholesale price, even though we are not registered

    Well, it should always show P.V.P behind the price, except when the user has the Wholesale Customer rool, the following screenshot is a user with the Wholesale Customer rool https://ibb.co/gzg8kH4

    I think my function is not well elaborated, can you help me?

    I have also discovered that when we are registered with the Wholesale Customer rool, the text “Wholesale Price” is displayed.
    Can you eliminate this, or even if this text we change it for the text that the wholesale price needs, P.V.M, that would be great,
    https://ibb.co/s3bCS1L
    this would be possible ?
    You can help ?

    Plugin Author Josh Kohlbach

    (@jkohlbach)

    Hi @miguelitolaparra yep, it gets pretty complicated which is why I originally suggested the premium plugin because we have quite a bit of extra logic around this to ensure it displays price suffixes properly.

    You might be able to switch to using the filter “woocommerce_get_price_suffix” in WooCommerce. You can set the price suffix for retail prices under WooCommerce->Settings->Tax, Price suffix. That filter should let you override it.

    Thread Starter miguelitolaparra

    (@miguelitolaparra)

    I understand
    Thank you for your support these days,
    As I told you, now I must reduce expenses as much as possible until I can start my business.
    But don’t worry, I’ll find another way to get what I’m looking for with another Plugin.
    I wish you a happy day @jkohlbach and good luck with your sales.

    Plugin Author Josh Kohlbach

    (@jkohlbach)

    Totally understand @miguelitolaparra. I do think you’re on the right track, if you swap to using the filter I suggested for the price suffix, you’ll be there I think.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to add a different text to the price depending on the type of user Rool?’ is closed to new replies.