# REST API

Bizzwishlist provides a REST API for developers who want to integrate wishlist functionality into custom applications, headless WooCommerce stores, or mobile apps.

## Base URL

```
/wp-json/bizzwishlist/v1/items
```

Full example: `https://yourstore.com/wp-json/bizzwishlist/v1/items`

## Authentication

All REST API endpoints require **authentication**. Only logged-in users can access the API. Guest wishlists are not available through the REST API.

Supported authentication methods:

- **Cookie authentication** — When making requests from the WordPress frontend (requires a valid nonce).
- **Application Passwords** — WordPress application passwords for external integrations.
- **WooCommerce REST API keys** — If using WooCommerce API infrastructure.

## Endpoints

### GET /items

Retrieve all items in the authenticated user's wishlist.

**Request:**
```
GET /wp-json/bizzwishlist/v1/items
```

**Response (200 OK):**
```json
[
    {
        "id": 1,
        "product_id": 42,
        "name": "Classic T-Shirt",
        "price": "29.99",
        "price_html": "<span class=\"amount\">$29.99</span>",
        "image": "https://yourstore.com/wp-content/uploads/tshirt.jpg",
        "permalink": "https://yourstore.com/product/classic-t-shirt/",
        "created_at": "2025-01-15 10:30:00"
    },
    {
        "id": 2,
        "product_id": 87,
        "name": "Running Shoes",
        "price": "89.99",
        "price_html": "<span class=\"amount\">$89.99</span>",
        "image": "https://yourstore.com/wp-content/uploads/shoes.jpg",
        "permalink": "https://yourstore.com/product/running-shoes/",
        "created_at": "2025-01-16 14:20:00"
    }
]
```

**Empty wishlist response:**
```json
[]
```

---

### POST /items

Add a product to the authenticated user's wishlist.

**Request:**
```
POST /wp-json/bizzwishlist/v1/items
Content-Type: application/json

{
    "product_id": 42
}
```

**Parameters:**

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `product_id` | integer | Yes | The WooCommerce product ID to add |
| `variation_id` | integer | No | The variation ID (for variable products) |

**Response (201 Created):**
```json
{
    "message": "Product added to wishlist.",
    "product_id": 42,
    "count": 3
}
```

**Error — Product already in wishlist (400 Bad Request):**
```json
{
    "code": "already_exists",
    "message": "Product is already in your wishlist.",
    "data": {
        "status": 400
    }
}
```

**Error — Invalid product (400 Bad Request):**
```json
{
    "code": "invalid_product",
    "message": "Invalid product ID.",
    "data": {
        "status": 400
    }
}
```

---

### DELETE /items

Remove a product from the authenticated user's wishlist.

**Request:**
```
DELETE /wp-json/bizzwishlist/v1/items
Content-Type: application/json

{
    "product_id": 42
}
```

**Parameters:**

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `product_id` | integer | Yes | The WooCommerce product ID to remove |

**Response (200 OK):**
```json
{
    "message": "Product removed from wishlist.",
    "product_id": 42,
    "count": 2
}
```

**Error — Product not found in wishlist (404 Not Found):**
```json
{
    "code": "not_found",
    "message": "Product not found in your wishlist.",
    "data": {
        "status": 404
    }
}
```

---

## AJAX Endpoints

In addition to the REST API, Bizzwishlist provides AJAX endpoints for frontend use. These work for both logged-in users and guests.

All AJAX requests are sent to `wp-admin/admin-ajax.php` with a nonce parameter for security.

### Available AJAX Actions

| Action | Method | Description |
|--------|--------|-------------|
| `bizzwishlist_toggle` | POST | Add or remove a product (toggle) |
| `bizzwishlist_remove` | POST | Remove a product from the wishlist |
| `bizzwishlist_get_count` | POST | Get the current wishlist item count |
| `bizzwishlist_get_items` | POST | Get all wishlist items with product data |
| `bizzwishlist_get_mini_fragment` | POST | Get the mini wishlist HTML fragment |
| `bizzwishlist_add_to_cart_single` | POST | Add a single wishlist item to the WooCommerce cart |
| `bizzwishlist_add_all_to_cart` | POST | Add all wishlist items to the WooCommerce cart |
| `bizzwishlist_add_selected_to_cart` | POST | Add selected wishlist items to the cart |

### AJAX Request Example

```javascript
jQuery.ajax({
    url: bizzwishlist_params.ajax_url,
    type: 'POST',
    data: {
        action: 'bizzwishlist_toggle',
        nonce: bizzwishlist_params.nonce,
        product_id: 42
    },
    success: function( response ) {
        if ( response.success ) {
            console.log( response.data.action ); // "added" or "removed"
            console.log( response.data.count );  // Updated count
        }
    }
});
```

### AJAX Response Format

**Toggle — Product Added:**
```json
{
    "success": true,
    "data": {
        "action": "added",
        "count": 3,
        "message": "Added to wishlist."
    }
}
```

**Toggle — Product Removed:**
```json
{
    "success": true,
    "data": {
        "action": "removed",
        "count": 2,
        "message": "Removed from wishlist."
    }
}
```

**Get Count:**
```json
{
    "success": true,
    "data": {
        "count": 5
    }
}
```

### Security

All AJAX endpoints verify the nonce using `check_ajax_referer()`. The nonce is generated by the plugin and available in JavaScript via `bizzwishlist_params.nonce`. Requests without a valid nonce will be rejected.
