php 在 WooCommerce 中获取当前产品 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34137129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the current product ID in WooCommerce
提问by R. De Caluwé
I'm using the WooCommerce pluginin Wordpressfor a project that i am currently working on.
我使用的是WooCommerce插件在WordPress的一个项目,我目前工作的。
Normally, when you want to buy something (from a customer perspective) you'll add the product to the cart and buy it.
通常,当您想购买某物时(从客户的角度),您会将产品添加到购物车并购买。
The problem is, the client I am working for doesn't want to use the cart. Instead, he wants customers to (when they click on a product) go to a page where they'll need to fill out their name, email adress etc. and send an email for a request to buy the specific product.
问题是,我为之工作的客户不想使用购物车。相反,他希望客户(当他们点击产品时)转到需要填写姓名、电子邮件地址等的页面,然后发送电子邮件请求购买特定产品。
But I am running into a little bit of trouble. Creating a form and sending an email is not a problem, but I need to post the current product ID to the form in order for it to work and somehow I cannot find a way to post it to the page. Is there a specific variable in which the current product ID is stored? or how do I get the product ID for me to store in a variable so I can post it to my form?
但我遇到了一点麻烦。创建表单并发送电子邮件不是问题,但我需要将当前产品 ID 发布到表单中才能使其工作,但不知何故我找不到将其发布到页面的方法。是否存在存储当前产品 ID 的特定变量?或者我如何获取产品 ID 以存储在变量中,以便我可以将其发布到我的表单中?
Many thanks, René
非常感谢,雷内
回答by Rapha?l Vigée
You can get it inside Wordpress like this :
您可以像这样在 Wordpress 中获取它:
$id = $product->id;
$id = $post->id // Not working
$id = $post->id // Not working
EDIT (according to comment):Since WooCommerce 3, product properties should not be accessed directly, so $product->id
will log a PHP error. Instead use $product->id()
.
编辑(根据评论):从WooCommerce 3 开始,不应直接访问产品属性,因此$product->id
会记录 PHP 错误。而是使用$product->id()
.
回答by tazziedave
Latest method (WooCommerce 3.0.8) is $product->get_id()
最新方法(WooCommerce 3.0.8)是 $product->get_id()