在 WooCommerce 中的价格前后添加文本,不包括订阅产品

Adding text before and after prices in WooCommerce excluding subscriptions products

在 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”,但我的代码有问题。现在在订阅包中,我有以下价格显示之一:

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

以下将排除 WooCommerce 订阅产品...

add_filter( 'woocommerce_get_price_html', 'change_product_price_display', 10, 2 );
add_filter( 'woocommerce_cart_item_price', 'change_product_price_display', 10, 2 );
function change_product_price_display( $price, $product ) {
    if( ! in_array( $product->get_type(), array('subscription', 'subscription_variation', 'variable_subscription') ) )
        echo 'Rent: ' . $price . '/day';    
}

So you need to find out which are the product types for YITH WooCommerce Subscription and to set them in this code.

代码进入您的活动子主题(或活动主题)的 functions.php 文件。它应该有效。

您可以添加条件来检查当前产品是否订阅。例子.

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 );