我正在尝试编辑简短描述模板,使其在变量(单个)产品页面上与在简单产品上不同.该页面中的代码如下:
global $post;
if ( ! $post->post_excerpt )
    return;
?>
<div itemprop="description">
    <?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
</div>
我想在if语句中添加一些类似的代码
如果帖子有变化,请不要显示简短描述,如果简单产品DO显示
但我在代码中找不到任何方法来区分常规的简单产品帖子和可变的产品帖子(有变化).通过Woo网站(http://docs.woothemes.com/wc-apidocs/)查看API文档,我发现没有那种.
Dan*_*jel 52
使用$product->is_type()功能检查产品类型.要检查产品是否为可变产品,请使用:
global $product;
// $product->is_type( $type ) checks the product type, string/array $type ( 'simple', 'grouped', 'variable', 'external' ), returns boolean
if ( $product->is_type( 'variable' ) ) {}
还有一个
$product->get_type()函数可以将产品的内部类型作为字符串返回.
Ste*_*hen 27
经过多次心痛,我找到了以下两种解决方案:
在产品循环中,您可以使用:
 if( $product->has_child() ) { 
但由于某些原因,在单个产品页面的简短描述中,我不得不使用这个:
global $post;
$children = get_pages('child_of='.$post->ID);
if( count( $children ) !== 0 ) {
希望这可以帮助那些在我身边挣扎的人......
Ser*_*ons 10
变量产品始终基于WC_Product_Variable类。例如,WooCommerce 订阅就遵循这种方法。
因此,检查可以是:
is_a( $product, 'WC_Product_Variable' )
这确保了无论是否有儿童,产品的类型都是可变的。而且速度很快。
| 归档时间: | 
 | 
| 查看次数: | 36964 次 | 
| 最近记录: |