Jump to content

Can anyone help with this please


Greystoke

Recommended Posts

Hi,

 

Can anyone help with this please.

 

    $products = $cart->get_products();
    print_r('$products');

}

 

print_r('$products');

 

outputs:

 

Array ( [0] => Array ( [id] => 28{7}14 [name] => Pro Install [model] => pro_install [image] => [price] => 25.0000 [quantity] => 1 [weight] => 0.00 [final_price] => 25 [tax_class_id] => 0 [attributes] => Array ( [7] => 14 ) ) [1] => Array ( [id] => 25 [name] => Keyboard [model] => KB [image] => intkeyboardps2.gif [price] => 69.9900 [quantity] => 1 [weight] => 8.00 [final_price] => 69.99 [tax_class_id] => 1 [attributes] => ) ) 

 

How do I get it to check the Array for 2 values, 'pro_install' and 'custom_install'.

If the values don't exits, I need it to redirect to index.php.

 

Link to comment
https://forums.phpfreaks.com/topic/233125-can-anyone-help-with-this-please/
Share on other sites

Hi,

 

I tried this, but it didn't work.

 

    $products = $cart->get_products();
    print_r($products);
if(!isset($products['pro_install']) || !isset($products['custom_install']))
{
       echo 'test';

}

 

It still outputs 'test', even when 'pro_install' was in the Array.

Please, next time post your print_r with the proper line breaks. Reading a multidimensional array in one line like that is a pain.

 

Here's the deal. $products has two members in your example, [0] and [1]. Each one of those is another array, which have other members. If 'pro_install' and 'custom_install' are supposed to be inside the 'model' key, try this:

foreach($products as $product) {
  if($product['model'] != 'pro_install' && $product['model'] != 'custom_install') {
    echo 'test';
  }
}

That will loop through each product in $products, echoing 'test' every time it doesn't match both.

Sorry about not posting the print_r correctly.

 

What I want it to do is if 'pro_install' or 'custom_install' are not in the array, then redirect.

 

E.g.

If 'pro_install' is there don't redirect.

 

or

 

If 'custom_install' is there don't redirect.

 

or

 

If 'pro_install' and 'custom_install' are there don't redirect.

So you're not sure it'll be the 'model' every time? In that case try this:

$found = false;
foreach($products as $product) {
  if(in_array('pro_install', $product) || in_array('custom_install', $product)) {
    $found = true;
    break;
  }
}
if(!$found) {
  //redirect
  exit;
} else {
  //do something else
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.