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

查看:30
本文介绍了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天全站免登陆