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

时间:2019-05-08 02:16:10

标签: php wordpress woocommerce cart subscription

在WooCommerce中,我使用以下代码在显示的产品价格(“租金:”和“ /天”)周围添加一些文本,例如:

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 /月/天。

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

2 个答案:

答案 0 :(得分:1)

以下内容将不包括WooCommerce订阅产品…

darwin
  

因此,您需要找出哪些是YITH WooCommerce Subscription的产品类型,并在此代码中进行设置。

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。

答案 1 :(得分:1)

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

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