oyshiu
oyshiu

Reputation: 85

Woocommerce - Remove add to cart button when user is the product author

I'm trying to remove the Add to Cart button when Current user is the logged in user and add the Edit Product link. But this is totally breaking my design and not working:

Any help? Thanks!!

Upvotes: 0

Views: 537

Answers (2)

Reigel Gallarde
Reigel Gallarde

Reputation: 65274

Please try this code instead. Place this on your current theme's functions.php

add_action( 'woocommerce_shop_loop', 'custom_woocommerce_shop_loop' );

function custom_woocommerce_shop_loop() {

    global $post;
    $current_user = wp_get_current_user();

    if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );;
        add_action( 'woocommerce_after_shop_loop_item', 'btn_edit_own_product', 10 );

    }
}
function btn_edit_own_product() {
    edit_post_link('Edit Product');
}

Upvotes: 0

Sunil Dora
Sunil Dora

Reputation: 1472

Try this code,

/* remove add-to-cart from shop page for product author  */
add_action('woocommerce_after_shop_loop_item_title','user_filter_addtocart_for_shop_page') ;
function user_filter_addtocart_for_shop_page(){
    $user_id = get_current_user_id();
    $author_id = get_post_field('post_author', get_the_ID());
    if($user_id == $author_id){
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    }
}

/* remove add-to-cart from single product  page for product author  */
add_action('woocommerce_before_single_product_summary','user_filter_addtocart_for_single_product_page') ;
function user_filter_addtocart_for_single_product_page(){
    $user_id = get_current_user_id();
    $author_id = get_post_field('post_author', get_the_ID());
    if($user_id == $author_id){
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
}

Hope this will helps you.

For more details visit,

woocommerce- hide add to cart button for product author

Upvotes: 1

Related Questions