Untitled

 avatar
webwizards
plain_text
17 days ago
3.2 kB
11
Indexable
function custom_grouped_quantity_filter($quantity, $variation_id, $product_id) {
    
    // ============================================
    // CONFIGURATION: Define your product groups here
    // ============================================
    $product_groups = array(
        // Group 1: Mix of categories and individual products
        'group_1' => array(
            'categories' => array('category-a', 'category-b'), // Category slugs
            'products' => array(123), // Individual product IDs (use empty array if none)
        ),
        
        // Group 2: Another mix
        'group_2' => array(
            'categories' => array('category-d', 'category-f'),
            'products' => array(456, 789), // Product IDs
        ),
        
        // Group 3: Only categories (no individual products)
        'group_3' => array(
            'categories' => array('category-x', 'category-y', 'category-z'),
            'products' => array(),
        ),
        
        // Group 4: Only individual products (no categories)
        'group_4' => array(
            'categories' => array(),
            'products' => array(111, 222, 333),
        ),
    );
    // ============================================
    // END CONFIGURATION
    // ============================================
    
    // Determine which group the current product belongs to
    $current_product_group = null;
    
    foreach ($product_groups as $group_name => $group_config) {
        // Check if product is in this group's categories
        foreach ($group_config['categories'] as $category_slug) {
            if (has_term($category_slug, 'product_cat', $product_id)) {
                $current_product_group = $group_name;
                break 2;
            }
        }
        
        // Check if product ID is directly in this group
        if (in_array($product_id, $group_config['products'])) {
            $current_product_group = $group_name;
            break;
        }
    }
    
    // If product doesn't belong to any group, return default quantity
    if ($current_product_group === null) {
        return $quantity;
    }
    
    // Calculate total quantity for all products in the same group
    $total_quantity = 0;
    $group_config = $product_groups[$current_product_group];
    
    foreach (WC()->cart->get_cart() as $cart_item) {
        $cart_product_id = $cart_item['product_id'];
        $include_item = false;
        
        // Check if cart item belongs to the same group (via categories)
        foreach ($group_config['categories'] as $category_slug) {
            if (has_term($category_slug, 'product_cat', $cart_product_id)) {
                $include_item = true;
                break;
            }
        }
        
        // Check if cart item belongs to the same group (via product ID)
        if (in_array($cart_product_id, $group_config['products'])) {
            $include_item = true;
        }
        
        if ($include_item) {
            $total_quantity += $cart_item['quantity'];
        }
    }
    
    return $total_quantity;
}

add_filter('b2bking_cart_item_quantity_tiers', 'custom_grouped_quantity_filter', 10, 3);
Editor is loading...
Leave a Comment