DEV Community

Cover image for Building a Custom WordPress + WooCommerce Integration with a Third-Party Wholesaler
Hassan Hafeez
Hassan Hafeez

Posted on

Building a Custom WordPress + WooCommerce Integration with a Third-Party Wholesaler

One of the things I love about WordPress development is how flexible it can be when solving real-world business problems. Recently, I worked on a project for Bulk Branded
, a UK-based supplier that needed a WooCommerce store tightly integrated with their wholesaler’s catalog.

Instead of manually uploading hundreds of SKUs (and constantly updating prices/stock), I built a custom WordPress plugin that connects to the wholesaler’s API and automatically creates/updates WooCommerce products.

🚩 The Challenge

Fetch thousands of products from a wholesaler’s API.

Keep products synced with stock, categories, and prices.

Match the store’s branding and design requirements.

Handle bulk imports without breaking the server.

🛠️ The Approach

I created a plugin that:

Connects to the wholesaler API.

Maps API data to WooCommerce fields.

Creates categories if they don’t exist.

Generates product variations (e.g., color options).

Handles featured images + galleries dynamically.

Runs in batches via AJAX/Cron to avoid timeouts.

🧑‍💻 A Peek at the Code

Here’s a simplified version of the core sync function:

`//====================== Sync Products From API =================
add_action('wp_ajax_sync_wholesaler_products', 'sync_wholesaler_products');

function sync_wholesaler_products() {
$response = wp_remote_get('https://api.xxxxxxxxxxxxxxx');
$products = json_decode(wp_remote_retrieve_body($response));

if (empty($products)) {
    return wp_send_json_error('No products found');
}

foreach ($products as $item) {
    // 1. Insert product
    $product_id = wp_insert_post([
        'post_type'   => 'product',
        'post_title'  => sanitize_text_field($item->Title),
        'post_content'=> wp_kses_post($item->Description),
        'post_status' => 'publish',
    ]);

    // 2. Add SKU + Pricing
    update_post_meta($product_id, '_sku', $item->SKU);
    foreach ($item->Pricing as $price) {
        add_row('bulk_prices', [
            'min_quantity'  => $price->Quantity,
            'cost_per_unit' => $price->Price
        ], $product_id);
    }

    // 3. Handle Categories
    wp_set_object_terms($product_id, $item->CategoryMain, 'product_cat', true);
    wp_set_object_terms($product_id, $item->CategorySub, 'product_cat', true);

    // 4. Featured Image
    Generate_Featured_Image($item->Images[0]->SrcFull, $product_id);

    // 5. Variations (e.g., Colors)
    foreach ($item->Colours as $colour) {
        $variation_id = bb_create_variation($product_id, [
            'pa_color' => sanitize_title($colour->Name)
        ], $colour->Stock, $item->Pricing[0]->Price);

        Generate_Featured_Image($colour->Image, $variation_id);
    }
}

return wp_send_json_success('Products synced successfully');
Enter fullscreen mode Exit fullscreen mode

}
`

👉 This is a simplified snippet — the real code includes batching, error handling, and more robust taxonomy management.

📚 Lessons Learned

Batch processing is everything — syncing thousands of products in one go can kill your server. Running in chunks via AJAX or WP-Cron makes it stable.

Stick to WooCommerce CRUD functions — never hack the DB directly. It ensures compatibility with future updates.

Dynamic taxonomies save time — programmatically creating categories/attributes on the fly keeps the store clean and organized.

🎉 The Outcome

The result was a fully automated WooCommerce store that:

Syncs with the wholesaler daily.

Creates products with variations, stock levels, and pricing.

Matches the brand’s design guidelines.

Saves the client hours of manual work each week.

Top comments (0)