Okay
  Public Ticket #3716234
Can I disable multiple vendor checkout?
Closed

Comments

  •  6
    Eana started the conversation

    Is there a way to disable multiple vendor checkout?

    Would like to only allow 1 vendor per checkout.


    Thanks

  •  2,218
    WebWizards replied

    Hello Eana,

    One way to achieve this is to add the following PHP code snippet to your site:

    // Add a filter to check the cart before proceeding to checkout
    add_action( 'woocommerce_check_cart_items', 'check_cart_authors' );
    function check_cart_authors() {
        // Initialize an empty array to store author IDs
        $author_ids = array();
        // Loop through each item in the cart
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            // Get the author ID of the current item
            $author_id = get_post_field( 'post_author', $cart_item['product_id'] );
            // If the author ID is not in the array, add it
            if ( ! in_array( $author_id, $author_ids ) ) {
                $author_ids[] = $author_id;
            }
        }
        // If there is more than one author in the cart, show an error message and prevent checkout
        if ( count( $author_ids ) > 1 ) {
            wc_add_notice( __( 'You cannot checkout because the cart contains products from different vendors.', 'woocommerce' ), 'error' );
            // Remove the "Proceed to Checkout" button
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
    

    It will show an error on the cart page if there are multiple vendors:

    https://prnt.sc/QvNB_x5PKinf

    The code can be added to functions.php or to any snippets plugin.


    Also, to remove the standard message there saying “The products in your cart are sold by multiple different vendor partner….” you can go to MarketKing -> Settings -> Language & Text, and delete it.


    Kind regards,

    Thomas

  •  6
    Eana replied

    Thank you for this, it is such a great help!