You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

大量PHP Notice报错:ps_sharebuttons.php中Undefined index: product

Fixing "Undefined index: product" in ps_sharebuttons.php Line 140

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() or array_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_sharebuttons module 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:00:32