Search code examples
woocommerceconditional-statementsbuddypress

Show button "Add to cart" of WooCommerce only to my friends buddypress


I created a site with buddypress and WooCommerce and everyone can upload products to the store. The idea is that if you're a friend of the author, you can buy the product.

I have customized the file simple.php WooCommerce / single-product / add-to-cart / folder and I've added this code:

global $bp;
if (friends_check_friendship( !bp_displayed_user_id(), bp_loggedin_user_id())) { 
    echo ' Add To Cart';
}
elseif (!friends_check_friendship( !bp_displayed_user_id(), bp_loggedin_user_id())) { 
echo 'Only Friends';
}

But it does not work. This code only works if you are the administrator, and always have to be a friend of the administrator to display the "Add To Cart" button. Why is this happening?


Solution

  • bp_displayed_user_id() only works on BuddyPress profile pages.

    Assuming the single-product page was created by a specific user, you need the author id.

    Try this:

    if (friends_check_friendship( get_the_author_meta('ID'), bp_loggedin_user_id())) { 
        echo ' Add To Cart';
    }
    else
        echo 'Only Friends';