Woocommerce通过ID获取产品价值 [英] Woocommerce Get Product Values by ID

查看:224
本文介绍了Woocommerce通过ID获取产品价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图通过产品ID在自定义模板上获取产品数据,现在我有了此代码来获取产品标题.

Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.

$productId = 164;
echo $p_title = get_the_title( $productId );

寻找简短说明,价格,产品图片,产品网址,产品品牌.也许循环会更好,但该循环应使用产品静态ID.

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.

谢谢.

推荐答案

创建新的产品对象可能会更好地为您服务.

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();

请注意,简短描述仅是帖子的post_excerpt.如果在循环之外使用(自动定义$post),则需要直接获取该帖子.

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 = 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 );

因为WooCommerce产品类将使用get_post()自动定义$product->post属性.

since the WooCommerce product class will automatically define the $product->post property with get_post().

apply_filters()表示此时可以附加功能以修改$product->post->post_content的内容.至少,我知道wpautop()附加到woocommerce_short_description过滤器以创建段落分隔符.

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.

这篇关于Woocommerce通过ID获取产品价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆