Toggle Buy Button by Group

Tags

I have a membership site using Groups and Groups For Woocommerce. Is there a way to hide the Buy Now / Add to Cart button on a per product basis if a user is in certain Groups?

27 Responses to Toggle Buy Button by Group

  1. George October 7, 2015 at 7:52 pm #

    Hi Robert,

    Try the following snippet, with this you should be ok with your configuration.
    The snippet is here, copy and paste it in your functions.php file replacing the previous snippet i have sent you.

    Cheers

  2. Robert September 29, 2015 at 9:41 pm #

    I’m sorry. I was hoping to save you time. My current products are as follows:

    Monthly Sermon Partner
    A Celebration of Discipline Guidance
    Achieving True Kingdom Greatness
    Circle Maker Dream Big
    Circle Maker Keep Circling Jericho
    Circle Maker Pray Hard
    Circle Maker What Is Your Jericho
    Circle Maker: Circle Makers Overcoming Obstacles And Hindrances in Prayer
    Circle Maker: Think Long
    Four Year Master Plan
    How to Use The Weapons of God for Victory
    Money vs Treasure The Pursuit of the Glory
    My Father’s Name Sake
    The House That Sin Built

    Each product above has an associated group of the same name.

    The first product is the monthly membership product. This is the only product that when purchased will place the user in the group that will make all products unpurchasable.

    All the other products, when purchased, will place the user in a unique group of the same name as the product. All users in that group will see that product as unpurchasable.

    So there will only be 2 groups that make any given product unpurchasable. The first is the Monthly Sermon Partner. That group will make all products unpurchasable. The second group that will make any single product unpurchasable is the group for that single product only, and it has the same name as the product.

    Does that information help you?

  3. George September 29, 2015 at 7:46 pm #

    Hi Robert,

    You didn’t put it the right way.

    Do this

    f ( $is_a_member_Monthly_Sermon_Partner || $is_a_member_A_Celebration_of_Discipline_Guidance || put all the rest here seperated by || ) {
    $purchaseable = false;
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }

    The last two lines will also remove the Read more/Buy buttons.

    It would be a lot easier for both of us though, if you have stated all your requirements from the beginning.

    Kind regards,
    George

  4. Robert September 29, 2015 at 12:53 pm #

    I see. But seems like my code is a bit off. Now all the products are unpurchasable. I’ll show you my code below, but here is another issue that code is causing. While it hides the Buy button on the single products pages, it is causing the Add To Cart button on the shop page (which I customized to say Access Full Sermon, and to have a different color) to revert back to the default styling and to say Read More. I need the Add To Cart / Read More button to also be hidden from the shop page. Here is my code, which I’m sure is wrong. I’m hoping you’ll be able to immediately see what is wrong and tell me how to fix it.

    Looking forward to your correction, or is that I need a separate function for each product I need to make unpurchasable based on its unique group?

    add_filter(‘woocommerce_is_purchasable’, ‘your_custom_function’, 10, 2);
    function your_custom_function( $purchaseable, $product ) {
    $purchaseable = true;
    $is_a_member_Monthly_Sermon_Partner = false;
    $is_a_member_A_Celebration_of_Discipline_Guidance = false;
    $is_a_member_Circle_Maker_Pray_Hard = false;
    $is_a_member_Achieving_True_Kingdom_Greatness = false;
    require_once( ABSPATH . ‘wp-includes/pluggable.php’ );
    if ( $group = Groups_Group::read_by_name( ‘Monthly Sermon Partner’ ) ) {
    $is_a_member_Monthly_Sermon_Partner = Groups_User_Group::read( get_current_user_id() , $group->group_id );
    }
    if ( $group = Groups_Group::read_by_name( ‘A Celebration of Discipline Guidance’ ) ) {
    $is_a_member_A_Celebration_of_Discipline_Guidance = Groups_User_Group::read( get_current_user_id() , $group->group_id );
    }
    if ( $group = Groups_Group::read_by_name( ‘Circle Maker Pray Hard’ ) ) {
    $is_a_member_Circle_Maker_Pray_Hard = Groups_User_Group::read( get_current_user_id() , $group->group_id );
    }
    if ( $group = Groups_Group::read_by_name( ‘Achieving True Kingdom Greatness’ ) ) {
    $is_a_member_Achieving_True_Kingdom_Greatness = Groups_User_Group::read( get_current_user_id() , $group->group_id );
    }

    if ( $is_a_member_Monthly_Sermon_Partner ) {
    $purchaseable = false;
    }
    if ( $is_a_member_A_Celebration_of_Discipline_Guidance ) {
    $purchaseable = false;
    }
    if ( $is_a_member_Circle_Maker_Pray_Hard ) {
    $purchaseable = false;
    }
    if ( $is_a_member_Achieving_True_Kingdom_Greatness ) {
    $purchaseable = false;
    }

    return $purchaseable;
    }

  5. George September 29, 2015 at 9:54 am #

    Yes exactly.
    If there is a logic connection between group name and product name like this
    ie
    product 1 would belong to group 1
    .
    .
    product 343 would belong to group 343.

    Then you can check for the names easily.

    Cheers

  6. Robert September 28, 2015 at 10:16 pm #

    The name of the group for each product is the same as the product, with rare exception, but when I’m done, all groups will have the same name as the product you have to buy to access that group.

    Are you saying I can just add a new line or code or 2 to the piece of code in functions.php to create an if then statement for every product I add…………to………:

    Make Product 1 unpurchasable if user is a member of Group 1
    Make Product 2 unpurchasable if user is a member of Group 2
    Make Product 3 unpurchasable if user is a member of Group 3
    Make Product 4 unpurchasable if user is a member of Group 4
    Make Product 5 unpurchasable if user is a member of Group 5

    etc……

    Is that what you are thinking about?

  7. George September 28, 2015 at 8:03 pm #

    Hi Robert,

    If you have a predefined list of groups for the products, please put it in a comment here.

    Thanks.

  8. Robert September 26, 2015 at 2:18 pm #

    ok thanks. And yes that is the part that I was afraid I wasn’t communicating properly to you.

    Each product has its own group that will make that product unpurchasable for logged in uses that have already paid for it. Basically, the product short description has the audio player in it, unlocked only for users who have purchased it. So they can access the file without have to “buy” it again for free (since I’m using a Groups Pricing plugin that I set to make the price of each product $0 for users who have already bought it based on the group that purchase put them in.

    The only Monthly Membership product is the only group that will make products free (or unpurchasable with no add to cart button) site wide.

    I look forward to your reply of what options you find for what I am buliding.

    Thanks again,
    Rob

  9. George September 26, 2015 at 10:11 am #

    Hi Robert,

    Since the group for every product will be different, then the second part won’t work for you. Let me check if there is another solution for that.

    Cheers

  10. Robert September 25, 2015 at 1:38 pm #

    George,

    2 questions:

    1. My monthly membership plan that will will hide Add to Cart buttons site-wide is called “Monthly Sermon Partner”. So is my syntax correct below?

    add_filter(‘woocommerce_is_purchasable’, ‘group_hide_add_to_cart_button’, 10, 2);
    function group_hide_add_to_cart_button( $purchaseable, $product ) {
    $purchaseable = true;
    $is_a_member_Monthly_Sermon_Partner = false;
    $is_a_member_red = false;
    require_once( ABSPATH . ‘wp-includes/pluggable.php’ );
    if ( $group = Groups_Group::read_by_name( ‘Monthly Sermon Partner’ ) ) {
    $is_a_member_Monthly_Sermon_Partner = Groups_User_Group::read( get_current_user_id() , $group->group_id );
    }
    if ( $group = Groups_Group::read_by_name( ‘Red Members’ ) ) {
    $is_a_member_red = Groups_User_Group::read( get_current_user_id() , $group->group_id );
    }
    if ( $is_a_member_Monthly_Sermon_Partner && $is_a_member_red ) {
    $purchaseable = false;
    }

    return $purchaseable;
    }

    2. The second group you have here is Red Group. The problem for me is that the second group that will hide the Add To Cart button will be different for every product. So is there a way for me to make the code do something like this:

    A. First check if user if in Monthly Sermon Group. If so, hide Add to Cart button (which I see in your code)…but then…
    B. If Product is Red, and if user is member of Red Group, then hide Add to Cart Button.

    Of course that code will get very long as I add 1 or 2 products and groups per week, but it’s not too much to manage, and if that code is possible, that will be my ideal situation. Can it be done?

  11. George September 25, 2015 at 9:03 am #

    Hi Robert,

    You can replace the previous snippet i’ve sent you with this one here.
    You can delete the previous snippet in your functions.php file and put the new one from the link above.

    Hope it fits your configuration.

    Cheers

  12. Robert September 24, 2015 at 2:06 pm #

    Ah, so are you saying that I apply the code you gave me to each product? I thought it went in functions.php.

    But how would I apply that code to each product separately, or am I misunderstanding what you are saying?

    Thanks again for all you help!

  13. George September 24, 2015 at 11:11 am #

    So since each customer is placed to the product Group after purchasing the product, then you can check for this group the same way we did for the previous group.

    Cheers

  14. Robert September 23, 2015 at 3:26 pm #

    Thanks so much. I will use your code.

    However, there is only 1 group that will make product unpurchasable site wide. We’ll call that group “Monthly Member”

    But each product also has its own group that all customers who purchase each product are placed in.

    So each product will be unpurchasable by the “Monthly Member” group, but also be one more group that will unique to each product.

    How do I accomplish that with your code? Is that what you are referring to by excluding products?

    Thanks,
    Rob

  15. George September 23, 2015 at 3:17 pm #

    Hi Robert,

    Please check the filter here and paste it in your functions.php file of your child theme. The only thing you have to change is “Blue Members” to your desired group.
    As for the products, it depends on how you want to exclude products, ie by id or category.

    Kind regards,
    George

  16. George September 22, 2015 at 8:30 pm #

    Hi Robert,

    No worries, I will prepare it for you and reply the snippet here.

    Kind regards,
    George

    • Robert September 22, 2015 at 9:31 pm #

      George,

      That would be very gracious of you and much appreciated! I will await your reply.

  17. Robert September 22, 2015 at 1:47 pm #

    George,

    Thanks so much for this, but unfortunately, my skill at php is insufficient to understand how to properly edit the code you gave me.

    It looks like I’m supposed to create a custom function name, and set the group the filter checks for. But I’m not sure in which all places I need to make these edits.

    Also, how do I make the code filter for a different group for each product?

    If this is too much for you to support, I understand. If you cannot guide me through the process, I guess I will have to seek a developer, or forego this this feature for now.

  18. George September 22, 2015 at 8:38 am #

    Hi Robert,

    The filter will help you set the product as purchaseable or not. Here is an example:
    http://pastebin.com/raw.php?i=j2kau5dN

    Kind regards,
    George

  19. Robert September 22, 2015 at 3:33 am #

    Perhaps this is just above my head, but I still don’t see how can use this filter to check if the buyer is a member of a specific group for each and every product. Each product has its own group, so the filter will need to check for a different group for each and every product we sell. Is that possible with the filter?

  20. George September 21, 2015 at 8:57 am #

    Yes Robert, the result is the same.
    If the user belongs to a group, then he doesn’t have the ability/capability to buy the product.

    Cheers

  21. George September 20, 2015 at 5:31 pm #

    Since you are trying to sell a product based on who is trying to buy it, i believe it is better to check if the user belongs to the specific group.

    Cheers

    • Robert September 20, 2015 at 9:44 pm #

      I didn’t tell you fully what I’m trying to do. It’s not that I’m trying to sell a product only to certain people. My product short description has content in it that is displayed to members of certain groups. And I have edited functions.php to display product short description on the shop page. Once someone buys a particular product, they are placed in a particular group exclusively for that product. From then on, I want them to no longer see the buy button on the shop page for that product. Instead they only see the unlocked content.

      I hope I am being clear. With that being said, do you think your suggestion will work for me?

  22. Robert September 20, 2015 at 5:04 pm #

    Well I don’t want to hide the actual products from anyone. All products are to always be visible. But I want the Add To Cart button for each product to be hidden from certain people depending which group they are in. Will that be possible?

  23. George September 20, 2015 at 4:59 pm #

    Well Robert,

    If you restrict access to a product, then if the user belongs to the group they cannot purchase, then they will not be able to even see the product, so its easier like that and you don’t have to use the filter to recheck.

    Kind regards,
    George

  24. George September 19, 2015 at 9:52 am #

    Hi Robert,

    Although i haven’t tried it myself, there is this filter add_filter('woocommerce_is_purchasable', 'your_custom_function', 10, 2);
    where you can check if the current user belongs to foobar group, then the product is purchasable. Otherwise if not or if he/she is still a visitor/guest then the product is not purchasable.

    Kind regards,
    George

    • Robert September 19, 2015 at 1:59 pm #

      Os it possible to use that filter so that each product has a different group the filter checks for.

      In other words, there is only one group that if a user is a member of, they cannot purchase anything on the site.

      Other than that, each and every product on the site is associated with its own unique group that the members of should not be able to buy that particular product.

      Could that filter do that?

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account

Share