Hello,
I found a significant performance issue in the standard Required Products
add-on on the backend product update page.
Environment
Edition: CS-Cart Ultimate
Version: [Multi-Vendorv4.20.1]
PHP: 8.2
Web server: Nginx
Database: Separate database VPS (intranet) on the same dedicated server.
Average network latency between the application and database servers: 0.315 ms
Affected file
app/addons/required_products/controllers/backend/products.post.php
Steps to reproduce
- Enable the standard Required Products add-on.
- Open an existing product in the administration panel.
- Measure the request with the CS-Cart debugger.
- Compare the results after disabling the Required Products add-on.
Original result with Required Products enabled
Page generation time: 7.2822 seconds
SQL query count: 3,973
SQL execution time: 5.6124 seconds
Result with Required Products disabled
Page generation time: 1.004 seconds
SQL query count: 364
SQL execution time: 0.429 seconds
The debugger backtrace showed this call chain:
app/addons/required_products/controllers/backend/products.post.php:34
fn_get_product_data()
fn_get_product_features()
fn_get_product_feature_variants()
db_get_field()
The add-on calls fn_get_product_data($product_id), apparently only to retrieve
the company_id value.
This causes all product features and feature variants to be loaded. In our
case, similar COUNT queries against product_feature_variants were executed
thousands of times.
Current code
$product_data = fn_get_product_data($product_id);
$product_company_id = !empty($product_data)
? $product_data[‘company_id’]
: 0;
Tested fix
$product_company_id = (int) db_get_field(
‘SELECT company_id FROM ?:products WHERE product_id = ?i’,
$product_id
);
Result after the fix, with Required Products enabled
Page generation time: 1.1474 seconds
SQL query count: 423
SQL execution time: 0.47735 seconds
The product edit page became approximately 6.3 times faster.
Functional verification
The Required Products tab still works.
The required-product relation is saved correctly.
The main product and required product are both automatically added to the cart.
Could you please confirm whether this is a bug and consider replacing the full
fn_get_product_data() call with a direct company_id lookup in an official
release?
Best regards,
Emre