I have a problem using an array within a foreach within a class. I get the warning..
PHP Warning: Invalid argument supplied for foreach()
on line
foreach($this->recordOfDiscounts as $key => $discount)
The problematic function is below,
public function modify_price( $price, $product_id ) {
foreach($this->recordOfDiscounts as $key => $discount) {
foreach($discount['get_one_free'] as $id) {
if($id == $product_id){
if( $discount['valid'] > 0 ) {
$price -= $discount['cost'];
$this->recordOfDiscounts[$key]['valid'] -= 1;
}
}
}
}
return $price;
}
I'm new to PHP, but what I've gathered is that the class scope ($this->) needs to prefix a var within a class.
error_log(print_r($this->recordOfDiscounts),0);
outputs the correct array info, so I know its defined.
Array
(
[0] => Array
(
[valid] => 1
[buy_one] => 2351
[cost] => 20
[old_cost] => 20
[get_one_free] => Array
(
[0] => 2471
[1] => 2470
[2] => 2472
[3] => 2473
[4] => 2474
[5] => 2475
[6] => 2476
[7] => 2477
)
)
)
$this->recordOfDiscountson the first line of your modify_price function.