# Installation & Setup Guide

## Prerequisites

Before installing this plugin, ensure you have:

1. **WordPress** 5.8 or higher
2. **WooCommerce** 5.0 or higher installed and activated
3. **PHP** 7.4 or higher with Composer installed
4. **Google Merchant Center** account with products listed
5. **Google Cloud Project** with Content API for Shopping enabled

## Step 1: Install Plugin Files

### Option A: Complete Package (Recommended)

If you received the plugin with the `vendor/` directory already included:

1. Upload the entire plugin directory to `/wp-content/plugins/`
2. Skip to Step 2 - the plugin is ready to activate!

### Option B: Install Dependencies Manually

If the `vendor/` directory is empty or missing:

1. Upload the plugin files to your WordPress plugins directory:
   ```bash
   cd /wp-content/plugins/
   # Upload or clone the plugin files
   ```

2. Navigate to the plugin directory:
   ```bash
   cd informatiq-smart-google-pricing
   ```

3. Install dependencies using ONE of these methods:

   **Method 1: Using Composer (easiest)**
   ```bash
   composer install --no-dev --optimize-autoloader
   ```

   **Method 2: Manual Download**
   - Download Google API PHP Client from: https://github.com/googleapis/google-api-php-client/releases
   - Extract to `vendor/google/apiclient/`
   - Download dependencies or see `VENDOR-SETUP.md` for detailed instructions

   **Note**: The plugin includes a custom autoloader that works with manually placed vendor files, so Composer is optional for end users.

## Step 2: Create Google Service Account

1. **Go to Google Cloud Console**:
   - Visit https://console.cloud.google.com/
   - Create a new project or select an existing one

2. **Enable Content API for Shopping**:
   - Navigate to "APIs & Services" > "Library"
   - Search for "Content API for Shopping"
   - Click "Enable"

3. **Create Service Account**:
   - Go to "APIs & Services" > "Credentials"
   - Click "Create Credentials" > "Service Account"
   - Fill in the service account details:
     - Name: `informatiq-pricing-service`
     - Description: `Service account for Smart Pricing plugin`
   - Click "Create and Continue"
   - Skip optional steps and click "Done"

4. **Create JSON Key**:
   - Click on the newly created service account
   - Go to the "Keys" tab
   - Click "Add Key" > "Create New Key"
   - Select "JSON" format
   - Click "Create"
   - Save the downloaded JSON file securely

5. **Grant Access to Merchant Center**:
   - Go to Google Merchant Center (https://merchants.google.com/)
   - Navigate to Settings > Users
   - Click "Add user"
   - Enter the service account email (looks like: `account-name@project-id.iam.gserviceaccount.com`)
   - Assign "Admin" or "Standard" access
   - Click "Save"

## Step 3: Activate Plugin

1. Log in to your WordPress admin panel
2. Navigate to **Plugins** > **Installed Plugins**
3. Find "informatiq-smart-google-pricing"
4. Click **Activate**

## Step 4: Configure Plugin Settings

1. Navigate to **WooCommerce** > **Smart Pricing**

2. **Configure Google Merchant Center Settings**:
   - **Google Merchant ID**: Enter your Merchant Center ID (found in Merchant Center settings)
   - **Service Account JSON**: Open the downloaded JSON file and paste its entire contents here

3. **Configure Pricing Settings**:
   - **Minimum Margin (%)**: Set your minimum acceptable profit margin (e.g., 10 for 10%)
   - This ensures prices never drop below cost + margin

4. **Configure Automation Settings**:
   - **Enable Automatic Updates**: Check to enable daily automated updates
   - **Update Frequency**: Choose how often to update prices:
     - Once Daily (recommended for most stores)
     - Twice Daily (for highly competitive markets)
     - Every 6 Hours (for very dynamic pricing)

5. Click **Save Settings**

## Step 5: Test Configuration

1. Click the **"Test Google API Connection"** button
2. Wait for the response
3. If successful, you'll see: "Connection successful!"
4. If failed, check your credentials and API access

## Step 6: Run Initial Sync

1. Click the **"Run Manual Sync Now"** button
2. The plugin will:
   - Fetch all in-stock products
   - Match them with Google Merchant Center
   - Retrieve competitive pricing
   - Update sale prices according to your rules

3. Review the results in the "Today's Price Updates" section

## Product Requirements

For products to be updated automatically, they must meet these criteria:

1. **Stock Status**: Must be "In Stock"
2. **SKU**: Must have a SKU that matches the product in Google Merchant Center
3. **GTIN** (optional): Can be set via `_gtin` meta field for better matching
4. **Cost** (optional): Set product cost using WooCommerce Cost of Goods plugin for accurate margin calculations

## Setting Product Cost

To use the minimum margin feature effectively:

1. **With Cost of Goods Plugin**:
   - Install "WooCommerce Cost of Goods" plugin
   - Edit each product
   - Enter cost in the "Cost of Goods" field

2. **Without Cost of Goods Plugin**:
   - The plugin will use the regular price as a fallback
   - This means minimum margin will be calculated from regular price

## Monitoring & Maintenance

### View Logs

- Navigate to **WooCommerce** > **Smart Pricing**
- Scroll to "Today's Price Updates" section
- View detailed logs of all price changes

### Check Schedule

- The "Schedule Information" section shows:
  - Next scheduled run time
  - Last run time and results
  - Number of products processed/updated

### Troubleshooting

If prices aren't updating:

1. **Check WooCommerce Logs**:
   - Navigate to **WooCommerce** > **Status** > **Logs**
   - Select the `informatiq-smart-pricing-...` log file
   - Review error messages

2. **Verify Settings**:
   - Ensure Google API credentials are correct
   - Test API connection
   - Check that automatic updates are enabled

3. **Check Cron**:
   - Ensure WordPress cron is running properly
   - Install "WP Crontrol" plugin to debug cron jobs
   - Look for `informatiq_sp_daily_price_update` event

4. **Product Issues**:
   - Verify products have SKUs
   - Ensure products are in stock
   - Check that products exist in Google Merchant Center

## Advanced Configuration

### Custom Cron Schedule

To change the cron schedule programmatically:

```php
// In your theme's functions.php or custom plugin
Informatiq_SP_Scheduler::reschedule( 'daily' ); // or 'twice_daily', 'every_6_hours'
```

### Hooks and Filters

The plugin provides hooks for customization:

```php
// Modify minimum offset
add_filter( 'informatiq_sp_min_offset', function() {
    return 0.10; // Minimum $0.10 difference
});

// Modify maximum offset
add_filter( 'informatiq_sp_max_offset', function() {
    return 0.50; // Maximum $0.50 difference
});
```

## Security Recommendations

1. **Protect Service Account JSON**:
   - Never commit the JSON key to version control
   - Store securely in WordPress database (plugin does this automatically)
   - Use environment variables for extra security if needed

2. **Limit Service Account Permissions**:
   - Only grant necessary permissions in Merchant Center
   - Use "Standard" access if "Admin" isn't required

3. **Regular Audits**:
   - Review price change logs regularly
   - Monitor for unexpected pricing patterns
   - Set up alerts for significant price drops

## Uninstallation

If you need to remove the plugin:

1. Deactivate the plugin from WordPress admin
2. Delete the plugin (this will remove all settings and logs)
3. The plugin automatically:
   - Removes all settings from database
   - Deletes the custom log table
   - Clears scheduled cron events

Note: Product prices will remain at their last updated values. You'll need to manually adjust them if desired.

## Support

For issues or questions:

- Email: contact@malin.ro
- Website: https://informatiq.services
- Documentation: See README.md

## Updates

To update the plugin:

1. Download the latest version
2. Deactivate the old version
3. Replace plugin files
4. Run `composer install` if dependencies changed
5. Reactivate the plugin

Your settings and logs will be preserved during updates.
