Elodie Gaborit
Elodie Gaborit

Reputation: 21

Avoid Dokan vendors to purchase their own product in WooCommerce

I use Dokan and WooCommerce. I want to hide the add to cart buttons on the products, so sellers do not have payment set up in their seller dashboard.

I have the following function, but it does not work:

add_filter( 'woocommerce_is_purchasable','woocommerce_available_payment_gateways' function( $show_cart, $product, $available_payment_gateways ) {

    $seller_id = get_post_field('post_author', $product->get_id());

    if(isset($available_payment_gateways( $seller_id )) {
        $show_cart = true;
    } else {
        $show_cart = false;
    }
    return $show_cart;
}, 10, 2 );

Upvotes: 0

Views: 449

Answers (3)

Yeasin Arafat
Yeasin Arafat

Reputation: 1

This has been added as a default feature of Dokan plugin.

Upvotes: 0

Elodie Gaborit
Elodie Gaborit

Reputation: 21

We found that dokan simply displays a message at the end when the customer enters his card to indicate that payment for the product is not possible because the seller has not entered a payment method. We would rather hide the add to cart button from visitors on products from sellers who have not entered a payment method in their seller dashboard.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254221

There are a lot of mistakes in your code, and your question is not very clear. So I guess that you want to avoid a seller to be able of purchasing its own products.

Then try the following:

add_filter('woocommerce_is_purchasable', 'filter_woocommerce_is_purchasable', 10, 2 );
function filter_woocommerce_is_purchasable( $purchasable, $product ) {
    $seller_id = get_post_field('post_author', $product->get_id());

    if( get_current_user_id() == $seller_id ) {
        $purchasable = false;
    }

    return $purchasable;
}

Code goes in function.php file of your active child theme (or active theme). It should work.

Upvotes: 1

Related Questions