Won't add to cart. Undefined index: components

Created on 14 August 2024, 8 months ago
Notice: Undefined index: components in commerce_webform_get_products_from_webform_submission() (line 283 of C:\wamp64\www\jalt_d7100\sites\all\modules\commerce_webform\commerce_webform.module).
Warning: Invalid argument supplied for foreach() in commerce_webform_get_products_from_webform_submission() (line 285 of C:\wamp64\www\jalt_d7100\sites\all\modules\commerce_webform\commerce_webform.module).
Notice: Undefined index: sid in commerce_webform_order_create() (line 166 of C:\wamp64\www\jalt_d7100\sites\all\modules\commerce_webform\commerce_webform.rules.inc).

Hi, I ran into these problems on a fresh D7 101 build. After submitting a webform with a product it pops up this error code and does not create a shopping cart item.

Can you help?

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada seanenroute

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @seanenroute
  • What version of Webform Rules module are you running? I was getting the same issue when using Webform Rules 7.x-1.7.

    Downgrading Webform Rules module back to 7.x-1.6 fixed the issue for me.

  • Here is a patch I applied that seems to work with Webform Rules 7.x-1.7:

    In commerce_webform_get_products_from_webform_submission() in commerce_webform.module modify this function from this:

    function commerce_webform_get_products_from_webform_submission($submission) {
      $product_ids = array();
      $products = array();
      $data = array();
    
      $components = $submission['components'];
    
      foreach ($components as $component) {
        if ($component['component']['type'] == 'productfield') {
          foreach ($component['value'] as &$value) {
            $value = json_decode($value);
            if (!empty($value->product_id) && $value->quantity > 0) {
              $product_ids[] = $value->product_id;
              if (empty($data[$value->product_id])) {
                $data[$value->product_id] = $value;
                $data[$value->product_id]->required = $component['component']['required'];
                $data[$value->product_id]->choose_quantity = $component['component']['extra']['choose_quantity'];
              }
              else {
                // Now we need to change the quantity.
                $data[$value->product_id]->quantity = $data[$value->product_id]->quantity + $value->quantity;
              }
            }
          }
        }
      }
    
      $products = commerce_product_load_multiple($product_ids);
    
      foreach ($products as $product) {
        $data[$product->product_id]->product = $product;
      }
    
      return $data;
    }

    Change it to this:

    function commerce_webform_get_products_from_webform_submission($submission) {
      $product_ids = array();
      $products = array();
      $data = array();
    
      $webform_node = reset($submission);
      $webform = node_load($webform_node->nid);
      $components = $webform->webform['components'];
    
      foreach ($webform_node->data as $cid => $val) {
        $c_key = $components[$cid]['form_key'];
        $webform_node->components[$c_key] = array(
          'nid' => $components[$cid]['nid'],
          'cid' => $cid,
          'pid' => $components[$cid]['pid'],
          'form_key' => $c_key,
          'name' => $components[$cid]['name'],
          'type' => $components[$cid]['type'],
          'value' => $val,
          'extra' => $components[$cid]['extra'],
          'required' => $components[$cid]['required'],
          'weight' => $components[$cid]['weight'],
          'page_num' => $components[$cid]['page_num'],
          );
      }
    
      foreach ($webform_node->components as $component) {
        if ($component['type'] == 'productfield') {
          foreach ($component['value'] as &$value) {
            $value = json_decode($value);
            if (!empty($value->product_id) && $value->quantity > 0) {
              $product_ids[] = $value->product_id;
              if (empty($data[$value->product_id])) {
                $data[$value->product_id] = $value;
                $data[$value->product_id]->required = $component['required'];
                $data[$value->product_id]->choose_quantity = $component['extra']['choose_quantity'];
              }
              else {
                // Now we need to change the quantity.
                $data[$value->product_id]->quantity = $data[$value->product_id]->quantity + $value->quantity;
              }
            }
          }
        }
      }
    
      $products = commerce_product_load_multiple($product_ids);
    
      foreach ($products as $product) {
        $data[$product->product_id]->product = $product;
      }
    
      return $data;
    }
    
    
Production build 0.71.5 2024