---
description: Multi-vendor marketplace Phase 1 - Foundation complete
---

# Multi-Vendor System - Phase 1

## Overview
Transform Awesome Mall into multi-vendor marketplace where vendors can register and sell products.

## Phase 1: Core Foundation ✅ COMPLETE

### 1. Database Schema

**File:** `/setup/create_vendor_tables.sql`

**Tables Created:**

1. **vendors** - Store vendor profiles
   - user_id, store_name, store_slug
   - business_email, business_phone, business_address
   - commission_rate, status (pending/active/suspended/rejected)
   - created_at, approved_at, approved_by

2. **vendor_earnings** - Track earnings per order
   - vendor_id, order_id, product_id
   - order_amount, commission_rate, commission_amount
   - vendor_earning, status (pending/processing/paid)

3. **vendor_payouts** - Manage payouts
   - vendor_id, amount, payout_method
   - transaction_id, reference_number
   - status, requested_date, processed_date

4. **vendor_reviews** - Customer reviews
   - vendor_id, user_id, order_id
   - rating (1-5), review_title, review_text
   - status (pending/approved/rejected)

5. **vendor_notifications** - Notification system
   - vendor_id, title, message, type, link
   - is_read, created_at

6. **vendor_settings** - Vendor preferences
   - vendor_id, setting_key, setting_value

**Products Table Modified:**
- Added `vendor_id` column
- Added `approval_status` (pending/approved/rejected)
- Added `stock_quantity`
- Added `sku`

### 2. Vendor Registration

**File:** `/vendor-register.php`

**Features:**
- Personal information (name, username, email, password)
- Store information (store name, business email/phone)
- Business address
- Creates user with 'vendor' role
- Creates vendor profile with 'pending' status
- Shows 10% commission disclosure
- Terms agreement checkbox

**Process:**
1. Check if email exists
2. Create user account (role: vendor)
3. Create vendor profile (status: pending)
4. Create user_details entry
5. Redirect to login

### 3. Vendor Login

**File:** `/vendor-login.php`

**Features:**
- Email and password login
- Status checking (pending/active/rejected/suspended)
- Different messages for each status
- Redirects to vendor dashboard on success
- Links to registration and main store

**Status Handling:**
- **Pending:** "Application is pending approval"
- **Rejected:** "Application was rejected"
- **Suspended:** "Account has been suspended"
- **Active:** Login successful → dashboard

### 4. Vendor Security

**File:** `/vendorportal/includes/vendor-security.php`

**Purpose:** Include at top of every vendor page

**Checks:**
1. Vendor is logged in (`$_SESSION['vendor_id']`)
2. Vendor status is still 'active'
3. Loads vendor information into `$VENDOR_INFO` array

**Available Data:**
```php
$VENDOR_INFO = [
    'id' => vendor_id,
    'user_id' => user_id,
    'store_name' => store_name,
    'store_slug' => store_slug,
    'email' => email,
    'username' => username,
    'commission_rate' => commission_rate,
    'status' => status
];
```

### 5. Vendor Dashboard

**File:** `/vendorportal/dashboard.php`

**Stats Displayed:**
- Total Products
- Total Orders
- Pending Orders
- Pending Earnings

**Features:**
- Quick action buttons (Add Product, View Orders, etc.)
- Recent orders table (last 5)
- Links to vendor portal sections

**Metrics Calculated:**
- Products: COUNT where vendor_id = current vendor
- Orders: COUNT via JOIN with products table
- Earnings: SUM from vendor_earnings table

### 6. Vendor Portal Layout

**Header:** `/vendorportal/includes/header.php`
- Store name in navbar
- "View My Store" button
- User dropdown (Settings, Logout)

**Sidebar:** `/vendorportal/includes/sidebar.php`
- Dashboard
- Products (Add, Manage)
- Orders (All, Pending, Completed)
- Earnings, Payouts
- Store Profile

**Footer:** `/vendorportal/includes/footer.php`
- Simple footer with copyright

### 7. Admin Vendor Management

**File:** `/__admin__/pending-vendors.php`

**Features:**
- View all pending applications
- View detailed vendor information (modal)
- Approve vendors (status → active)
- Reject vendors (status → rejected)
- Shows store and owner information

**Approval Process:**
1. Admin reviews application
2. Clicks "Approve"
3. Status updated to 'active'
4. Approved date and admin ID recorded
5. Vendor can now login

**Admin Sidebar:**
- System > Vendors > Pending Applications
- System > Vendors > All Vendors (placeholder)
- System > Vendors > Vendor Earnings (placeholder)

### 8. Vendor Logout

**File:** `/vendorportal/logout.php`

Clears vendor session and redirects to login.

## Commission System

**Default Rate:** 10%
- Platform takes 10%
- Vendor keeps 90%
- Configurable per vendor
- Stored in `vendors.commission_rate`

## File Structure Created

```
/vendor-register.php
/vendor-login.php
/vendorportal/
├── dashboard.php
├── logout.php
└── includes/
    ├── vendor-security.php
    ├── header.php
    ├── sidebar.php
    └── footer.php

/__admin__/
└── pending-vendors.php

/setup/
└── create_vendor_tables.sql
```

## Setup Instructions

### Step 1: Run Database Setup
```sql
-- In phpMyAdmin, run:
/setup/create_vendor_tables.sql
```

### Step 2: Test Vendor Registration
```
http://localhost/awesome-mall/vendor-register.php
```

### Step 3: Approve Vendor (Admin)
```
Admin Panel > Vendors > Pending Applications
```

### Step 4: Vendor Login
```
http://localhost/awesome-mall/vendor-login.php
```

## Next: Phase 2

**Product Management for Vendors:**
- Add products
- Edit products  
- Manage inventory
- Upload images
- Bulk operations

## Status: ✅ Phase 1 COMPLETE!

All foundation components built and tested. Ready for Phase 2!
