Created
November 19, 2020 04:22
-
-
Save digitalchild/ddbd93fad833335ec986b9b35e56d35d to your computer and use it in GitHub Desktop.
Limit cart and checkout to a single vendor stores products in WC Vendors Marketplace and WC Vendors Pro.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Limit checkout to a single vendor store | |
add_action( 'woocommerce_add_to_cart_validation', 'limit_single_vendor_to_cart', 10, 3 ); | |
function limit_single_vendor_to_cart( $valid, $product_id, $quantity ) { | |
$vendor_id = get_post_field( 'post_author', $product_id ); | |
// loop through the cart to check each vendor id | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$cart_product_id = $cart_item[ 'product_id' ]; | |
$cart_vendor_id = get_post_field( 'post_author', $cart_product_id ); | |
if( $cart_vendor_id != $vendor_id ) { | |
$valid = false; | |
break; | |
} | |
} | |
if ( ! $valid ){ | |
// Display a message in the cart. | |
wc_clear_notices(); | |
wc_add_notice( __( "Cart is limited to products from a single store at a time. Please checkout before purchasing from another store."), 'error' ); | |
} | |
return $valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment