Custom Text not showing on non-taxable products
-
Hi, I hope someone can help me?..
I have site with both taxable and non-taxable.
For taxable products, I use Woo’s builtin Price display suffix with “Excluding TAX”
But as Woo doesn’t have an a builtin way to display custom text on non-taxable product. I use the following code
But, I have just installed your plugin to show qty discounts, unfortunately, my custom snippet doesn’t work anymore on non-taxable product… the custom text appears briefly for 1 second, then disappears.
Can anyone help me edit the code to work with your plugin?
Thank you.add_filter( 'woocommerce_get_price_html', 'add_text_to_nontaxable_price', 10, 2 ); function add_text_to_nontaxable_price( $price_html, $product ) { if ( ! $product->is_taxable() ) { $custom_text = ' (Tax Exempt)'; // Replace with your desired text $price_html .= $custom_text; } return $price_html; }
-
Hello @rockso,
The plugin rebuilds the pricing once you update the product quantity on the product page, so your chances are overridden.The plugin considers the pricing suffix when it rebuilds the pricing, so I believe you better filter the pricing suffix instead of the whole price html.
Your code can look something like this:add_filter( 'woocommerce_get_price_suffix', 'add_text_to_nontaxable_price', 5, 2 ); function add_text_to_nontaxable_price( $price_html, $product ) { if ( ! $product->is_taxable() ) { return ' (Tax Exempt)'; // Add custom suffix } return $price_html; }
I hope this helps
Hi, thanks for the help, I tried your code and the following, but again, both only works when your plugin is deactivated. It’s got me puzzled…
add_filter( 'woocommerce_get_price_suffix', 'change_suffix_for_nontaxable', 5, 2 ); function change_suffix_for_nontaxable( $suffix, $product ) { if ( ! $product->is_taxable() ) { $new_suffix = ' (Tax Exempt)'; return $new_suffix; } return $suffix; }
SOLVED
add_filter( 'woocommerce_get_price_html', 'my_custom_price_text', 100 ); function my_custom_price_text( $price ) { global $product; // Check if the product is taxable using the 'taxable' property if ( ! $product->is_taxable() ) { $nonTaxableSuffix = ' (VAT Exempt)'; return $price . $nonTaxableSuffix; } else { // Return Woo's builtin suffix if aplicable return $price; } }
NOT SOLVED – of sorts,
Works with the classic basket and checkout, but causes a ‘critical error’ on the new block based basket and checkout.Hello @rockso,
Sounds odd. The snippet shouldn’t interact with the checkout in any ways.
Check the error log to get the full description of the error (WooCommerce -> Status -> Log -> Fatal errors)
Critical Uncaught Error: Call to a member function is_taxable() on null in /home/hyp/public_html/wp-content/themes/blocksy-child/functions.php:23 CONTEXT: {"error":{"type":1,"file":"\/home\/hyp\/public_html\/wp-content\/themes\/blocksy-child\/functions.php","line":23},"backtrace":["","#0 \/home\/hyp\/public_html\/wp-includes\/class-wp-hook.php(326): my_custom_price_text()","#1 \/home\/hyp\/public_html\/wp-includes\/plugin.php(205): WP_Hook->apply_filters()","#2 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/includes\/abstracts\/abstract-wc-product.php(1884): apply_filters()","#3 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/StoreApi\/Schemas\/V1\/ProductSchema.php(467): WC_Product->get_price_html()","#4 [internal function]: Automattic\\WooCommerce\\StoreApi\\Schemas\\V1\\ProductSchema->get_item_response()","#5 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/StoreApi\/Schemas\/V1\/AbstractSchema.php(285): array_map()","#6 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/StoreApi\/Schemas\/V1\/CartSchema.php(365): Automattic\\WooCommerce\\StoreApi\\Schemas\\V1\\AbstractSchema->get_item_responses_from_schema()","#7 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/StoreApi\/Routes\/V1\/Cart.php(50): Automattic\\WooCommerce\\StoreApi\\Schemas\\V1\\CartSchema->get_item_response()","#8 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/StoreApi\/Routes\/V1\/AbstractRoute.php(128): Automattic\\WooCommerce\\StoreApi\\Routes\\V1\\Cart->get_route_response()","#9 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/StoreApi\/Routes\/V1\/AbstractCartRoute.php(119): Automattic\\WooCommerce\\StoreApi\\Routes\\V1\\AbstractRoute->get_response_by_request_method()","#10 \/home\/hyp\/public_html\/wp-includes\/rest-api\/class-wp-rest-server.php(1230): Automattic\\WooCommerce\\StoreApi\\Routes\\V1\\AbstractCartRoute->get_response()","#11 \/home\/hyp\/public_html\/wp-includes\/rest-api\/class-wp-rest-server.php(1063): WP_REST_Server->respond_to_request()","#12 \/home\/hyp\/public_html\/wp-includes\/rest-api.php(555): WP_REST_Server->dispatch()","#13 \/home\/hyp\/public_html\/wp-includes\/rest-api.php(2922): rest_do_request()","#14 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/Blocks\/Domain\/Services\/Hydration.php(45): rest_preload_api_request()","#15 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/Blocks\/Assets\/AssetDataRegistry.php(327): Automattic\\WooCommerce\\Blocks\\Domain\\Services\\Hydration->get_rest_api_response_data()","#16 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/Blocks\/BlockTypes\/Checkout.php(365): Automattic\\WooCommerce\\Blocks\\Assets\\AssetDataRegistry->hydrate_api_request()","#17 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/Blocks\/BlockTypes\/AbstractBlock.php(415): Automattic\\WooCommerce\\Blocks\\BlockTypes\\Checkout->enqueue_data()","#18 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/Blocks\/BlockTypes\/Checkout.php(126): Automattic\\WooCommerce\\Blocks\\BlockTypes\\AbstractBlock->enqueue_assets()","#19 \/home\/hyp\/public_html\/wp-content\/plugins\/woocommerce\/src\/Blocks\/BlockTypes\/AbstractBlock.php(95): Automattic\\WooCommerce\\Blocks\\BlockTypes\\Checkout->enqueue_assets()","#20 \/home\/hyp\/public_html\/wp-includes\/class-wp-block.php(463): Automattic\\WooCommerce\\Blocks\\BlockTypes\\AbstractBlock->render_callback()","#21 \/home\/hyp\/public_html\/wp-includes\/blocks.php(1705): WP_Block->render()","#22 \/home\/hyp\/public_html\/wp-includes\/blocks.php(1743): render_block()","#23 \/home\/hyp\/public_html\/wp-includes\/class-wp-hook.php(324): do_blocks()","#24 \/home\/hyp\/public_html\/wp-includes\/plugin.php(205): WP_Hook->apply_filters()","#25 \/home\/hyp\/public_html\/wp-includes\/post-template.php(256): apply_filters()","#26 \/home\/hyp\/public_html\/wp-content\/themes\/blocksy\/inc\/components\/single\/content-helpers.php(225): the_content()","#27 \/home\/hyp\/public_html\/wp-content\/themes\/blocksy\/template-parts\/single.php(74): blocksy_single_content()","#28 \/home\/hyp\/public_html\/wp-includes\/template.php(812): require('\/home\/hyp')","#29 \/home\/hyp\/public_html\/wp-includes\/template.php(745): load_template()","#30 \/home\/hyp\/public_html\/wp-includes\/general-template.php(206): locate_template()","#31 \/home\/hyp\/public_html\/wp-content\/themes\/blocksy\/single.php(17): get_template_part()","#32 \/home\/hyp\/public_html\/wp-includes\/template.php(812): require('\/home\/hyp')","#33 \/home\/hyp\/public_html\/wp-includes\/template.php(745): load_template()","#34 \/home\/hyp\/public_html\/wp-includes\/general-template.php(206): locate_template()","#35 \/home\/hyp\/public_html\/wp-content\/themes\/blocksy\/page.php(15): get_template_part()","#36 \/home\/hyp\/public_html\/wp-includes\/template-loader.php(106): include('\/home\/hyp')","#37 \/home\/hyp\/public_html\/wp-blog-header.php(19): require_once('\/home\/hyp')","#38 \/home\/hyp\/public_html\/index.php(17): require('\/home\/hyp')","#39 {main}","thrown"]}
I have no idea what any of it means… I have tried switching to several other classic themes, and it’s still the same… breaks the blocks based checkout & basket… works on classic basket/checkout no problem
Try this patched code:
add_filter( 'woocommerce_get_price_html', 'my_custom_price_text', 100, 2 ); function my_custom_price_text( $price, $product ) { // Check if the product is taxable using the 'taxable' property if ( $product && ! $product->is_taxable() ) { return $price . ' (VAT Exempt)'; } // Return default price return $price; }
Wow.. that works!
with your plugin activated and the block basket/checkout work too…
I have a lot to learn, thank you
- You must be logged in to reply to this topic.