Custom stylization of decimals breaks default WooCommerce calculations - php

I'm using Stylization of decimals as uppercase in WooCommerce frontend only answer code from my previous question.
Now I have a particular issue: I've noticed that my code is breaking the default WooCommerce decimals calculation rules, is there anything you can see in my code that would be breaking it?
As an example, with this code, a price entered before tax in the backend as 7,39669€ (which is the result of 8.95€/1.21) is wrongly being shown as 8.94€ instead of 8.95€. When I disable this custom code, WooCommerce rules work fine again.
I'd like to ensure that the code "calls" the default WooCommerce calculations, so that the code only changes the display of the decimals and that's all.

To avoid this problem, here I am using a different way to separate decimals from the price:
add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
// Not on backend
if ( ! is_admin() ) {
$price_data = explode($decimal_separator, $formatted_price);
return $price_data[0] . '<sup>' . $price_data[1] . '</sup>';
}
return $formatted_price;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related

Override WooCommerce global template quantity-input.php for better quantity input experience on mobile

I want WooCommerce to better handle Quantity field input when using mobile devices. Instead of showing a full keyboard for user to input quantity field, I want mobile browser to show a numeric keypad instead.
I can achieve that by modifying the quantity-input.php from
pattern="<?php echo esc_attr( $pattern ); ?>"
inputmode="<?php echo esc_attr( $inputmode ); ?>"
to
pattern="[0-9]*"
inputmode="numeric"
in
wp-content/plugins/woocommerce/templates/global/quantity-input.php
But obviously modifying the php files of WooCommerce directly is a terrible idea, so I need some guidance on the best practice of such modification.
I have been using a plugin called Code Snippet in Wordpress and it works well. How can I put by code to my site using that?
I have found a post that mentions template files overriding in WooCommerce by using filter here but I am not familiar with that as I am new to Wordpress / WooCommerce
It seems to be the way to go is to use the filterwc_get_template_part and / or woocommerce_locate_template but I don't know how to do that.
Any suggestion is welcome.
You can simply use woocommerce_quantity_input_args dedicated filter hook like:
add_filter( 'woocommerce_quantity_input_args', 'filter_quantity_input_args_callback', 10, 2 );
function filter_quantity_input_args_callback( $args, $product ) {
$args['pattern'] = '[0-9]*';
$args['inputmode'] = 'numeric';
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Or this instead two filter hooks instead (that will have the same effect):
add_filter( 'woocommerce_quantity_input_pattern', 'filter_quantity_input_pattern_callback', 10, 1 );
function filter_quantity_input_pattern_callback( $input_pattern ) {
return '[0-9]*';
}
add_filter( 'woocommerce_quantity_input_inputmode', 'filter_quantity_input_inputmode_callback', 10, 1 );
function filter_quantity_input_inputmode_callback( $inputmode ) {
return 'numeric';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Customizing totals lines in Woocommerce customer email notifications

My woocommerce sends out an as it is suppose to.
How ever the tax fields shows up with what seems to be an unclosed tag.
I have grepped thru the entire woocommerce code, but I cant find where the tags are generated.
this is how my tax field looks in the email.
Total: DKK 0.00 <small class="includes_tax"
This can only be the result of a customization that you have made on order totals, or that your theme or a plugin is making. By default there is no such behavior in Woocommerce. It seems in your case due to a plugin (or some customizations) that displays the currency symbol as a Code.
Now order totals rows in Woocommerce email notifications are generated using the WC_Order method get_order_item_totals()
Then you can make changes in it using the following code:
add_filter( 'woocommerce_get_order_item_totals', 'customize_order_line_totals', 1000, 3 );
function customize_order_line_totals( $total_rows, $order, $tax_display ){
// Only on emails notifications
if( ! is_wc_endpoint_url() || ! is_admin() ) {
// Remove any other html tags from gran total value
$total_rows['order_total']['value'] = strip_tags( wc_price( $order->get_total() ) );
}
return $total_rows;
}
Code goes in function.php file of your active child theme (or active theme). It should solve your problem.
But the best way should be to find out the guilty, instead of patching something wrong done by some customization somewhere.

Custom query string in Woocommerce external / affiliate product URL

I wonder if you guys can help. I do not know php very well.
I would like to add the same extra string on every external product url. The extra string would be like "?prodid=12345" so I do not want to overide the url just add to it. Would this be possible? TIA
This can be done easily with the following hooked function:
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_product_add_to_cart_url', 20, 2 );
function custom_product_add_to_cart_url( $add_to_cart_url, $product ){
if( $product->is_type('external') )
$add_to_cart_url .= '?prodid=' . $product->get_id();
return $add_to_cart_url;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
You can change in the code $product->get_id(); by a static value or any other dynamic value of your choice.

Remove subtotal and Shipping calculator block in Woocommerce cart totals section

In woocommerce, I am using the following to remove the shipping block in cart totals section bloc:
function disable_shipping_calc_on_cart( $show_shipping ) {
if( is_cart() ) {
return false;
}
return $show_shipping;
}
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_calc_on_cart', 99 );
But in that cart totals section bloc there are some other things like the cart Subtotal and Total.
I just need to keep only Total
Any help is welcome.
You seem to keep only the total on cart totals section. For that the only way is overriding woocommerce templates.
There is 2 steps:
You should read Template structure & Overriding templates via a theme to understand how you can override woocommerce templates via your active child theme (or active theme).
So you will have to create in you active theme folder (if it doesn't exist yet) a folder named "woocommerce" and a sub-sub folder "cart"…
You will have to copy from the wp-content/plugins/woocommerce/templates/cart/cart-totals.php to your active theme woocommerce/cart/cart-totals.php (if it not exist yet).
Editing cart-totals.phptemplate and removing code:
Since WooCommerce version 2.3.6 and up: You will have to remove the code from line 32 to 59 (You will keep only fees and taxes if they are any)
If you want to keep only the total amount you will remove the code from line 32 to line 87
Then save.
You will not need your function code anymore
Now your cart totals will be something like this:
add_filter( 'woocommerce_get_order_item_totals', 'adjust_woocommerce_get_order_item_totals' );
function adjust_woocommerce_get_order_item_totals( $totals ) {
unset($totals['cart_subtotal'] );
return $totals;
}
Is this what you are looking for? Basically you asked to remove the subtotal.

How to remove attributes from product title in woocomerce order view page? [duplicate]

Ever since we upgraded to Woocommerce version 3 our order confirmations are showing huge titles that include the variation detail. I don't like how it looks and it breaks some important functionalities in some custom-made plugins.
Reference: Order Name Showing Variations since update to WC version 3
There is a filter that can be used to disable this data displaying in the title called woocommerce_product_variation_title_include_attribute_name from what I understand. But I have no idea where to apply the filter.
Is there a quick way to apply the filter to change it back to display as it did before?
This filter should work returning a false value for $should_include_attributes first argument in woocommerce_product_variation_title_include_attributes filter hook this way:
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
function custom_product_variation_title($should_include_attributes, $product){
$should_include_attributes = false;
return $should_include_attributes;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
It should just work as you expect.
Update: The shorter way is:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
just works too.
A quick gotcha if you're using this filter to remove attributes from e-mail items. It appears that once an item has been written to an order the properties of it will not change.
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
As #freemason_17 pointed out, #LoicTheAztec's answer could potentially hide those details at other places as well so just to be sure I added a condition that would limit this to the cart page alone:
function custom_product_variation_title($should_include_attributes, $product){
if(is_cart()) {
$should_include_attributes = false;
return $should_include_attributes;
}
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
I use this:
/* -------------------- Remove variation names from title ------------------- */
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

Categories