Search code examples
phpwordpresswoocommercecart

How can I create a cart for each brand in Woocommerce


So I'm using Woo Brand plugin to display all brands that i'm using in my store. What I want to do is limit customers to buy from several brands at once. For example, if I have Brand A and Brand B, the customer can add products to the cart only from brand A or only from brand B. If they have products in the basket only from brand A and they go to the page from brand B, on the page from brand B I want to have a new basket, in which they can only add products from brand B. If they return to the page from brand A, they will still have in their basket the products added only from brand A. How can I achieve this?

Currently I have this piece of code which can add in cart only from a specific brand e.g (If i'm on brand A page, I can add only from they)

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_brand_allowed', 20, 3 );
function only_one_product_brand_allowed( $passed, $product_id, $quantity) {

// Getting the product brand term slugs in an array for the current product
$brand_slugs = wp_get_post_terms( $product_id, 'product_brand', array( 'fields' => 'slugs' ) );


$cart_contents = WC()->cart->get_cart();
$cart_item_keys = array_keys ( $cart_contents );

// Get the brand name for first item from cart
$first_item = $cart_item_keys[0];
$first_item_id = $cart_contents[$first_item]['product_id'];
$brand_name = get_the_terms($first_item_id, 'product_brand');
$current_product_brand = get_the_terms($product_id, 'product_brand');




// Loop through cart items
foreach ($cart_contents as $cart_item_key => $cart_item ){

    // Check if the product category of the current product don't match with a cart item
    if( ! has_term( $brand_slugs, 'product_brand', $cart_item['product_id'] ) ){

        // phpAlert('Trebuie golit cosul');

        // Displaying a custom notice
       wc_add_notice( __('You can add products to the cart only from the same brand. In your cart are products from <strong>'.$brand_name[0]->name. '.' ), 'error' );

        // Avoid add to cart
        return false; // exit
    }
}
return $passed;
}

Solution

  • You can't have in WooCommerce different baskets (cart) for each product brand, you can only avoid customer to have combined items from different brands and optimize your code like:

    add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_brand_allowed', 20, 3 );
    function only_one_product_brand_allowed( $passed, $product_id, $quantity) {
        $taxonomy    = 'product_brand';
        $field_names = array( 'fields' => 'names');
    
        // Getting the product brand term name for the current product
        if( $term_name = wp_get_post_terms( $product_id, $taxonomy, $field_names ) ) {
            $term_name = reset($term_name);
        } else return $passed;
    
    
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item ){
            // Get the cart item brand term name
            if( $item_term_name = wp_get_post_terms( $cart_item['product_id'], $taxonomy, $field_names ) ) {
                $item_term_name = reset($item_term_name);
            } else continue;
    
            // Check if the product brand of the current product exist already in cart items
            if( isset($term_name) && isset($item_term_name) && $item_term_name !== $term_name ){
                // Displaying a custom notice 
                wc_add_notice( sprintf( __("You are not allowed to combine products from different brands. There is already a cart item from <strong>%s</strong> brand.", "woocommerce" ), $item_term_name ), 'error' );
    
                // Avoid add to cart and display message
                return false;
            }
        }
        return $passed;
    }
    

    Code goes in function.php file of the active child theme (or active theme). Tested and works.

    Now what you could do is to spit an order in multiple sub-orders, one for each brand (or merchant), keeping your original Order.