Kolawole Emmanuel Izzy
Kolawole Emmanuel Izzy

Reputation: 1052

How to check if a product is created by a user in woocommerce

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.

Upvotes: 2

Views: 2120

Answers (1)

Noah
Noah

Reputation: 570

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' );

Upvotes: 2

Related Questions