車對象(WC()->cart->cart_contents
)的cart_contents
變量包括的信息在車中的每個項目的陣列的...我的樣本車內容一個var_dump
看起來如下:
["cart_contents"]=> array(1) {
["7cbbc409ec990f19c78c75bd1e06f215"]=>
array(5) {
["product_id"]=> int(70)
["variation_id"]=> string(0) ""
["variation"]=> string(0) ""
["quantity"]=> int(1)
["data"]=> object(WC_Product_Simple)#530 (3) {
["id"]=> int(70)
["post"]=> object(WP_Post)#532 (24) {
["ID"]=> int(70)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2013-06-07 11:25:01"
["post_date_gmt"]=> string(19) "2013-06-07 11:25:01"
["post_content"]=> string(278) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo."
["post_title"]=> string(12) "Flying Ninja"
["post_excerpt"]=> string(278) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo."
["post_status"]=> string(7) "publish"
["comment_status"]=> string(4) "open"
["ping_status"]=> string(6) "closed"
["post_password"]=> string(0) ""
["post_name"]=> string(12) "flying-ninja"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2014-10-06 16:53:48"
["post_modified_gmt"]=> string(19) "2014-10-06 16:53:48"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(67) "http://demo2.woothemes.com/woocommerce/?post_type=product&p=70"
["menu_order"]=> int(0)
["post_type"]=> string(7) "product"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "4"
["filter"]=> string(3) "raw"
}
["product_type"]=> string(6) "simple"
}
}
}
因此,在一個foreach()
環比車的內容,產品ID會被抓住,像這樣:
$contents = WC()->cart->cart_contents;
if($contents) foreach ($contents as $cart_item){
echo $cart_item['product_id'];
}
如果你已經有了具體的$cart_item
變量(例如:你重新在購物車模板中,並已經在Woo的循環中),那麼你只需訪問product_id
數組。 $cart_item['product_id'];
我總是覺得使用var_dump()
或print_r()
知道我可以使用哪些變量或數組鍵。
順便說一句,你可能想看看Product Add-ons這將做你正在描述的。
非常感謝您的意見Helga,但我只是需要woo商務產品來自的帖子的post_id。我能夠在發佈表上爲產品標題運行自定義的wp查詢,這與產品標題相同,並且我還查詢了帖子類型和發佈狀態,以便我可以獲取帖子ID。請參閱以下代碼... – 2014-10-08 17:20:35
global $ wpdb; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t使用產品稱號//,找到的帖子ID。 \t \t \t \t \t \t \t \t \t $ querystr =「 \t \t \t \t \t \t \t \t \t \t選擇$ wpdb->的帖子。ID \t \t \t \t \t \t \t \t \t \t FROM $ wpdb->帖子 \t \t \t \t \t \t \t \t \t \t WHERE $ wpdb-> posts.post_title = '$ this_products_title' \t \t \t \t \t \t \t \t \t \t和$ wpdb-> posts.post_status = '發佈' \t \t \t \t \t \t \t \t \t \t和$ wpdb-> posts.post_type = '住宿' \t \t \t \t \t \t \t \t \t \t和WPDB $ - > posts.post_date get_row($ querystr,ARRAY_A); \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t //的print_r($ pageposts); \t \t \t \t \t \t \t \t \t //回聲$ pageposts [ID]; –
2014-10-08 17:20:59
這是一個有點太多的代碼在評論中清晰可讀。你能編輯你的問題,並正確地格式化代碼,以便我可以閱讀它嗎?但是,除非您使用順序訂單號插件,否則產品ID是發佈ID。因此,'$ cart_item ['product_id'];'是購物車中特定商品的發佈ID。 – helgatheviking 2014-10-08 17:23:36