By default, the checkout process in WCFM is designed for multiple vendors, but what if you want to enable the single-vendor checkout option?

This can be useful if you have vendors that only sell a few products and you want to streamline the checkout process for your customers.

Here’s the simple snippet code that will enable the single vendor checkout option in your WCFM marketplace:

PHP Snippet: Enable Single Vendor Checkout in WCFM Marketplace

/**
* @snippet Change WooCommerce ‘Add to Cart’ button to 'View Product'
* @source https://www.wptechnic.com/?p=4615
* @compatible WC 6.3.1
*/
// Enable Single Vendor Checkout in WCFM
add_action( 'woocommerce_add_to_cart_validation', function( $is_allow, $product_id, $quantity ) {
$product = get_post( $product_id );
$product_author = $product->post_author;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_product_id = $cart_item['product_id'];
$cart_product = get_post( $cart_product_id );
$cart_product_author = $cart_product->post_author;
if( $cart_product_author != $product_author ) {
$is_allow = false;
break;
}
}
if( !$is_allow ){
// We display an error message
wc_clear_notices();
wc_add_notice( __( "Well, you already have some item in your cart. First checkout with those and then purchase other items!", "wcfm" ), 'error' );
}
return $is_allow;
}, 50, 3 );

How to add PHP Snippet code to your WordPress site?

There are many ways of adding a PHP Snippet code to your WordPress website. You can add this PHP Snippet at the very bottom of your active child theme (or main theme) functions.php file. You can also add this code using any third-party plugin like the ‘Insert Headers and Footers‘ or ‘Code Snippets‘ plugin.

single vendor checkout in wcfm phpsnippet

That’s it!

With this code, you have now enabled the single vendor checkout option in WCFM.

With this code, customers can only purchase products from a single vendor. If your customers try to add products from another vendor, they’ll get to see the Error message like this:

single vendor checkout in wcfm

Did this code work for your website? Or you’re having difficulties? Either way, let me know by leaving a comment below right now.

Avatar for Muhammad Tamzid
Author

Muhammad Tamzid, the founder of WPTechnic, is a WordPress Enthusiast with a passion to help beginners learning WordPress. Also managing WPrevival, a 24/7 WordPress Website Development, Maintenance & Security Service company.

Write A Comment