从WooCommerce结帐页上的产品获取Wordpress发布ID?
问题描述:
我有一些自定义字段,例如“清洁费”和“保证金”,我想添加到结帐页上的项目,我认为这将是非常直接的,但我没有看到任何之间的关联项目和项目来源的自定义帖子。我会以某种方式必须得到从项目的帖子ID能够使用的线路是这样的...从WooCommerce结帐页上的产品获取Wordpress发布ID?
<?php $thisItemsSecurityDeposit = get_post_meta($cart_item['the post id'], 'accommodation_security_deposit', true); echo $thisItemsSecurityDeposit ; ?>
...并有项目的价格之后包括保证金。
我在想,也许有一些对象或会话变量数组,其中包含关联产品与产品来自的帖子ID的关键字。
请参阅... http://www.dreamhomevacationrentals.com/hotels/spanish-modern-retreat/ 然后,单击“可用性”,然后单击“立即预订”以查看购物车的实际使用情况。 任何帮助,不胜感激。
答
车对象(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