• Resolved pierrehooker

    (@pierrehooker)


    Hello,

    I would like to change the price display to say “per month” but only if it is in one of several categories.

    The function and filter to change the price seems easy enough, I just cant figure out how to adapt it so it only applies to products of certain categories

    Can anybody help? Please don’t recommend plugins.

    
    function mmi_change_product_price_display( $price ) {
    
    	$price .= ' per month';
    	return $price;
    
    }
    
    	add_filter( 'woocommerce_get_price_html', 'mmi_change_product_price_display' );
    	add_filter( 'woocommerce_cart_item_price', 'mmi_change_product_price_display' );

    I tried just wrapping it like this:

    function mmi_change_product_price_display( $price ) {
    
    	if ( is_product() && has_term( 'oboe-rentals', 'product_cat' ) ) {
    		$price .= ' per month';
    		return $price;
    	}
    
    }
    
    add_filter( 'woocommerce_get_price_html', 'mmi_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'mmi_change_product_price_display' );

    But it didnt work, presumably because my php skills are deficient.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not 100% sure what you are going for but try…

    
    function mmi_change_product_price_display( $price ) {
    	if ( has_term( 'clothing', 'product_cat' ) ) {
    		$price .= ' per month';
    
    		return $price;
    	} else {
    		return $price;
    	}
    
    }
    

    When I tested this it added per month to anything that was listed in the clothing category and the price without if it wasn’t.

    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filter to change price display based on category?’ is closed to new replies.