大量PHP Notice报错:ps_sharebuttons.php中Undefined index: product
Got it, let's tackle this annoying repeated PHP notice that's clogging up your error log. The issue is straightforward: your ps_sharebuttons module is trying to access $params['product']['id_product'] on line 140, but sometimes the product key doesn't exist in the $params array—usually when the module loads on non-product pages like the homepage, category listings, or checkout.
Why This Happens
The current code assumes $params['product'] is always available:
$key = 'ps_sharebuttons|' . $params['product']['id_product'];
But when there's no product context (like on pages that aren't single product pages), that product index isn't set, hence the "Undefined index" notice.
Quick & Robust Fix: Add a Guard Clause
Just add a check to make sure $params['product'] exists before trying to use it. Update lines 139-142 like this:
{ // Exit early if there's no product data in params if (!isset($params['product'])) { return; // Adjust this if you need to handle empty product cases differently } $key = 'ps_sharebuttons|' . $params['product']['id_product']; if (!empty($params['product']['id_product_attribute'])) { $key .= '|' . $params['product']['id_product_attribute'];
Shorter Fix for PHP 7+ (Null Coalescing)
If your server runs PHP 7 or newer, you can use the null coalescing operator to simplify the check:
{ $product = $params['product'] ?? null; if (!$product) { return; } $key = 'ps_sharebuttons|' . $product['id_product']; if (!empty($product['id_product_attribute'])) { $key .= '|' . $product['id_product_attribute'];
Bonus: Prevent This From Happening Again
- Validate array keys first: Always use
isset()orarray_key_exists()before accessing nested array values, especially in modules that load across multiple page types. - Adjust module loading: You could also configure the
ps_sharebuttonsmodule to only load on product pages via your PrestaShop backend, but adding the guard clause is more reliable (it handles edge cases where the module might load unexpectedly).
内容的提问来源于stack exchange,提问作者Alexandru Theodor

