# Informatiq Smart Google Pricing

A production-ready WooCommerce plugin that automatically adjusts sale prices based on Google Merchant Center competitor data.

## Description

This plugin connects to Google Merchant Center API to retrieve competitive pricing data and automatically updates your WooCommerce product prices to stay competitive while maintaining your desired profit margins.

## Features

- **Automated Daily Price Updates**: Schedule automatic price adjustments using WordPress cron
- **Google Merchant Center Integration**: Connects via Google Content API for Shopping
- **Tax-Aware Pricing**: Handles both tax-inclusive and tax-exclusive pricing configurations
- **Minimum Margin Protection**: Never sell below your cost + minimum margin
- **In-Stock Only Processing**: Only updates products that are currently in stock
- **Comprehensive Logging**: Track all price changes with detailed logs
- **Manual Sync**: Test and trigger price updates manually from the admin panel
- **WooCommerce Cost of Goods Support**: Uses product cost data for margin calculations

## Requirements

- WordPress 5.8 or higher
- WooCommerce 5.0 or higher
- PHP 7.4 or higher
- Google Merchant Center account
- Google Service Account with Content API access

## Installation

### Quick Install (Recommended)

1. Download the plugin with vendor libraries included (complete package)
2. Upload to `/wp-content/plugins/informatiq-smart-google-pricing/`
3. Activate the plugin through the 'Plugins' menu in WordPress
4. Configure settings under WooCommerce > Smart Pricing

### Manual Setup (If vendor directory is missing)

1. Upload the plugin files to `/wp-content/plugins/informatiq-smart-google-pricing/`
2. Set up Google API Client library (see `VENDOR-SETUP.md` for details):
   - **Option A**: Run `composer install` in the plugin directory
   - **Option B**: Manually download and extract Google API Client to `vendor/` directory
3. Activate the plugin through the 'Plugins' menu in WordPress
4. Configure settings under WooCommerce > Smart Pricing

The plugin includes a custom autoloader that works with manually included libraries, so Composer is not required for end users.

## Configuration

### Google Merchant Center Setup

1. Create a Google Cloud Project
2. Enable the Content API for Shopping
3. Create a Service Account and download the JSON key
4. Grant the service account access to your Merchant Center account

### Plugin Settings

1. Navigate to **WooCommerce > Smart Pricing**
2. Enter your Google Merchant ID
3. Paste your Service Account JSON key
4. Set your minimum profit margin percentage
5. Configure automation settings
6. Save settings and test the connection

## How It Works

1. **Daily Automation**: The plugin runs automatically based on your configured schedule
2. **Product Matching**: Matches WooCommerce products with Google Merchant Center via SKU/GTIN
3. **Competitive Analysis**: Retrieves the lowest competitor price from Google
4. **Price Calculation**:
   - Calculates new price as: Competitor Price - Random Offset (0.05-0.20)
   - Ensures price never goes below Cost + Minimum Margin
   - Handles tax calculations based on WooCommerce settings
5. **Price Update**: Updates the product sale price in WooCommerce
6. **Logging**: Records all changes for auditing and review

## Tax Handling

The plugin intelligently handles both pricing configurations:

- **Prices Include Tax**: Sets sale price directly to match desired final price
- **Prices Exclude Tax**: Reverse-calculates base price to achieve desired final price after tax

## Minimum Margin Protection

The plugin ensures profitability by:

1. Checking product cost from WooCommerce Cost of Goods (`_wc_cog_cost` meta)
2. Falling back to regular price if cost is not set
3. Calculating minimum price as: `Cost × (1 + Minimum Margin %)`
4. Never setting prices below this minimum threshold

## Admin Interface

Access the admin panel at **WooCommerce > Smart Pricing** to:

- Configure Google API credentials
- Set pricing rules and margins
- Test API connection
- Run manual price syncs
- View sync schedule and status
- Review today's price updates
- Monitor logging and errors

## Logging

All price updates are logged with:

- Product ID and name
- Old and new prices
- Competitor price
- Timestamp
- Update message

Logs are stored in a custom database table and also written to WooCommerce logs for debugging.

## Cron Schedule

The plugin supports multiple update frequencies:

- Once daily (default)
- Twice daily (every 12 hours)
- Every 6 hours

## Development

### File Structure

```
informatiq-smart-google-pricing/
├── admin/
│   └── class-informatiq-sp-admin.php
├── assets/
│   ├── css/
│   │   └── admin.css
│   └── js/
│       └── admin.js
├── includes/
│   ├── class-informatiq-sp-google-api.php
│   ├── class-informatiq-sp-logger.php
│   ├── class-informatiq-sp-price-updater.php
│   └── class-informatiq-sp-scheduler.php
├── composer.json
├── informatiq-smart-google-pricing.php
├── README.md
└── uninstall.php
```

### Dependencies

The plugin uses Composer for dependency management:

```bash
composer install
```

Main dependencies:
- `google/apiclient`: ^2.15

## Support

For support and documentation, visit [informatiq.services](https://informatiq.services)

## Author

**Mălin Cenușă**
- Website: [malin.ro](https://malin.ro)
- Company: [informatiq.services](https://informatiq.services)

## License

GPL v2 or later

## Changelog

### 1.0.0
- Initial release
- Google Merchant Center integration
- Automated daily price updates
- Tax-aware pricing logic
- Minimum margin protection
- Comprehensive logging
- Admin interface with manual sync
