# AliExpress WooCommerce Product Manager - Usage Guide

## Quick Start

### 1. Installation
1. Upload the plugin files to `/wp-content/plugins/aliexpress-wp-plugin/`
2. Activate the plugin in WordPress Admin → Plugins
3. Ensure WooCommerce is installed and activated

### 2. Setting Up AliExpress Products

#### Method 1: Using Custom Fields (Recommended)
1. Go to **Products → Add New** or edit an existing product
2. Scroll down to **Custom Fields** section
3. Add a new custom field:
   - **Name**: `productUrl`
   - **Value**: Your AliExpress product URL (must contain "aliexpress")
4. Update/Publish the product

#### Method 2: Using Advanced Custom Fields Plugin
1. Install Advanced Custom Fields plugin
2. Create a new field group for products
3. Add a text field with name `productUrl`
4. Set location rules to show on Product post type
5. Add your AliExpress URLs to products

#### Method 3: Programmatically
```php
// Add this to your theme's functions.php or a custom plugin
add_action('save_post_product', 'add_aliexpress_url_to_product', 10, 2);

function add_aliexpress_url_to_product($post_id, $post) {
    // Only run once
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    
    // Check if this is an AliExpress product
    $aliexpress_url = 'https://www.aliexpress.com/item/your-product-url';
    
    // Update the productUrl meta field
    update_post_meta($post_id, 'productUrl', $aliexpress_url);
}
```

### 3. Accessing the Admin Interface

1. Go to **AliExpress Products** in the main WordPress admin menu
2. You'll see all products with AliExpress URLs
3. Use the filters to find specific products

## Features Explained

### Search and Filtering

#### Search Box
- Searches product titles only
- Always respects the "aliexpress" condition
- Real-time search with 500ms delay

#### Sort Options
- **Title**: Alphabetical sorting by product name
- **Views**: Sort by number of views (most/least popular)
- **Date**: Sort by creation date (newest/oldest)

#### Views Filter
- **All Views**: Show all products
- **0 Views**: Products never viewed
- **1-10 Views**: Low traffic products
- **11-50 Views**: Medium traffic products
- **51-100 Views**: High traffic products
- **100+ Views**: Very popular products

### Action Buttons

#### Open AliExpress
- Opens the AliExpress product URL in a new tab
- Only shows if product has a valid AliExpress URL

#### Edit
- Opens the WordPress product edit page in a new tab
- Quick access to modify product details

#### Preview
- Opens the product frontend page in a new tab
- See how customers view the product

### View Tracking

#### How It Works
- Views are tracked automatically when customers visit product pages
- Each session only counts one view per product
- View counts update in real-time in the admin interface

#### Session Management
- Uses PHP sessions to prevent duplicate counting
- Session data is stored in `$_SESSION['aliexpress_viewed_products']`
- Views are stored in the `product_views` meta field

## Advanced Usage

### Customizing the Plugin

#### Change Products Per Page
```php
// Add to functions.php
add_filter('aliexpress_wp_plugin_products_per_page', function($per_page) {
    return 50; // Change from default 20
});
```

#### Custom AliExpress Keyword
```php
// Add to functions.php
add_filter('aliexpress_wp_plugin_keyword', function($keyword) {
    return 'aliexpress'; // Default keyword
});
```

#### Add Custom Columns
```php
// Add to functions.php
add_action('aliexpress_wp_plugin_admin_page_after_table', function() {
    // Add custom content after the products table
    echo '<div class="custom-section">Your custom content here</div>';
});
```

### Database Structure

#### Custom Meta Fields
- `productUrl`: Stores the AliExpress product URL
- `product_views`: Stores the view count (integer)

#### Example SQL Queries
```sql
-- Get all AliExpress products
SELECT p.*, pm.meta_value as product_url, pm2.meta_value as views
FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = 'productUrl'
LEFT JOIN wp_postmeta pm2 ON p.ID = pm2.post_id AND pm2.meta_key = 'product_views'
WHERE p.post_type = 'product' 
AND p.post_status = 'publish'
AND pm.meta_value LIKE '%aliexpress%';

-- Get products with most views
SELECT p.ID, p.post_title, pm.meta_value as views
FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = 'product_views'
WHERE p.post_type = 'product'
ORDER BY CAST(pm.meta_value AS UNSIGNED) DESC
LIMIT 10;
```

### Troubleshooting

#### Plugin Not Showing Products
1. Check if WooCommerce is activated
2. Verify products have `productUrl` meta field
3. Ensure URLs contain "aliexpress" (case-insensitive)
4. Check user permissions (admin only)

#### Views Not Tracking
1. Check if sessions are enabled
2. Verify JavaScript is loading on product pages
3. Check browser console for errors
4. Ensure product has valid AliExpress URL

#### Performance Issues
1. Consider adding database indexes:
```sql
ALTER TABLE wp_postmeta ADD INDEX idx_product_url (meta_key, meta_value(50));
ALTER TABLE wp_postmeta ADD INDEX idx_product_views (meta_key, meta_value);
```

2. Use caching plugins for better performance
3. Consider pagination for large product catalogs

### Security Notes

- All inputs are sanitized using WordPress functions
- All outputs are escaped to prevent XSS
- AJAX requests are protected with nonces
- Admin access requires `manage_options` capability
- SQL queries use prepared statements

### Support

For technical support or feature requests, please refer to the main README.md file for contact information. 