2

In WooCommerce I use the following code to add some text around the displayed product price ("Rent:" and "/day") like:

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

I also use the plugin "YITH WooCommerce Subscription" and there is a problem with my code. Now in the subscription packages I have one of this price displays:

  • Rent: $20/7 days/day
  • Rent: $80/month/day.

How to exclude subscription products or category of subscriptions from my codeTo avoid this problems?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Dmitry
  • 119
  • 1
  • 9
  • 38

2 Answers2

1

The following will exclude WooCommerce subscription products…

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.

Code goes in functions.php file of your active child theme (or active theme). It should works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for the answer. But unlike the other answer, nothing changes here. – Dmitry May 08 '19 at 08:29
  • @Dmitry I have never used your plugin, so I don't know what are the product types for it… In my answer I clearly mention that you need to find out them. My answer Works for official WooCommerce Subscriptions plugin… – LoicTheAztec May 08 '19 at 08:36
  • Yes, I understand that. But anyway, thanks for the help. The second answer suits me better. – Dmitry May 08 '19 at 08:44
1

You can add condition to check if current product is subscription or not. Example.

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

And for cart price:

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 );
Nilambar Sharma
  • 1,732
  • 6
  • 18
  • 23