在WooCommerce中不包括订阅产品的价格之前和之后添加文本 [英] Adding text before and after prices in WooCommerce excluding subscriptions products

查看:0
本文介绍了在WooCommerce中不包括订阅产品的价格之前和之后添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我使用以下代码在显示的产品价格周围添加一些文本("Rent:"和"/day"),如:

function cp_change_product_price_display( $price ) {
    echo 'Rent: ' . $price . '/day';    
}
add_filter( 'woocommerce_get_price_html', 'cp_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cp_change_product_price_display' );

我也使用插件"YITH WooCommerce Subscription",但我的代码有问题。现在,在订阅包中,我有一个价格显示:

  • 租金:20美元/7天/天
  • 租金:80美元/月/天

如何从我的代码中排除订阅产品或订阅类别以避免此问题?

推荐答案

您可以添加条件,查看当前产品是否为订阅。示例。

function cp_change_product_price_display( $price, $instance ) {
    if ( 'yes' === $instance->get_meta( '_ywsbs_subscription' ) ) {
        return $price;
    }
    return 'Rent: ' . $price . '/day';
}

add_filter( 'woocommerce_get_price_html', 'cp_change_product_price_display', 10, 2 );

购物车价格:

function cp_change_product_price_display_in_cart( $price, $cart_item, $cart_item_key ) {
    $product = $cart_item['data'];
    if ( 'yes' === $product->get_meta( '_ywsbs_subscription' ) ) {
        return $price;
    }
    return 'Rent: ' . $price . '/day';
}

add_filter( 'woocommerce_cart_item_price', 'cp_change_product_price_display_in_cart', 10, 3 );

这篇关于在WooCommerce中不包括订阅产品的价格之前和之后添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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