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 or updated from Square — on every import path (manual import, cron sync, webhook update).
Location: includes/Woo/CreateProduct.php
| Parameter | Type | Description |
|---|---|---|
| $product_id | int | The WooCommerce product ID |
| $product | WC_Product | The saved product object |
| $wc_product_data | array | Mapped product data used for the import (name, price, stock, categories, variations, square_product_id, ...) plus is_new_product (bool) — true when the product was just created, false on updates |
| $data_to_import | array | Field flags for this sync (title, price, stock, categories, image, ... each true/false) |
add_action('squarewoosync_product_created', function($product_id, $product, $wc_product_data, $data_to_import) {
if (!empty($wc_product_data['is_new_product'])) {
update_post_meta($product_id, '_first_imported_at', time());
}
}, 10, 4);squarewoosync_product_categories_assigned
Since 9.8.5. Fires after Square categories have been assigned to a product during import. Not fired when an import removes or clears categories. Useful for reacting to category placement — e.g. tagging, notifications, or applying extra product settings per category.
Location: includes/Woo/CreateProduct.php
| Parameter | Type | Description |
|---|---|---|
| $product | WC_Product | The product the terms were assigned to |
| $category_ids | int[] | WooCommerce product_cat term IDs that were assigned |
| $wc_product_data | array | Full mapped product data (the categories key holds the source Square categories) |
add_action('squarewoosync_product_categories_assigned', function($product, $category_ids, $wc_product_data) {
foreach ($category_ids as $term_id) {
$term = get_term($term_id, 'product_cat');
if ($term && $term->slug === 'events') {
// e.g. mark event products as virtual
$product->set_virtual(true);
$product->save();
}
}
}, 10, 3);squarewoosync_sync_product_to_square
Programmatically sync a WooCommerce product to Square. Useful for triggering syncs from custom code, scheduled tasks, or third-party integrations.
Location: includes/Woo/SyncProduct.php
| Parameter | Type | Description |
|---|---|---|
| $product_id | int | WooCommerce product ID |
| $fields | array | Optional. Specific fields to sync (default: all fields) |
Requirements:
- The product must already be linked to Square (have a
square_product_idmeta value) - This action is for syncing existing linked products, not for initial export
Supported Fields:
| Field | Description |
|---|---|
stock | Inventory quantity |
title | Product name |
description | Product description |
price | Product price |
sku | SKU |
images | Product images |
categories | Product categories |
metafields | Custom metafields |
// Sync all fields for a product
do_action('squarewoosync_sync_product_to_square', $product_id);
// Sync only specific fields
do_action('squarewoosync_sync_product_to_square', $product_id, [
'stock' => true,
'price' => true,
]);
// Example: Sync inventory after external update
add_action('my_inventory_updated', function($product_id) {
do_action('squarewoosync_sync_product_to_square', $product_id, [
'stock' => true,
]);
});
// Example: Bulk sync prices for a category
$products = wc_get_products(['category' => 'sale-items']);
foreach ($products as $product) {
do_action('squarewoosync_sync_product_to_square', $product->get_id(), [
'price' => true,
]);
}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(
'[email protected]',
'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, $product, $wc_product_data, $data_to_import) {
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, 4);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, $product, $wc_product_data, $data_to_import) {
// Add an extra WooCommerce category based on the Square categories
$square_category_names = array_map(fn($c) => $c['name'] ?? '', $wc_product_data['categories'] ?? []);
if (in_array('Specials', $square_category_names, true)) {
wp_set_object_terms($product_id, 'special-items', 'product_cat', true);
}
// Feature newly imported products over a price threshold
if (!empty($wc_product_data['is_new_product']) && ($wc_product_data['price'] ?? 0) > 100) {
update_post_meta($product_id, '_featured', 'yes');
}
}, 10, 4);