Hide add to cart from WCFM Marketplace vendors except admins in Woocommerce

135 Views Asked by At

I'm using WCFM Marketplace plugin for WooCommerce and I want to hide add to cart for all vendors except admin.

I found some code that hide add to cart for some categories, I think it could be modified and used to hide add to cart for vendors.

add_filter( 'woocommerce_is_purchasable', function( $is_purchasable, $product ) {
    $pcategories = get_the_terms( $product->get_id(), 'product_cat' );
    if( !empty($pcategories) ) {
        foreach($pcategories as $pkey => $pcategory) {
            if( absint($pcategory->term_id) == 197 ) {
                $is_purchasable = false;
            }
        }
    }
    return $is_purchasable;
}, 500, 2 );

I also found a code that hide add to cart for author:

/* 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 );
    }
}

I tried some changes in both codes but no one work for me.

Any help is appreciated.

1

There are 1 best solutions below

0
Aminor On

Finally I found the solution: As admin is not a vendor, I added a conditional CSS that hides the "Add to cart" button on the product page if the author is a vendor.

    add_action( 'wp_head', function () {
    // Check if it's a product page
    if ( is_product() ) {
        global $product;
        
        // Check if it's a Vendor product WCFM
        $vendor_id = get_post_field( 'post_author', $product->get_id() );
        if ( wcfm_is_vendor( $vendor_id ) ) {
            // It's a vendor product
            ?>
            <style>
                .woocommerce div.product form.cart { display: none; }
            </style>
            <?php
        }
    }
    } );