3

I have code below where my product attributes will be select and add to cart

foreach($product->suboptions as $subs) {
      $customAttributes[] = [
          'attr' => [
              'label' => $subs->title,
              'price' => $subs->price,
          ]
      ];
    }

the problem is if my product doesn't have any attribute to choose i will get error while trying to add product to cart. So I tried to do something like code below but i get this error:

Parse error: syntax error, unexpected '{'

if(!empty($product->suboptions){
        foreach($product->suboptions as $subs) {
          $customAttributes[] = [
              'attr' => [
                  'label' => $subs->title,
                  'price' => $subs->price,
              ]
          ];
        }
      }

any idea?

Update

after fixing if statement i get this error on 'attributes' => $customAttributes,

Undefined variable: customAttributes

here is my full code

public function addingItem($id)
    {
      $product = Product::where('id', $id)->firstOrFail();

      if(!empty($product->suboptions)){
        foreach($product->suboptions as $subs) {
          $customAttributes[] = [
              'attr' => [
                  'label' => $subs->title,
                  'price' => $subs->price,
              ]
          ];
        }
      }
      Cart::add(array(
        'id' => $product->id,
        'name' => $product->title,
        'price' => $product->price,
        'quantity' => 1,
        'attributes' => $customAttributes,
      ));
      Session::flash('success', 'This product added to your cart successfully.');
      return redirect()->back();
    }
Community
  • 1
  • 1
mafortis
  • 6,750
  • 23
  • 130
  • 288

1 Answers1

2

You are missing a closing bracket in your if statement.

Your error message hints at this, by saying there is a syntax error - specifically an unexpected {.

First step to troubleshoot would be to follow your code through and look for each { and inspect the code preceding it as there is something in the preceding code that is leading to the error. If you find everything is OK in the preceding code then move on to the next occurrence of {.

if(!empty($product->suboptions){ should be if(!empty($product->suboptions)){.

As a whole:

if(!empty($product->suboptions)){
    foreach($product->suboptions as $subs) {
      $customAttributes[] = [
          'attr' => [
              'label' => $subs->title,
              'price' => $subs->price,
          ]
      ];
    }
  }

Update to address edit

Again, the hint from the error is that the variable $customAttributes doesn't exist.

Let's look at the scope of $customAttributes:

if(!empty($product->suboptions)){
    foreach($product->suboptions as $subs) {
        $customAttributes[] = [
            'attr' => [
                'label' => $subs->title,
                'price' => $subs->price,
            ]
        ];
    }
}

Each time you loop over the products suboptions, you declare $customAttributes and it exists only for that iteration of the loop. Going on from this, once you are out of the for loop, $customAttributes no longer exists.

As such you get the error that $customAttributes is an undefined variable when you try to use it on your Cart model.

To resolve this, specify $customAttributes outside of the for loop and push to the array on each iteration of the loop. Something along the lines of this:

$customAttributes = [];
if(!empty($product->suboptions)){
    foreach($product->suboptions as $subs) {
        array_push($customAttributes, [
            'attr' => [
                'label' => $subs->title,
                'price' => $subs->price,
            ]
        );
    }
}
James
  • 15,754
  • 12
  • 73
  • 91
  • thanks James now i get another error (i updated my question) – mafortis Jan 23 '18 at 01:34
  • @mafortis have edited my answer to address your edit. – James Jan 23 '18 at 01:45
  • `specify $customAttributes outside of the for loop and push to the array on each iteration of the loop.` how to do that? i didn't get it completely! – mafortis Jan 23 '18 at 01:48
  • 1
    Just set `'attributes' => $customAttributes ?? null,` because it is undefined if condition in previous block not met. Or something similar. – Tpojka Jan 23 '18 at 01:49
  • @Tpojka that still won't work, if the `if` statement evaluates to `true`, `$customAttributes` is still only scoped to the forloop and won't exist outside it - `'attributes' => $customAttributes ?? null` will always set null. – James Jan 23 '18 at 01:53
  • @mafortis I have provided an example of how you **could** do this. – James Jan 23 '18 at 01:56
  • and it's working, thank you so much. – mafortis Jan 23 '18 at 02:00
  • @James Variable if set in foreach loop, [it will exist](https://3v4l.org/DITSg) later in that method. If not set, there is fallback in null coalesce operator. Of course, null coalesce is not supported in PHP <7. – Tpojka Jan 23 '18 at 02:03