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.