• Resolved lennertdaniels

    (@lennertdaniels)


    Hi,

    it would be logical to have some HTML markup on the prices that are shown as part of a select, radio or checkbox label. Currently, the price is just appended to the field name, in the same span tag. Nesting the price inside of its own span tag yields opportunities for separate CSS styling.

    Printscreen:
    https://prnt.sc/pw5blk

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @lennertdaniels,

    Yes your point is worthy to note. We will add this feature in coming version ??

    Or you could override the ppom_option_label as I have with this function and filter. With this you can change the format of the price and add that space before the parentheses that should be there.

    This function works to change a price’s format up to $999,999.99 or $999999.99 from the PPOM formatted option(+$x.xx) to option [ +$x.xx ].

    This works for checkboxes, radio buttons and select dropdowns.

    I changed mine up a bit to add the span tag you are asking for and also to not show the span for select field types.

    
    /**
     * Change PPOM option label price format
     * @param type $option_label
     * @return type
     */
    function custom_ppom_option_label( $option_label, $meta ) {
    	$price_replacement = ' <span>[ +$2$3.$7 ]</span>';
    	if( isset($meta['type']) ) {
    		if ($meta['type']==="select") {
    			$price_replacement = ' [ +$2$3.$7 ]';
    		}
    	}
    	return preg_replace(
    		"/(\()(".get_woocommerce_currency_symbol().")((\d{1,3},\d{1,3})|(\d{1,6}))(\.)(\d{2})(\))/", # pattern
    		$price_replacement, # price replacement
    		$option_label # what PPOM produces
    	);
    }
    add_filter( 'ppom_option_label', 'custom_ppom_option_label' );

    Just add this to your theme’s functions.php and modify to your needs. Enjoy!

    Thread Starter lennertdaniels

    (@lennertdaniels)

    Thanks @brozra! Super helpful!

    I never knew you could hook into plugin functions as well.

    You’re welcome. It’s the same principle as hooking into WP plugins. It also depends on the developer of the plugin if they create custom actions within their plugin. Luckily, in this case, it was available.

    Thread Starter lennertdaniels

    (@lennertdaniels)

    @brozra
    Implemented, works like a charm.

    Only thing I had to change was the match pattern, which assumed your localisation is using a dot as decimal indicator, but in the EU, that’s often a comma.
    And I gave the $meta parameter a default value, since WordPress complained that the function was requiring too many arguments.

    Neat.

    $meta already has a value that gets generated in the plugin’s inc/functions.php file. You can keep it with your set value but for proper coding the priority and variable count for the filter needs to be added. Change the add_filter line to this:

    add_filter( 'ppom_option_label', 'custom_ppom_option_label', 10, 2 );

    That should resolve the too many arguments. The first number (10) is the priority and the second number (2) is the number of arguments the function takes.

    As far as the currency decimal, I didn’t think to add wc_get_price_decimals() to check for the decimals separator. I was going for a simple fix for my client’s website and not delving too deep into the global semantics of its usage or look too closely at the full usage of the ppom_option_label filter. If I spend a bit more time on it I’m sure I can make a better function.

    Here’s a better solution that should be a universal function as it doesn’t do any manipulation of the price at all. It just strips it out of the option label and reprints it. The price was already formatted by both WooCommerce and PPOM so the currency symbol, thousands separator and decimals separator are untouched.

    I have verified that this works with € and commas as a decimals separator. Should work equally well with currencies that show on the right side of the price.

    Of course, if you prefer the parentheses() then you can use those and if you don’t like the plus(+) sign you can remove that. Just be sure to adjust the $price_replacement on both lines where it is set to change to your preference.

    If @nmedia adds a span to the option label in the future then this code should be removed from your theme’s functions.php file.

    
    /**
     * Change PPOM option label price format
     * @param type $option_label
     * @param type $meta
     * @return type
     */
    function custom_ppom_option_label( $option_label, $meta ) {
    	$price_replacement = ""; 
    	$currencySymbol = get_woocommerce_currency_symbol();
    
    	if ( strpos( $option_label, $currencySymbol ) > -1 ) {
    		$priceStart = strpos( $option_label, "(" );
    		$priceLength = strlen( $option_label ) - $priceStart - 2;
    		$price = substr( $option_label, $priceStart+1, $priceLength );
    
    		if (strlen($price)>0) {
    			$price_replacement = " <span>[ +{$price} ]</span>";
    			if( isset($meta['type']) && $meta['type']==="select") { $price_replacement = " [ +{$price} ]"; }
    			$option_label = str_replace("(".$price.")",$price_replacement,$option_label);
    		}
    	}
    	return $option_label;
    }
    add_filter( 'ppom_option_label', 'custom_ppom_option_label', 10, 2 );
    

    Hi @brozra,

    we have updated in PPOM for following function for:
    1. add new tag for price
    2. add class for above tag

    new function is:

    function ppom_generate_option_label( $option, $price, $meta) {
    	
    	$the_option = isset($option['option']) ? $option['option'] : '';
    	if( isset($meta['type']) && $meta['type'] == 'imageselect' ) {
    		$the_option = isset($option['title']) ? $option['title'] : '';
    	}
    	$option_label = !empty($option['label']) ? $option['label'] : $the_option;
    	$option_label = stripcslashes($option_label);
    	
    	if( !empty($price) ) {
    		$price = apply_filters('ppom_option_price', $price);
    		$price = strip_tags(ppom_price($price));
    		
    		$price_replacement = " <span class='ppom-option-label-price'>[+{$price}]</span>";
    		if( isset($meta['type']) && $meta['type']==="select") { $price_replacement = " [+{$price}]"; }
    			
    		$option_label = "{$option_label}{$price_replacement}";
    	}
    	
    	return apply_filters('ppom_option_label', $option_label, $option, $meta, $price);
    }

    Looks good. Although I don’t think everyone will like the brackets [] but they can always reference this post to override. ??

    Will this be an update included with 18.8?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Price HTML Markup’ is closed to new replies.