← Back to Portfolio
WordPress
WooCommerce
PHP
REST API
Plugin Dev

Custom Composite Product Plugin Specialist

Client: Industrial E-Commerce ClientYear: 2024
Custom Composite Product Plugin Specialist

The Challenge

A WooCommerce store selling configurable industrial equipment needed a custom composite product plugin that could dynamically assemble product components from a 4,000-item catalog. Native WooCommerce product bundling was too rigid β€” it couldn't handle conditional component rules, real-time pricing recalculation, or per-component metadata fetching at scale. Each page load triggered dozens of individual database queries, pushing configurator load times above 2.8 seconds.

System Architecture Strategy

The solution required a custom WordPress plugin built on top of WooCommerce with a WP REST API layer for component discovery, a PHP-driven rules engine for conditional component logic, and a JavaScript-powered front-end configurator that batched metadata requests to eliminate N+1 query problems. The API endpoint was designed as a single aggregated query that returned all component metadata, pricing, and availability in one response.

Code Implementation

A custom WP REST API endpoint aggregates all component metadata in a single JOIN query, eliminating per-component database round trips.

// Custom WP REST API endpoint for composite product metadata
add_action('rest_api_init', function () {
    register_rest_route('composite/v1', '/components', [
        'methods'             => WP_REST_Server::READABLE,
        'callback'            => 'get_composite_components',
        'permission_callback' => '__return_true',
        'args' => [
            'ids' => ['required' => true, 'type' => 'string'],
        ],
    ]);
});

function get_composite_components(WP_REST_Request $request) {
    global $wpdb;
    $ids          = array_map('intval', explode(',', $request['ids']));
    $placeholders = implode(',', array_fill(0, count($ids), '%d'));

    $results = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT p.ID, p.post_title, pm.meta_key, pm.meta_value
             FROM {$wpdb->posts} p
             JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
             WHERE p.ID IN ($placeholders)
               AND pm.meta_key IN ('_price', '_stock_status', '_sku')
             ORDER BY p.ID",
            ...$ids
        ),
        ARRAY_A
    );

    return rest_ensure_response(group_by_product_id($results));
}

Scalability Results

The optimized endpoint reduced component-loading time from 2.8s to 180ms for a 48-component configurator. The plugin handles over 300 daily configuration sessions with zero database bottlenecks. The modular rules engine allows new component types to be registered without touching core plugin logic, cutting future feature development time significantly.