Actions Reference
WordPress actions to respond to SquareSync events.
Actions let you respond to events without modifying data. Use these hooks to trigger custom functionality, log events, send notifications, and integrate with other systems.
Order Actions
squarewoosync_before_order_import
Fires before importing a Square order to WooCommerce.
Location: includes/Jobs/WebhookProcessJob.php
| Parameter | Type | Description |
|---|---|---|
| $orderId | string | Square order ID |
| $data | array | Webhook payload |
| $parent_process_id | string | Parent process identifier |
add_action('squarewoosync_before_order_import', function($orderId, $data, $parent_process_id) {
// Perform pre-import actions
error_log("Starting import of Square order: {$orderId}");
}, 10, 3);squarewoosync_square_order_created
Fires after a Square order is created from a WooCommerce order.
Location: includes/Woo/SyncOrder.php
| Parameter | Type | Description |
|---|---|---|
| $square_order_id | string | Created Square order ID |
| $wc_order_id | int | WooCommerce order ID |
add_action('squarewoosync_square_order_created', function($square_order_id, $wc_order_id) {
error_log("Order {$wc_order_id} synced to Square: {$square_order_id}");
}, 10, 2);squarewoosync_square_order_imported
Fires after a WooCommerce order is created from a Square order.
Location: includes/Woo/CreateOrder.php
| Parameter | Type | Description |
|---|---|---|
| $square_order_id | string | Source Square order ID |
| $wc_order_id | int | Created WooCommerce order ID |
add_action('squarewoosync_square_order_imported', function($square_order_id, $wc_order_id) {
// Post-import processing
$order = wc_get_order($wc_order_id);
$order->add_order_note('Imported from Square: ' . $square_order_id);
}, 10, 2);sws_order_import_completed
Fires when a batch order import job completes.
Location: includes/Jobs/OrderImportJob.php
| Parameter | Type | Description |
|---|---|---|
| $progress | array | Import progress data |
add_action('sws_order_import_completed', function($progress) {
$imported = $progress['imported'] ?? 0;
$failed = $progress['failed'] ?? 0;
error_log("Order import completed: {$imported} imported, {$failed} failed");
});Product Actions
squarewoosync_product_created
Fires after a WooCommerce product is created from Square.
Location: includes/Woo/CreateProduct.php
| Parameter | Type | Description |
|---|---|---|
| $product_id | int | Created WooCommerce product ID |
| $square_item_id | string | Source Square item ID |
| $square_data | array | Raw Square item data |
add_action('squarewoosync_product_created', function($product_id, $square_item_id, $square_data) {
update_post_meta($product_id, '_square_item_id', $square_item_id);
update_post_meta($product_id, '_imported_from_square', true);
}, 10, 3);Payment Actions
squarewoosync_payment_completed
Fires when a Square payment is completed successfully.
Location: includes/Payments/WC_SquareSync_Gateway.php
| Parameter | Type | Description |
|---|---|---|
| $order_id | int | WooCommerce order ID |
| $payment_id | string | Square payment ID |
| $payment_data | array | Full payment details |
add_action('squarewoosync_payment_completed', function($order_id, $payment_id, $payment_data) {
// Access full payment data
$amount = $payment_data['amount_money']['amount'] ?? 0;
// Send custom notification
wp_mail(
'sales@example.com',
'New Square Payment',
"Order #{$order_id} paid via Square: {$payment_id} - Amount: {$amount}"
);
}, 10, 3);Best Practices
Priority Management
Use appropriate priority values to control execution order:
// Run early (before other plugins)
add_action('squarewoosync_square_order_created', 'my_function', 5, 2);
// Run at default priority
add_action('squarewoosync_square_order_created', 'my_function', 10, 2);
// Run late (after other plugins)
add_action('squarewoosync_square_order_created', 'my_function', 99, 2);Error Handling
Actions should handle errors gracefully to avoid breaking the sync process:
add_action('squarewoosync_product_created', function($product_id, $square_item_id, $square_data) {
try {
// Your custom logic
do_something_with_product($product_id);
} catch (Exception $e) {
error_log('SquareSync action error: ' . $e->getMessage());
// Don't re-throw - let the sync continue
}
}, 10, 3);Performance Considerations
Avoid heavy operations in hooks that run during checkout or frequent syncs:
// Bad: Expensive operation during payment
add_action('squarewoosync_payment_completed', function($order_id, $payment_id, $payment_data) {
$all_orders = get_posts(['post_type' => 'shop_order', 'numberposts' => -1]); // Slow!
}, 10, 3);
// Good: Defer expensive operations
add_action('squarewoosync_payment_completed', function($order_id, $payment_id, $payment_data) {
// Schedule for later processing
wp_schedule_single_event(time() + 60, 'my_deferred_processing', [$order_id]);
}, 10, 3);Logging
Use WordPress logging for debugging:
add_action('squarewoosync_square_order_created', function($square_order_id, $wc_order_id) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log(sprintf(
'[SquareSync] Order %d synced to Square as %s',
$wc_order_id,
$square_order_id
));
}
}, 10, 2);Testing
Test your customizations thoroughly:
- Use Sandbox mode first — Test with Square's sandbox environment
- Test with various order types — Simple, variable, subscription
- Check error logs — Monitor for issues
- Verify data in Square — Confirm customizations apply correctly
- Test edge cases — Empty values, missing data, errors
Common Use Cases
Send Slack Notification on Import
add_action('squarewoosync_square_order_imported', function($square_order_id, $wc_order_id) {
$order = wc_get_order($wc_order_id);
$total = $order->get_total();
wp_remote_post('https://hooks.slack.com/services/YOUR/WEBHOOK/URL', [
'body' => json_encode([
'text' => "New Square order imported: #{$wc_order_id} - \${$total}"
]),
'headers' => ['Content-Type' => 'application/json']
]);
}, 10, 2);Sync to External CRM
add_action('squarewoosync_payment_completed', function($order_id, $payment_id, $payment_data) {
$order = wc_get_order($order_id);
// Send to CRM
wp_remote_post('https://api.crm.example.com/orders', [
'body' => json_encode([
'order_id' => $order_id,
'email' => $order->get_billing_email(),
'total' => $order->get_total(),
'payment_id' => $payment_id
]),
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . CRM_API_KEY
]
]);
}, 10, 3);Custom Product Post-Processing
add_action('squarewoosync_product_created', function($product_id, $square_item_id, $square_data) {
// Set custom category based on Square data
$square_category = $square_data['item_data']['category_id'] ?? '';
if ($square_category === 'CATEGORY_ABC') {
wp_set_object_terms($product_id, 'special-items', 'product_cat');
}
// Add to featured if marked in Square
if (!empty($square_data['item_data']['is_featured'])) {
update_post_meta($product_id, '_featured', 'yes');
}
}, 10, 3);