Skip to main content

WooCommerce Sync Target

The WooCommerce sync target pushes nFusion prices directly into a WooCommerce database by updating the WordPress wp_postmeta table. This is the primary sync method for both the Retail and Dealers WooCommerce stores.


How It Works

The provider connects to the WooCommerce database using a dedicated Laravel database connection (separate from the middleware's own database). It:

  1. Calls the nFusion General API (api.nfusionsolutions.biz) to fetch the current gold, silver, and platinum spot prices.
  2. Writes the spot prices snapshot to the wp_options table (key: suma_metal_spot_prices).
  3. Looks up each product by SKU (_sku meta key) to find its post_id.
  4. Updates _price, _regular_price, and optionally _sale_price / _sale_price_dates_from / _sale_price_dates_to in wp_postmeta.
  5. Stores _markup_rate, _markup_mode, _metal_weight, and _volume_pricing (tier data) as additional meta.
  6. Processes products in configurable chunk sizes to avoid memory exhaustion.

Configuration Settings

Configured per sync target in the Filament admin:

SettingDefaultDescription
connectionwoocommerceLaravel DB connection name (set in config/database.php)
postmeta_tablewp_postmetaWooCommerce post meta table name
sku_meta_key_skuMeta key used to match SKU
price_meta_key_priceMeta key for the active price
regular_price_meta_key_regular_priceMeta key for the regular price
sale_price_meta_key_sale_priceMeta key for the sale price
sale_price_dates_from_meta_key_sale_price_dates_fromSale start date meta key
sale_price_dates_to_meta_key_sale_price_dates_toSale end date meta key
markup_meta_key_markup_rateMeta key for the markup rate
markup_mode_meta_key_markup_modeMeta key for the markup mode
tier_meta_key_volume_pricingMeta key for volume pricing tiers (stored as JSON)
weight_meta_key_metal_weightMeta key for the product's metal weight in troy ounces
spot_options_tablewp_optionsWordPress options table to write the spot prices snapshot to
spot_option_keysuma_metal_spot_pricesOption name under which the JSON spot snapshot is stored
chunk_size500Number of SKUs processed per DB batch (50–5000)

Spot Prices Option

On every sync run, the provider fetches the current gold, silver, and platinum spot prices from the nFusion General API and writes them to a single wp_options row. This allows WooCommerce themes and plugins to read live spot prices via the standard WordPress get_option() function.

$spotPrices = get_option('suma_metal_spot_prices'); // returns decoded array

The stored value is a JSON object:

{
"gold": { "ask": 4550.93, "bid": 4548.93 },
"silver": { "ask": 72.054, "bid": 71.204 },
"platinum": { "ask": 1893.20, "bid": 1883.20 }
}

If the nFusion General API call fails, the wp_options write is skipped silently — a warning is logged but the rest of the product price sync continues normally. The option retains the value from the previous successful sync.


Database Connection Setup

Add the WooCommerce database credentials to .env:

DB_WOOCOMMERCE_HOST=your-woo-db-host
DB_WOOCOMMERCE_PORT=3306
DB_WOOCOMMERCE_DATABASE=woocommerce_db
DB_WOOCOMMERCE_USERNAME=woo_user
DB_WOOCOMMERCE_PASSWORD=secret

Then register the connection in config/database.php:

'woocommerce' => [
'driver' => 'mysql',
'host' => env('DB_WOOCOMMERCE_HOST', '127.0.0.1'),
'port' => env('DB_WOOCOMMERCE_PORT', '3306'),
'database' => env('DB_WOOCOMMERCE_DATABASE', ''),
'username' => env('DB_WOOCOMMERCE_USERNAME', ''),
'password' => env('DB_WOOCOMMERCE_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation'=> 'utf8mb4_unicode_ci',
],

Markup Modes

ModeDescription
each_fixedFixed dollar markup per unit
weight_percentPercentage of the base ask price, multiplied by product weight (in ounces)

The _markup_mode meta key stores this value for reference by the WooCommerce theme layer.


Volume Pricing Tiers

When tier data is available, the provider stores it as a JSON array in _volume_pricing:

[
{"qty": 1, "ask": 28.50, "markup": 1.50},
{"qty": 10, "ask": 27.00, "markup": 0.00},
{"qty": 25, "ask": 26.00, "markup": -1.00}
]

The Dealers WooCommerce theme reads this meta to display stepped pricing tables.

note

Tier data is sorted and normalized in via the sortTiersByQuantityAscending() function located in app/Utils/PricingUtils.php file.

As of 5/18/2026, we found and issue when syncing data from the nFusion API. In the nFusion dashboard tier data is always sorted by quantity ascending, but tier data from the API comes through in the order tiers are created. Site logic expects tier data to be stored ascending by quantity.


Key File

app/SyncTargets/WooCommerce/WooCommerceSyncTargetProvider.php