-2

I have this array, that is dumped from WooCommerce using these lines:

$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) { 
    // some code here…
} 

If I make a raw output of my cart ($items) I get this:

array(1) {
  ["f584d8671586d336d84e8cf9ed43303c"]=>

  array(11) {


["booking"]=>
array(15) {
  ["_year"]=>
  int(2016)
  ["_month"]=>
  int(8)
  ["_day"]=>
  int(28)
  ["_persons"]=>
  array(1) {
    [0]=>
    int(1)
  }
  ["_date"]=>
  string(9) "2016-8-28"
  ["date"]=>
  string(13) "28 août 2016"
  ["_time"]=>
  string(5) "21:30"
  ["time"]=>
  string(11) "21 h 30 min"
  ["_qty"]=>
  int(1)
  ["Personnes"]=>
  int(1)
  ["_start_date"]=>
  int(1472419800)
  ["_end_date"]=>
  int(1472421600)
  ["_all_day"]=>
  int(0)
  ["_cost"]=>
  int(0)
  ["_booking_id"]=>
  int(13013)
}



["product_id"]=>
int(12856)
["variation_id"]=>
int(0)
["variation"]=>
array(0) {
}
["quantity"]=>
int(1)
["line_total"]=>
float(0)
["line_tax"]=>
int(0)
["line_subtotal"]=>
int(0)
["line_subtotal_tax"]=>
int(0)
["line_tax_data"]=>
array(2) {
  ["total"]=>
  array(0) {
  }
  ["subtotal"]=>
  array(0) {
  }
}
["data"]=>
object(WC_Product_Booking)#11131 (20) {
  ["availability_rules":"WC_Product_Booking":private]=>
  array(0) {
  }
  ["id"]=>
  int(12856)
  ["post"]=>
  object(WP_Post)#11132 (24) {
    ["ID"]=>
    int(12856)
    ["post_author"]=>
    string(2) "14"
    ["post_date"]=>
    string(19) "2016-08-16 22:04:09"
    ["post_date_gmt"]=>
    string(19) "2016-08-16 20:04:09"
    ["post_content"]=>
    string(0) ""
    ["post_title"]=>
    string(10) "La Cuchara"
    ["post_excerpt"]=>
    string(0) ""
    ["post_status"]=>
    string(7) "publish"
    ["comment_status"]=>
    string(4) "open"
    ["ping_status"]=>
    string(6) "closed"
    ["post_password"]=>
    string(0) ""
    ["post_name"]=>
    string(12) "la-cuchara-2"
    ["to_ping"]=>
    string(0) ""
    ["pinged"]=>
    string(0) ""
    ["post_modified"]=>
    string(19) "2016-08-16 22:13:52"
    ["post_modified_gmt"]=>
    string(19) "2016-08-16 20:13:52"
    ["post_content_filtered"]=>
    string(0) ""
    ["post_parent"]=>
    int(0)
    ["guid"]=>
    string(59) ""
    ["menu_order"]=>
    int(0)
    ["post_type"]=>
    string(7) "product"
    ["post_mime_type"]=>
    string(0) ""
    ["comment_count"]=>
    string(1) "0"
    ["filter"]=>
    string(3) "raw"
  }
  ["product_type"]=>
  string(7) "booking"
  ["shipping_class":protected]=>
  string(0) ""
  ["shipping_class_id":protected]=>
  int(0)
  ["total_stock"]=>
  NULL
  ["supports":protected]=>
  array(0) {
  }
  ["price"]=>
  string(1) "0"
  ["wc_display_cost"]=>
  string(0) ""
  ["wc_booking_base_cost"]=>
  string(0) ""
  ["wc_booking_min_duration"]=>
  string(1) "1"
  ["wc_booking_cost"]=>
  string(0) ""
  ["wc_booking_has_resources"]=>
  string(2) "no"
  ["wc_booking_has_persons"]=>
  string(3) "yes"
  ["wc_booking_has_person_types"]=>
  string(2) "no"
  ["wc_booking_min_persons_group"]=>
  string(1) "1"
  ["tax_status"]=>
  string(7) "taxable"
  ["stock_status"]=>
  string(7) "instock"
  ["manage_stock"]=>
  string(2) "no"
    }
  }
}

I would like to use specific data values in php variables (the booking date, the time and the person):

  ["date"]=> string(13) "28 août 2016"

  ["_persons"]=> array(1) { [0]=> int(1) }

  ["time"]=> string(11) "21 h 30 min"

How can I do to get this specific values from WooCommerce cart object?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Zufy
  • 65
  • 6
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Charlotte Dunois Aug 22 '16 at 09:15
  • Hello Charlotte, i have this array i want to get the date 28 août 2016 from booking date key and put it into variable to use it in another function ... i failed printing it out . – Zufy Aug 22 '16 at 09:21
  • Note that [urgent begging is firmly discouraged here](http://meta.stackoverflow.com/q/326569/472495). – halfer Aug 22 '16 at 09:42
  • ok halfer i changed the title , sorry – Zufy Aug 22 '16 at 09:46
  • What is your `foreach()` doing - is it `var_dump($values)`? At present it is empty, so it is hard to work out what is happening. – halfer Aug 22 '16 at 11:41
  • At a guess, you might be after `$items['booking']['date']`, assuming `$items` is either a real array or an iterable object. – halfer Aug 22 '16 at 11:44
  • here is the full foreach boolean : $items = $woocommerce->cart->get_cart(); foreach($items as $item => $values) { $_product = $values['data']->post; echo "".$_product->post_title.'
    Quantity: '.$values['quantity'].'
    '; $price = get_post_meta($values['product_id'] , '_date', true); echo " Price: ".$price."
    "; }
    – Zufy Aug 22 '16 at 11:56

1 Answers1

4

You could try first this (but I am unsure as I think it can be an object, so may be it will not work):

 echo $items[0]['booking']['date'] . '<br>';
 echo $items[0]['booking']['_persons'] . '<br>';
 echo $items[0]['booking']['time'] . '<br>';

If not working with a foreach loop it will:

$items = WC()->cart->get_cart();

foreach($items as $item) { 
    $date = $item['booking']['date'];
    $person = $item['booking']['_persons'];
    $time = $item['booking']['time'];
}
// displaying values for test
echo 'Booking - Date: ' . $date . '/ Person: ' . $person . '/ Time: ' . $time;

One of these will work, for sure…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thank you ,, it works :) what about ["wc_booking_has_resources"]=> string(2) "no" – Zufy Aug 22 '16 at 12:34
  • you are very welcome , thanks to you i solved an issue that took me a lot of time :) from the same array i need to get this two elements ["wc_booking_has_resources"]=> string(2) "no" ["_booking_id"]=> int(13013) the returned values should be no and 13013 Thanks in advance sir – Zufy Aug 26 '16 at 17:18
  • please help with the second issue – Zufy Aug 27 '16 at 13:03
  • http://stackoverflow.com/questions/39181674/how-to-get-wc-booking-has-resources-value-from-the-woocommerce-cart – Zufy Aug 27 '16 at 13:28