2

Good Day, i'm trying to check if a product is created by a user and if no, the add to cart button should be removed.

I'm working on a store whereby a registered user can create product from frontend. I added this argument to create the product from frontend;

$post = array(
        'post_author' => $currentCOUser_ID // This Return the User's ID using wp_get_current_user()->ID
        'post_status' => "publish",
        'post_excerpt' => $pProduct_excerpt,
        'post_title' => $ProductTitle,
        'post_type' => "product",
    );

    //create product for product ID
    $product_id = wp_insert_post( $post, __('Cannot create product', 'izzycart-function-code') );

When product is created i only want the author and admin to be able to see the add to cart button on the product single page. I used the below code but didn't work;

function remove_product_content() {
    global $post;
    $current_user = wp_get_current_user();
    $product_author_id = $current_user->ID;
    $admin_role = in_array( 'administrator', (array) $current_user->roles );


    //check if is a product & user is logged in and is either admin or the author
    //is a product and user is not logged in, remove add to cart
    //is a product and user is logged in and not either admin or product author, remove add to cart button
    if ( is_product() && is_user_logged_in() && (!$admin_role || $product_author_id != $post->post_author)  ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
}
add_action( 'wp', 'remove_product_content' );

when i run the above code, it completely hide the add to cart button from everyone. Not sure what i'm doing wrong. Thanks for your help.

1 Answers1

2

You are missing an ampersand. It should be &&. & is a bit wise AND. See this link for more details on the difference between the two: https://stackoverflow.com/a/2376353/10987825

Also, wrap the || portion of the statement in parentheses. Otherwise, as long as the current user is not the author, it will be hidden. Check these two links to see the difference.

Incorrect Version

Correct Version

So your code would become:

function remove_product_content() {
    global $post;
    $current_user = wp_get_current_user();
    $product_author_id = $current_user->ID;


    //check if is a product & user is logged in and is either admin or the author
    //is a product and user is not logged in, remove add to cart
    //is a product and user is logged in and not either admin or product author, remove add to cart button
    if ( is_product() && (!is_user_logged_in() || (!is_admin() && $product_author_id != $post->post_author)) ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
}
add_action( 'wp', 'remove_product_content' );
Noah
  • 550
  • 2
  • 8
  • i have added the ampersand and yet, the add to cart button doesn't show [updated my code] – Kolawole Emmanuel Izzy Feb 01 '19 at 00:14
  • I’ve updated my answer. I think you need parentheses around the “or” statement otherwise it is sort of like “(a and b) or c – Noah Feb 01 '19 at 00:21
  • Still not working, i have created another user and created product from frontend with the user who is not an admin, yet cant view the add to cart button as the Product Author – Kolawole Emmanuel Izzy Feb 01 '19 at 00:53
  • Come to think of it, based on your question it should be an &&. You want it to be hidden if “a product page and (either not logged in or (not an admin and not the author )) – Noah Feb 01 '19 at 01:01
  • I've done that but not working and i was able to figure out that is_admin() doesn't check if user is administrator [as seen here](https://developer.wordpress.org/reference/functions/is_admin/). I have updated my code to detect administrator (which worked). I think the culprit here is the `$product_author_id != $post->post_author` , as this doesn't check if the user is author. This is where i need help now – Kolawole Emmanuel Izzy Feb 01 '19 at 01:36
  • It looks like you’re saving the user display name. You should save the user ID instead. – Noah Feb 01 '19 at 01:38
  • i just modified the code to save the user ID and not the display name and it worked. Thanks. Will update my code – Kolawole Emmanuel Izzy Feb 01 '19 at 01:56