【问题标题】:Using Woocommerce product category as condition in an if statement在 if 语句中使用 Woocommerce 产品类别作为条件
【发布时间】:2018-11-30 11:25:36
【问题描述】:

我是 php 和 Woocommerce 的新手,我从某处借用了一些代码,以便在每个价格后添加一个文本。 我把这段代码放在functions.php中,它工作正常。

但我想使用产品类别作为条件来确定是否应该显示 tex。

请看下面的例子:

if ( "ProductCategoryID == Something") {
    $textafter = '( Ex. Moms )'; //add your text
    return $price . '<span style="font-size: 14px; margin-left:10px; color:#E9483F" class="price-description">' . $textafter . '</span>';
}

任何帮助我解决这种情况将不胜感激?

【问题讨论】:

    标签: php wordpress if-statement woocommerce custom-taxonomy


    【解决方案1】:

    Woocommerce 产品类别是自定义分类法,您将使用WordPress conditional function has_term(),这样:

    if( has_term( array('Something'), 'product_cat', get_the_id() ) )
        $textafter = '( Ex. Moms )'; //add your text 
        return $price . '<span style="font-size: 14px; margin-left:10px; color:#E9483F" class="price-description">' . $textafter . '</span>';
    }
    

    或与$product WC_Product 对象)

    if( has_term( array('Something'), 'product_cat', $product->get_id() ) )
        $textafter = '( Ex. Moms )'; //add your text 
        return $price . '<span style="font-size: 14px; margin-left:10px; color:#E9483F" class="price-description">' . $textafter . '</span>';
    }
    

    “某物”是您的产品类别……

    条件 has_term() 函数将接受产品类别 ID、名称或名称。

    【讨论】:

    • 感谢您的回答!只是一个问题,我应该将“某事”替换为类别的书面名称还是 ID?
    • 它就像一个魅力,我学到了一些新东西。谢谢! :)
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2019-06-20
    • 2021-04-19
    • 1970-01-01
    • 2018-02-07
    • 2021-12-29
    相关资源
    最近更新 更多