WooCommerce:基于产品类别的产品价格后缀

时间:2021-02-16 16:46:08

标签: php wordpress woocommerce product suffix

在我的 WooCommerce 网站上,我将面料设置为每半米出售,下面的代码运行正常:

function conditional_price_suffix( $price, $product ) {

    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    $product_categories = array('half-metre');

    if( has_product_categories( $product_categories, $product_id ) )
        $price .= ' ' . __('per ½ metre');

    return $price;
}

但是,我如何操作代码以添加每“四分之一米”的另一个选项并输出“每 ¼ 米”作为后缀。

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

可能是这样的

function conditional_price_suffix( $price, $product ) {
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    $product_categories = array('half-metre');
    $product_categories2 = array('quarter-metre');
    if( has_product_categories( $product_categories, $product_id ) ) {
        $price .= ' ' . __('per ½ metre');
    } elseif( has_product_categories( $product_categories2, $product_id ) ) {
        $price .= ' ' . __('per ¼ metre');
    }
    return $price;
}