Skip to content

Instantly share code, notes, and snippets.

@eduwass
Created May 3, 2016 19:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save eduwass/498d6f695983b7b4da5407957e2ecf5a to your computer and use it in GitHub Desktop.
Save eduwass/498d6f695983b7b4da5407957e2ecf5a to your computer and use it in GitHub Desktop.
Hide WooCommerce subscriptions text (in product and cart pages) from products with a set Price but no Subscription Fee
// Define which products we should hide the subscription text for
function should_hide_subscription_text($product){
$is_subscription = (get_class($product) == 'WC_Product_Variable_Subscription');
$has_price = (intval($product->subscription_price)>0);
$has_fee = (intval($product->subscription_sign_up_fee)>0);
if($is_subscription && $has_price && !$has_fee){
return true;
} else {
return false;
}
}
function custom_subscriptions_product_price_string( $pricestring, $product, $include ) {
global $product;
if ( should_hide_subscription_text($product) ) {
$pricestring = str_replace( '<span class="amount">&#36;0.00</span>', '<span class="amount">&#36;'.$product->subscription_price.'</span>', $pricestring );
$pricestring = str_replace( 'for 1 month', '', $pricestring );
$pricestring = str_replace( 'and a', '', $pricestring );
$pricestring = str_replace( 'sign-up fee', '', $pricestring );
$pricestring = str_replace( 'month for', '', $pricestring );
$pricestring = str_replace( ' / ', '', $pricestring );
$pricestring = str_replace( 'Free!', '', $pricestring );
$pricestring = str_replace( ' months ', ' months for ', $pricestring );
}
return $pricestring;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'custom_subscriptions_product_price_string' );
// Remove bad subscription stuff from cart
function wc_subscriptions_custom_price_string( $pricestring ) {
$pricestring = str_replace( '<span class="amount">&#36;0.00</span>', '', $pricestring );
$pricestring = str_replace( '/ month for ', '', $pricestring );
$pricestring = str_replace( ' and a ', ' for ', $pricestring );
$pricestring = str_replace( ' sign-up fee', '', $pricestring );
return $pricestring;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment