{"id":1286,"date":"2022-11-03T10:57:31","date_gmt":"2022-11-03T10:57:31","guid":{"rendered":"https:\/\/mklasen.com\/?p=1286"},"modified":"2022-11-03T11:04:33","modified_gmt":"2022-11-03T11:04:33","slug":"rename-coupon-code-text-to-discount-code-in-woocommerce","status":"publish","type":"post","link":"https:\/\/mklasen.com\/rename-coupon-code-text-to-discount-code-in-woocommerce\/","title":{"rendered":"Rename Coupon code text to Discount code in Woocommerce"},"content":{"rendered":"\n
Another day, another snippet! This snippet allows you to replace all occurrences of the coupon word to something that you like better, like “discount code”.<\/p>\n\n\n\n
This code is highly inspired from the code shared in WPBeaches’ blog post<\/a>. It’s customized for usage in classes, and optimized as some occurrences were not replaced and the “Click here to enter your code” was broken. I shared these optimizations here as I was not able to comment on the blog post. <\/p>\n\n\n\n And these are the attached functions, mind that this code belongs in a class. A full code sample is shown in the bottom of this post.<\/p>\n\n\n\n Another day, another snippet! This snippet allows you to replace all occurrences of the coupon word to something that you like better, like “discount code”. This code is highly inspired from the code shared in WPBeaches’ blog post. It’s customized for usage in classes, and optimized as some occurrences were not replaced and the “Click […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[1],"tags":[],"acf":[],"yoast_head":"\nThe hooks, and filters we’re using<\/h2>\n\n\n\n
add_filter( 'gettext', array( $this, 'rename_coupon_field_on_cart' ), 10, 3 );\nadd_filter( 'woocommerce_coupon_error', array( $this, 'rename_coupon_label' ), 10, 3 );\nadd_filter( 'woocommerce_coupon_message', array( $this, 'rename_coupon_label' ), 10, 3 );\nadd_filter( 'woocommerce_cart_totals_coupon_label', array( $this, 'rename_coupon_label' ), 10, 1 );\nadd_filter( 'woocommerce_checkout_coupon_message', array( $this, 'rename_coupon_message_on_checkout' ) );<\/code><\/pre>\n\n\n\n
The functions<\/h2>\n\n\n\n
public function rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {\n\tif ( is_admin() || 'woocommerce' !== $text_domain ) {\n\t\treturn $translated_text;\n\t}\n\tif ( 'Coupon:' === $text ) {\n\t\t$translated_text = 'Discount Code:';\n\t}\n\n\tif ( 'Coupon has been removed.' === $text ) {\n\t\t$translated_text = 'Discount code has been removed.';\n\t}\n\n\tif ( 'Apply coupon' === $text ) {\n\t\t$translated_text = 'Apply';\n\t}\n\n\tif ( 'Coupon code' === $text ) {\n\t\t$translated_text = 'Discount Code';\n\n\t}\n\n\tif ( 'If you have a coupon code, please apply it below.' === $text ) {\n\t\t$translated_text = 'If you have a discount code, please apply it below.';\n\t}\n\n\treturn $translated_text;\n}\n\npublic function rename_coupon_message_on_checkout( $text ) {\n\treturn 'Have a discount code?' . ' ' . __( 'Click here to enter your code', 'woocommerce' ) . '<\/a>';\n}\n\n\npublic function rename_coupon_label( $err, $err_code = null, $something = null ) {\n\t$err = str_ireplace( 'Coupon', 'Discount Code ', $err );\n\treturn $err;\n}<\/code><\/pre>\n\n\n\n
Full code sample<\/h2>\n\n\n\n
init();\n\t}\n\n\tpublic function init() {\n\n\t\tadd_filter( 'gettext', array( $this, 'rename_coupon_field_on_cart' ), 10, 3 );\n\t\tadd_filter( 'woocommerce_coupon_error', array( $this, 'rename_coupon_label' ), 10, 3 );\n\t\tadd_filter( 'woocommerce_coupon_message', array( $this, 'rename_coupon_label' ), 10, 3 );\n\t\tadd_filter( 'woocommerce_cart_totals_coupon_label', array( $this, 'rename_coupon_label' ), 10, 1 );\n\t\tadd_filter( 'woocommerce_checkout_coupon_message', array( $this, 'rename_coupon_message_on_checkout' ) );\n\t}\n\n\tpublic function rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {\n\t\tif ( is_admin() || 'woocommerce' !== $text_domain ) {\n\t\t\treturn $translated_text;\n\t\t}\n\t\tif ( 'Coupon:' === $text ) {\n\t\t\t$translated_text = 'Discount Code:';\n\t\t}\n\n\t\tif ( 'Coupon has been removed.' === $text ) {\n\t\t\t$translated_text = 'Discount code has been removed.';\n\t\t}\n\n\t\tif ( 'Apply coupon' === $text ) {\n\t\t\t$translated_text = 'Apply';\n\t\t}\n\n\t\tif ( 'Coupon code' === $text ) {\n\t\t\t$translated_text = 'Discount Code';\n\n\t\t}\n\n\t\tif ( 'If you have a coupon code, please apply it below.' === $text ) {\n\t\t\t$translated_text = 'If you have a discount code, please apply it below.';\n\t\t}\n\n\t\treturn $translated_text;\n\t}\n\n\tpublic function rename_coupon_message_on_checkout( $text ) {\n\t\treturn 'Have a discount code?' . ' ' . __( 'Click here to enter your code', 'woocommerce' ) . '<\/a>';\n\t}\n\n\n\tpublic function rename_coupon_label( $err, $err_code = null, $something = null ) {\n\t\t$err = str_ireplace( 'Coupon', 'Discount Code ', $err );\n\t\treturn $err;\n\t}\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"