Can't Change WooCommerce Coupon Label In Cart Totals - wordpress

This has proven to be a challenge I can't figure out. I handled the renaming of "Coupon" to "Promo" pretty much everywhere else. However, the renaming of the WC LABEL that calls the name of the coupon and the LABEL is a lot harder.
Here's a screenshot of what I'm talking about exactly:
I've pretty much renamed all the uses of "coupon" to "promo" site-wide. This one is making my hair fall out. Has anyone ever successfully changed this? What am I missing?
Rename "Coupon" to "Promo" code below
// rename the coupon field on the cart page
function woocommerce_rename_coupon_field_on_cart( $translated_text, $text, $text_domain ) {
// bail if not modifying frontend woocommerce text
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon code:' === $text ) {
$translated_text = 'Promo Code:';
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_cart', 10, 3 );
// rename the "Have a Coupon?" message on the checkout page
function woocommerce_rename_coupon_message_on_checkout() {
return 'Have a Customer Code?' . ' <a href="#" class="showcoupon">' . __(
'Click here to enter your code', 'woocommerce' ) . '</a>';
}
add_filter( 'woocommerce_checkout_coupon_message',
'woocommerce_rename_coupon_message_on_checkout' );
// rename the coupon field on the checkout page
function woocommerce_rename_coupon_field_on_checkout( $translated_text, $text, $text_domain ) {
// bail if not modifying frontend woocommerce text
if ( is_admin() || 'woocommerce' !== $text_domain ) {
return $translated_text;
}
if ( 'Coupon code' === $text ) {
$translated_text = 'Promo Code';
} elseif ( 'Apply coupon' === $text ) {
$translated_text = 'Apply Code';
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_checkout', 10, 3 );
WC Coupon Notices/Message code below
function woocommerce_coupon_message( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Coupon' :
$translated_text = __( 'Promo', 'woocommerce' );
break;
case 'promo' :
$translated_text = __( 'promo', 'woocommerce' );
break;
case 'Please enter a coupon code.' :
$translated_text = __( 'Please enter a Promo code.', 'woocommerce' );
break;
case 'Coupon code already applied!' :
$translated_text = __( 'Promo code already applied!', 'woocommerce' );
break;
case 'Coupon has been removed.' :
$translated_text = __( 'Promo has been removed', 'woocommerce' );
break;
case 'Coupon code applied successfully.' :
$translated_text = __( 'Promo code applied successfully.', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'woocommerce_coupon_message', 20, 3 );

// Hide 'Coupon: CODE' in cart totals and instead return generic 'Promo Code Applied'
add_filter( 'woocommerce_cart_totals_coupon_label', 'woocommerce_change_coupon_label' );
function woocommerce_change_coupon_label() {
echo 'Promo Code Applied';
}

add_filter( 'woocommerce_cart_totals_coupon_label', 'woocommerce_change_coupon_label' );
function woocommerce_change_coupon_label($sprintf, $coupon) {
return 'Promo ' . $coupon->code;
}

I found a solutions for all needs.
Works with WooCommerce v7.
function custom_cart_totals_coupon_label($sprintf, $coupon)
{
$coupon_code = $coupon->get_code();
$sprintf = '<span class="discount_label">'.__('Coupon', 'veganwebagency').':</span> <span class="discount_code">'.$coupon_code.'</span>';
return $sprintf;
}
add_filter('woocommerce_cart_totals_coupon_label', 'custom_cart_totals_coupon_label', 10, 2);

Related

WooCommerce restrict coupon to user role error message

I'm using WooCommerce to restrict a coupon to the user role "gold-member" which works fine but I'm trying to change the message displayed when an invalid user tries to use the coupon:
Sorry, coupon code "goldmember10" is not valid with your customer role.
My code
add_filter( 'woocommerce_coupon_error','coupon_error_message_change',10,3 );
function coupon_error_message_change($err, $err_code, $parm ) {
switch ( $err_code ) {
case 100:
$err = sprintf( __( 'Sorry, coupon code "%s" is not valid.', 'woocommerce' ), $parm->get_code() );
break;
}
return $err;
}
$err_code matches 100 per class-wc-coupon.php but the message does not get updated.
This should suffice:
function filter_woocommerce_coupon_error( $err, $err_code, $coupon ) {
// Compare
if ( intval( $err_code ) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED ) {
// Message
$err = sprintf( __( 'Sorry, coupon code "%s" is not valid.', 'woocommerce' ), $coupon->get_code() );
} else {
$err = 'DEBUG INFORMATION, error code number = ' . intval( $err_code );
}
return $err;
}
add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );

Change wording on Woocommerce Checkout page

Thanks to help here:
Change Billing details text to Shipping details
I could change Billing details text on Woocommerce Checkout page by adding this to functions.php in Child theme:
function wc_billing_field_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );
What are the codes to change these 3 additional texts with different wording:
Additional information, PRODUCT & QUANTITY
(see screenshot):
Woocommerce Checkout Page
It would be better to override the checkout/form-checkout.php and checkout/review-order.php
Or You can use gettext filter .
This filter hook is applied to the translated text by the
internationalization functions (__(), _e(), etc.)
function th_wc_order_review_strings( $translated_text, $text, $domain ) {
if(is_checkout()){
switch ($translated_text) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
case 'Additional information':
$translated_text = __('New Field Name', 'woocommerce');
break;
case 'Your order':
$translated_text = __('My Order', 'woocommerce');
break;
case 'Product':
$translated_text = __('Your Product', 'woocommerce');
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'th_wc_order_review_strings', 20, 3 );
Try changing the above code to your requirements.

Adding New Stock Status to Products in Woocommerce [duplicate]

I am using the following code to add new stock statuses in WooCommerce 4+
The new statuses are:
Preorder
Contact us
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'Available', 'woocommerce' ), //changed the name
'outofstock' => __( 'Sold out', 'woocommerce' ), //changed the name
'onbackorder' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), //changed the name
'contact' => __( 'Contact us for Availability', 'woocommerce' ), //added new one
'preorder' => __( 'On Preorder: Pending Distributor release', 'woocommerce' ), //added new one
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
function woo_add_custom_general_fields_save_two( $post_id ){
// Select
$woocommerce_select = $_POST['_stock_status'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_stock_status', esc_attr( $woocommerce_select ) );
else
update_post_meta( $post_id, '_stock_status', '' );
}
function woocommerce_get_custom_availability( $data, $product ) {
switch( $product->stock_status ) {
case 'instock':
$data = array( 'availability' => __( 'Available', 'woocommerce' ), 'class' => 'in-stock' ); //changed name
break;
case 'outofstock':
$data = array( 'availability' => __( 'Sold Out', 'woocommerce' ), 'class' => 'out-of-stock' ); //changed name
break;
case 'onbackorder':
$data = array( 'availability' => __( 'Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'onbackorder' ); //changed name
break;
case 'contact':
$data = array( 'availability' => __( 'Contact us for Availability', 'woocommerce' ), 'class' => 'contact' ); //added new one
break;
case 'preorder':
$data = array( 'availability' => __( 'On Preorder : Pending Distributor release', 'woocommerce' ), 'class' => 'preorder' ); //added new one
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 4);
Works:
Backend: The new status is added in the dropdown menu, I can select the status I want
Does not work:
Front end: on single product page is not showing the correct status
Backend: Display the new status on the admin products list table
Someone who can assist me with this?
Last update: 04/22 - Tested in WordPress 5.9.2 & WooCommerce 6.3.1
Code goes in functions.php file of the active child theme (or active theme).
Use woocommerce_product_stock_status_options
instead of woocommerce_product_options_stock_status.
This way you can immediately add a status instead of replace the existing dropdown
Also use woocommerce_get_availability_text & woocommerce_get_availability_class
opposite woocommerce_get_availability.
This way you don't have to add the existing statuses again
// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['pre_order'] = __( 'Pre order', 'woocommerce' );
$status['contact_us'] = __( 'Contact us', 'woocommerce' );
return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'contact_us':
$availability = __( 'Contact us', 'woocommerce' );
break;
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
// Get stock status
switch( $product->get_stock_status() ) {
case 'pre_order':
$class = 'pre-order';
break;
case 'contact_us':
$class = 'contact-us';
break;
}
return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
Use woocommerce_admin_stock_html to display the new stock status on the admin products list table
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Simple
if ( $product->is_type( 'simple' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );
// Get stock status
$product_stock_status = $variation->get_stock_status();
/*
Currently the status of the last variant in the loop will be displayed.
So from here you need to add your own logic, depending on what you expect from your custom stock status.
By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
- Product should be in stock if a child is in stock.
- Product should be out of stock if all children are out of stock.
- Product should be on backorder if all children are on backorder.
- Product should be on backorder if at least one child is on backorder and the rest are out of stock.
*/
}
}
// Stock status
switch( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'contact_us':
$stock_html = '<mark class="contact-us" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>';
break;
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
Optional: if desired, the custom stock status can be used in hooks where you already have access to the $product object or you can use global $product.
1) No access to the $product object, use global $product as is the case for example with the woocommerce_shop_loop_item_title or the woocommerce_single_product_summary hook
function woocommerce_my_callback() {
// An example based on global $product
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Use this line during testing, delete afterwards!
echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
// Compare
if ( $product_stock_status == 'My custom stock status' ) {
// Etc..
}
}
}
add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_my_callback', 10 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_my_callback', 10 );
2) Access to the $product object, because it's passed by default to the callback function. As is the case for example with the woocommerce_get_price_html hook
function filter_woocommerce_get_price_html( $price, $product ) {
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get stock status
$product_stock_status = $product->get_stock_status();
// Use this line during testing, delete afterwards!
echo '<p style="color:red;font-size:20px;">DEBUG INFORMATION = ' . $product_stock_status . '</p>';
// Compare
if ( $product_stock_status == 'My custom stock status' ) {
// Etc..
// $price .= ' my text';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
In addition to filters provided by 7uc1f3r woocommerce_product_export_product_column_stock_status filter is required to display custom stock status in exported products CSV file:
function add_custom_stock_csv_data( $_, $product ) {
$status = $product->get_stock_status( 'edit' );
switch( $status ) {
case 'pre_order':
case 'contact_us':
return $status;
case 'onbackorder':
return 'backorder';
case 'instock':
return 1;
default:
return 0;
}
}
add_filter( 'woocommerce_product_export_product_column_stock_status', 'add_custom_stock_csv_data', 10, 2 );

Woocommerce Change "Select option" text in variable product page

On product page 'dropdown) i'd like to modify the SELECT OPTIONS text and i assume this code could to this but nothing change in my page even if i empty cache.
Thanks for your help
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'variable':
return __( 'Options', 'woocommerce' );
break;
}
}
Use the following code --
add_filter('woocommerce_dropdown_variation_attribute_options_args', 'custom_woocommerce_product_add_to_cart_text', 10, 2);
function custom_woocommerce_product_add_to_cart_text($args){
$args['show_option_none'] = __( 'Options', 'woocommerce' );
return $args;
}
I'm little late but below code can also be used .
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'variable':
return __( 'Options', 'woocommerce' );
break;
}
case 'simple':
return __( 'Purchase', 'woocommerce' );
break;
}
}
Reference : https://www.raadeen.com/change-select-options-text-woocommerce/

Change title "Additional Information" in woocommerce

I want to change the title from the checkout page. But I just can change the label and the placeholder.
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Please type your PO number here and we will add it to the invoice.';
$fields['order']['order_comments']['label'] = '';
return $fields;
}
https://docs.woocommerce.com/document/editing-product-data-tabs/#
/**
* Rename product data tabs
*/
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['reviews']['title'] = __( 'Ratings' ); // Rename the reviews tab
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
return $tabs;
}
As it stands there is not hook to change the section title. But here's the hack if you are desperate enough to make the modification.
Locate your template folder
Create a folder named 'checkout'
Locate the file form-shipping.php in the Woocommerce plugin foler under templates/checkout/
Copy file in step 3 to the folder created in step 2
Now you have superpowers over the checkout form
Edit this line:
<h3><?php _e( 'Additional Information', 'woocommerce' ); ?></h3>
This solution worked for me:
function ajg_relabel_additional_information_tab(){
return __( 'Specification', 'text-domain' );
}
add_filter('woocommerce_product_additional_information_heading', 'ajg_relabel_additional_information_tab');
function th_wc_order_review_strings( $translated_text, $text, $domain ) {
if(is_checkout()){
switch ($translated_text) {
case 'Billing details' :
$translated_text = __( 'Billing Info', 'woocommerce' );
break;
case 'Additional information':
$translated_text = __('New Field Name', 'woocommerce');
break;
case 'Your order':
$translated_text = __('My Order', 'woocommerce');
break;
case 'Product':
$translated_text = __('Your Product', 'woocommerce');
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'th_wc_order_review_strings', 20, 3 );
The documentation of Woocommerce is not complety...
https://docs.woocommerce.com/document/editing-product-data-tabs/
You would check condition about the callback before add or replace some value in array, otherwise the tab will display with nothing inside.
/**
* Filter product data tabs
*/
function filter_product_tabs( $tabs ) {
global $product;
if ( isset($tabs['description']['callback']) ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['description']['priority'] = 5; // Description
}
if ( isset($tabs['additional_information']['callback']) ) {
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
$tabs['additional_information']['priority'] = 10; // Additional information
}
if ( isset($tabs['reviews']['callback']) ) {
$tabs['reviews']['title'] = __( 'Review' ) . ' (' . $product->get_review_count() . ') '; // Rename the reviews tab
$tabs['reviews']['priority'] = 15; // Reviews
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_product_tabs', 98 );
Why ? because the developper Woocommerce will check the content of array in the tab template
(version 3.8.0) (WC
/**
* Filter tabs and allow third parties to add their own.
*
* Each tab is an array containing title, callback and priority.
*
* #see woocommerce_default_product_tabs()
*/
$product_tabs = apply_filters( 'woocommerce_product_tabs', array() );
if ( ! empty( $product_tabs ) ) :
....
This worked for me if anyone is still after this change
//Shipping Weight custom tab name
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['additional_information']['title'] = __( 'Additional Information' ); // Rename the Additional Information text
return $tabs;
}

Resources