Woocommerce预订-从“数据”获取特定值购物车物品对象 [英] Woocommerce Booking - Get a specific value from "Data" Cart item object
问题描述
我有这个数组,它是使用以下几行从WooCommerce转储的:
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…
}
如果我对购物车进行了原始输出($ item),我得到了:
If I make a raw output of my cart ($items) and I get this:
["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"
}
}
我想在某些php变量中使用特定的数据值,但是我找不到实现此目标的正确方法...
I would like to use specific data values in some php variables, and I don't find the right way to achieve this…
我该如何从[ wc_booking_has_resources ]?
How can I do to get the specific value from ["wc_booking_has_resources"]?
谢谢
推荐答案
第一 strong> —首先尝试执行此操作(但我不确定,因为我认为它可以是一个对象,因此可能无法正常工作):
FIRST — 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]['data']['wc_booking_has_resources'] . '<br>';
第二次-使用foreach循环肯定会起作用:
SECOND — With a foreach loop it will work certainly:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$item_data = $item['data'];
}
// displaying the value for test
echo 'Booking has ressources: ' . $item_data['wc_booking_has_resources'];
// or may be this one (as $item['data'] is an object)
echo 'Booking has ressources: ' . $item_data->wc_booking_has_resources;
其中之一肯定有效……
与作者评论有关的更新
如何获得预订请尝试以下方法之一:
How to get The booking try one of these:
// first (very unsure of this one)
$item_data = $items[0]['data']['id'];
echo $item_data['id'] . '<br>'; // not sure this one work
在每个循环中使用a会更好:
Using a for each loop it will be much better:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$item_data = $item['data'];
}
// As $item['data'] is an object
$booking_has_resources = $item_data->booking_has_resources;
// displaying
echo 'Booking has ressources: ' . $booking_has_resources;
这应该有效
这篇关于Woocommerce预订-从“数据”获取特定值购物车物品对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!