Change "Place order" button text to include Order Total - woocommerce

The idea here is to include the price (example: $40) with the button text, making it like this: "Place Order & Pay $40".
That is the expected outcome but the code I'm using (with or without wc_price()) only generates a span tag on the button.
I know that I can just add the currency symbol myself into the button text, but the idea is to make it global.
My code so far:
add_filter('woocommerce_order_button_text', 'place_order_button_with_order_total');
function place_order_button_with_order_total(){
$order_value = wc_price(WC()->cart->total);
return __('Place Order & Pay '.$order_value., 'woocommerce');
}
How do I change the code to include the price in the button text?

You can remove the HTML markup that you get when using wc_price (or in my example get_total()) with the strip_tags() function.
Also you shouldn't add variables to translatable strings. If you want to make a combination of a translatable string and a variable value you can make use of the sprintf() function.
The following code should do the trick:
add_filter('woocommerce_order_button_text', 'place_order_button_with_order_total');
function place_order_button_with_order_total(){
return sprintf( '%s %s', __( 'Place Order & Pay', 'custom-order-button' ), strip_tags( WC()->cart->get_total() ) );
}

Related

WordPress get_calendar() filter to Alter HTML Output

By default, WordPress ships with a Calendar widget that you can use to display a calendar with the days that have posts. I would like to filter the HTML output of this widget to customize its look. Specifically, the widget displays links to previous and next months at the bottom of the calendar in the following format:
« Jan Feb »
This is the HTML I would like to change.
What I Have Tried
If you look at wp-includes/general-template.php file in WordPress, there is a function called get_calendar() which contains the HTML I wish to change. It also has a filter hook to filter the HTML output at the bottom of the function.
If I use the following code, I can customize the output to remove the « and » and replace them with icons from FontAwesome. This works without issue.
function custom_calendar_output( $calendar_output ) {
$search = array( '«', '»' );
$replace = array( '<i class="far fa-arrow-alt-circle-left"></i>', '<i class="far fa-arrow-alt-circle-right"></i>' );
$cust_calendar_output = str_replace( $search, $replace, $calendar_output );
return $cust_calendar_output;
}
add_filter( 'get_calendar', 'custom_calendar_output' );
What I'm Having Trouble With
I would also like to remove the month text as well as the default arrow. So instead of having it read as: « Jan, It would just display the icon from FontAwesome. The month text changes (e.g. Jan, Feb, Mar) as the user cycles through the past months.
How can I remove both the default arrow and the month text to replace it with a custom icon?
I have read I may need to use preg_replace with regex but I can't seem to get this to work. I have also read that using regex uses a lot of overhead. Is there a cleaner way to do this?

woocommerce add custom tooltip in order view

I'd like to add a tooltip like this in order status, on mouse over:
I tried to use wc_help_tip as follows:
$help_tip = 'Ti do un suggerimento: studia!';
echo wc_help_tip(__($help_tip, 'mypluginname'));
As a result I get a similar behaviour: I display a question mark icon and on mouse over my custom text. I'd like to display my text in the same style as in WooCommerce order status column. Any suggestions?
I solved by myself using data-tip property as follows:
echo '<mark style="margin-bottom:2px;" class="order-status status-on-hold tips" data-tip="' . __('Document not created in MyPluginName', 'mypluginname') . '"><span>'. __('Not saved ', 'mypluginname') . '</span></mark><br />';

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Archive Description split using <!-- more -->

I've output my archive description as per below. However I would like to display the first part before the tag in one place and the rest excluding the first part elsewhere in other tag. Is there a way to split this to output the two different data? It could be split just using the first paragraph instead.
the_archive_description( '<div class="taxonomy-description">', '</div>' );
Try using get_the_archive_description()
instead. This returns the description instead of displaying it, which will allow you to manipulate it however you like using your own code before displaying (or not) parts of it. Kind of like this:
$myDescription = get_the_archive_description();
$sections = explode('<!-- more -->', $myDescription);
echo __( $sections[0], 'default' ); //Put this wherever you want to display the first part
echo __( $sections[1], 'default' ); //Put this wherever you want to display the rest

Woocommerce override grouped product title

I've seen this on other themes but can't work out how it has been done. In my grouped product list, the title lists as Parent product title --> Child product title. I need it only to list the Child product title.
I can see the code to alter is:
' . $child_product['product']->post->post_title . '
Either the post_title needs to overridden, or the code altered to...what?
(Although this is old, it's still a common question with a prominent google ranking)
Woocommerce defines a filter, woocommerce_product_title, which allows you to pass a product's title through a function that modifies the way it will display.
Add a filter, probably in your theme's functions.php
add_filter('woocommerce_product_title', 'clean_up_title');
This is the function I'm currently using to accomplish this, no promises that it's the best way possible:
function clean_up_title($title){
// check to see if there is an html arrow in the title
if (strpos($title, '→')){
$separator = '→';
}
// otherwise assume it's the character
else {
$separator = '→';
}
// split the title into multiple parts at the arrows
$prog_array = explode($separator, $title);
// get the last part, the actual product name
$prog_name = end($prog_array);
// slice off any leading or trailing whitespace
return trim($prog_name);
}
This would be a bit cleaner to do. Just 'return' the product title rather than 'edit' it.
function clean_product_title($title, $product) {
return $product->post->post_title;;
}
add_filter('woocommerce_product_title', 'clean_product_title', 10, 2);

Resources