The ZC API Plugin synchronizes product stock availability from the Z Portal API to WooCommerce products. It runs scheduled daily syncs and provides manual synchronization options through the WordPress admin interface.
- Installation & Setup
- Configuration
- Features
- Synchronization Process
- Admin Interface
- Technical Details
- Troubleshooting
- Security
- WordPress 5.0+
- WooCommerce 3.0+
- PHP 7.4+
- Valid Z Portal API Secure Key
- Upload the plugin file to
/wp-content/plugins/
directory - Activate the plugin through the 'Plugins' menu in WordPress
- Navigate to ZC API in the admin menu
- Enter your Secure Key from Z Portal API
- Save settings
// The plugin automatically schedules daily synchronization upon activation
wp_schedule_event(time(), 'daily', 'zc_sync_stock_price');
Secure Key
- Location:
ZC API > API Nastavení
- Required: Yes
- Purpose: Authenticates requests to Z Portal API
- Storage: WordPress options table (
zc_api_secure_key
)
private $api_url = 'https://api.zcportal.cz/public/graphql';
- Runs once per day via WordPress cron
- Scheduled on plugin activation
- Can be manually triggered from admin panel
- On-demand sync via admin interface
- Real-time status updates
- Progress tracking
- Maps Z Portal availability codes to WooCommerce stock statuses
- Updates stock quantities for products with stock management enabled
- Does not modify product prices
- Processes 100 products per batch
- Prevents API overload
- Includes rate limit handling
- Detailed activity log
- Error tracking
- Auto-updating status display (refreshes every 5 seconds)
- Automatic detection of API rate limits
- 1-hour wait period when limit reached
- Automatic retry after waiting period
- Token Acquisition
// Request authentication token using secure key
mutation RequestToken {
requestToken(input: {secure: "SECURE_KEY", scope: "products"}) {
token
}
}
- Product Fetching
// Fetch products in batches of 100
query Products {
products(pagination: {limit: 100, offset: OFFSET}) {
edges {
barcodes
supplies { availability }
}
}
}
- Stock Update
- Matches products by SKU (barcode)
- Updates WooCommerce stock status
- Updates stock quantity if stock management is enabled
$availability = ($availability_code === 'A') ? 'instock' : 'outofstock';
Z Portal Code | WooCommerce Status | Stock Quantity |
---|---|---|
A | In Stock | 100 (if managed) |
Other | Out of Stock | 0 (if managed) |
WordPress Admin > ZC API
- Saves the Secure Key configuration
- Required before first synchronization
- Triggers immediate synchronization
- Runs as background process
- Status updates in real-time
- Removes all log entries
- Useful for maintenance and debugging
- Immediately halts running sync
- Safe to use mid-process
- Can be resumed with "Synchronize Now"
The interface shows:
- Current Status: Running or Stopped
- Recent Log Entries: Last 20 entries (auto-refreshing)
- Timestamp: For each log entry
- Error Messages: Highlighted for easy identification
class ZC_API_Sync {
private $api_url; // API endpoint
private $log; // Runtime log array
private $batch_size; // Products per batch (100)
private $api_limit_wait; // Rate limit wait time (3600s)
private $max_log_entries; // Maximum log entries (100)
}
Obtains authentication token from Z Portal API.
Returns:
string
Token on successnull
on failure
Main synchronization method. Processes all products in batches.
Process:
- Checks if sync was manually stopped
- Acquires authentication token
- Fetches products in batches
- Updates WooCommerce stock status
- Handles rate limiting
- Logs all activities
Updates individual WooCommerce product stock.
Parameters:
$product
(array) Product data from Z Portal API
Returns:
bool
True on success, false on failure
Updates:
- Stock status (instock/outofstock)
- Stock quantity (if stock management enabled)
- DOES NOT update prices
Makes GraphQL requests to Z Portal API.
Parameters:
$query
(string) GraphQL query$token
(string|null) Authentication token
Returns:
array
Decoded JSON responsenull
on error
Solution: Enter your Secure Key in ZC API settings and save.
Possible causes:
- Server firewall blocking outbound connections
- SSL certificate issues
- API endpoint unavailable
Solution:
// Check if wp_remote_post is working
$test = wp_remote_post('https://api.zcportal.cz/public/graphql');
if (is_wp_error($test)) {
echo $test->get_error_message();
}
Symptom: Synchronization pauses for 1 hour
Cause: Too many API requests in short time
Solution: Wait for automatic retry or reduce batch size:
private $batch_size = 50; // Reduce from 100
Check:
- Product has correct SKU matching Z Portal barcode
- Product exists in WooCommerce
- SKU field is not empty
Enable WordPress debug logging:
// wp-config.php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
View logs at: wp-content/debug.log
-
Secure Key Storage
- Stored in WordPress options table
- Not exposed in frontend
- Escaped in SQL queries
-
Nonce Verification
wp_verify_nonce($_POST['zc_api_nonce'], 'zc_api_actions')
- Capability Checks
if (!current_user_can('manage_options')) {
wp_die('Unauthorized');
}
- AJAX Security
- Nonce validation on all AJAX requests
- Admin-only access
- Sanitized inputs
-
Secure Key Protection
- Never commit secure key to version control
- Use environment variables for production
- Rotate keys periodically
-
API Communication
- Uses SSL/TLS (sslverify: true)
- 30-second timeout prevents hanging
- Error logging without exposing sensitive data
-
Database Operations
- Uses WordPress database API
- Prepared statements via
esc_sql()
- Transaction-safe updates
Frequency: Daily
Hook: zc_sync_stock_price
// Change to twice daily
wp_clear_scheduled_hook('zc_sync_stock_price');
wp_schedule_event(time(), 'twicedaily', 'zc_sync_stock_price');
wp_clear_scheduled_hook('zc_sync_stock_price');
public static function activate() {
// Schedule daily cron event
if (!wp_next_scheduled('zc_sync_stock_price')) {
wp_schedule_event(time(), 'daily', 'zc_sync_stock_price');
}
}
public static function deactivate() {
// Remove scheduled events
wp_clear_scheduled_hook('zc_sync_stock_price');
// Clean up options
delete_option('zc_api_debug_log');
delete_option('zc_api_sync_status');
// Note: zc_api_secure_key is preserved
}
-
Batch Processing
- Prevents memory overflow
- Reduces server load
- Enables progress tracking
-
Log Rotation
- Maximum 100 entries in memory
- Automatic trimming of old entries
- Prevents database bloat
-
Conditional Updates
- Only logs when stock status changes
- Skips products without SKU
- Validates product existence before update
-
Rate Limiting
- 1-second delay between batches
- Automatic pause on API limits
- Prevents server overload
Minimum:
- PHP memory_limit: 128M
- max_execution_time: 300 seconds
- WordPress memory limit: 128M
Recommended:
// wp-config.php
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');
You can add actions before/after sync:
// Before sync starts
add_action('zc_sync_before_start', function() {
// Your custom code
});
// After sync completes
add_action('zc_sync_after_complete', function($total_updated, $total_errors) {
// Send notification email, etc.
});
add_filter('zc_api_batch_size', function($size) {
return 50; // Change from default 100
});
- Complete rewrite for better performance
- Added batch processing
- Improved error handling
- Real-time status updates
- Rate limit protection
- Enhanced logging system
- Stock-only updates (prices preserved)
For issues or questions:
- Check the log in ZC API > Synchronization Status
- Enable WordPress debug mode
- Verify Secure Key is correct
- Test API connection manually
- Check WooCommerce product SKUs match Z Portal barcodes
Proprietary - For use with Z Portal API integration only.
Bohuslav Sedláček