-1

EDIT: Got it, can't believe I missed it -

$HTTP_POST_VARS isn't global, changed it to a parameter to be passed in.

I normally use $_POST but in oscommerce the default is this and so I used that to maintain similarity.

I cannot get this code to work and I have no idea why.

   function check_product_available() {

global $cart;
$products = $cart->get_products();

  //product exclusion
    //check to see if the product is in one of the limited categories
    $check_product_query = tep_db_query( $sql = 'SELECT products_id
                                                 FROM discount_coupons_to_products
                                                 WHERE coupons_id="'.tep_db_input( $HTTP_POST_VARS['couponcart'] ).'"' );
    $exlproducts = array();
    if( tep_db_num_rows( $check_product_query ) > 0 ) {
      while( $products = tep_db_fetch_array( $check_product_query ) ) {
        $exlproducts[] = $products['products_id'];
      }
  }
        for ($i=0, $n=sizeof($products); $i<$n; $i++) {
    if( in_array( $products[$i]['id'], $exlproducts ) ) {
      //use this to debug exclusions:
      return false;
    } 
    }
    return true;
  }
  //end product exclusion

I have tried moving the loops around and it still always returns true. I have echoed out the products array and id is in there and ran the sql query hardcoding the coupon id and it also works fine.

  • it seems you don't select any 'id' field in your query, why should you have it in the associative array $products? That's why your last if statement always go to the else section (return true). IMHO. – spider Nov 25 '11 at 16:45
  • Which loop is always returning true? If it is `if( in_array( $products[$i]['id'], $exlproducts ) ) { //use this to debug exclusions: return false; } else { return true; }`, I would suggest possibly printing out the entire contents of both `products[]` and `exlproducts` and seeing if they match exactly. – CHawk Nov 25 '11 at 16:45

2 Answers2

3

Your for loop makes no sense as it will only loop one time. Returning a value will end all other execution. What you probably want is to return false when it is in the array and true in all other conditions.

for ($i=0, $n=sizeof($products); $i<$n; $i++) {
    if( in_array( $products[$i]['id'], $exlproducts ) ) {
        //use this to debug exclusions:
        return false;
    }
}
return true;

Edit: You really should not use $HTTP_POST_VARS anymore.

str
  • 42,689
  • 17
  • 109
  • 127
1

Got it....

$HTTP_POST_VARS isn't global, changed it to a parameter to be passed in.