php Woocommerce 通过 ID 获取产品价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35165402/
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
Woocommerce Get Product Values by ID
提问by Justin K
Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.
尝试通过产品 ID 获取自定义模板上的产品数据,现在我有此代码来获取产品标题。
$productId = 164;
echo $p_title = get_the_title( $productId );
looking for Short Description, Price, Product Image, Product Url, Product Brand. Or might be loop will be better but that loop should work with product static ID.
寻找简短描述、价格、产品图片、产品网址、产品品牌。或者可能循环会更好,但该循环应该与产品静态 ID 一起使用。
Thanks in advance.
提前致谢。
回答by helgatheviking
You would probably be better served by creating a new product object.
创建一个新的产品对象可能会更好地为您服务。
$productId = 164;
$product = wc_get_product( $productId );
echo $product->get_title();
echo $product->get_price_html();
Note, that the short description is merely the post's post_excerpt
. If using outside of the loop (where $post
is automatically defined) then you would need to get the post directly.
请注意,简短描述只是帖子的post_excerpt
. 如果在循环之外使用($post
自动定义的位置),那么您需要直接获取帖子。
$post = get_post( $productId );
echo apply_filters( 'woocommerce_short_description', $post->post_excerpt );
or alternatively, if you've already defined the product object you could do
或者,如果您已经定义了产品对象,您可以这样做
echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt );
since the WooCommerce product class will automatically define the $product->post
property with get_post()
.
因为 WooCommerce 产品类将自动$product->post
使用get_post()
.
apply_filters()
means that functions can be attached at this point to modify the content of $product->post->post_content
. At a minimum, I know that wpautop()
is attached to the woocommerce_short_description
filter to create paragraph breaks.
apply_filters()
表示此时可以附加函数来修改$product->post->post_content
. 至少,我知道它wpautop()
附加到woocommerce_short_description
过滤器以创建分段符。