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?
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();
}
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,
]
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With