Change cart totals title text on cart page in Woocommerce - php

I'm looking to change the text "Cart Totals" in the cart totals div in WooCommerce or remove it totally with an action.
I've added different text above the box in using
add_action( 'woocommerce_before_cart_totals', 'custom_before_cart_totals' );
function custom_before_cart_totals() {
echo '<h2>Checkout</h2>';
}
But I cant find a way to remove the default wording "Cart Totals" other than editing a WooCommerce template or target and hiding with css, but would love something that I can place in the functions file to either change the old text or remove it completely.
Any advice would be appreciated.
Default Cart Totals Example

It is possible using the WordPress filter hook gettext.
1) Removing "Cart totals":
add_filter( 'gettext', 'change_cart_totals_text', 20, 3 );
function change_cart_totals_text( $translated, $text, $domain ) {
if( is_cart() && $translated == 'Cart totals' ){
$translated = '';
}
return $translated;
}
2) Replace (or change) "Cart totals":
add_filter( 'gettext', 'change_cart_totals_text', 20, 3 );
function change_cart_totals_text( $translated, $text, $domain ) {
if( is_cart() && $translated == 'Cart totals' ){
$translated = __('Your custom text', 'woocommerce');
}
return $translated;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Or you can remove it from the Woocommerce template cart/cart_totals.php

function change_cart_totals($translated){
$translated = str_ireplace('Cart Totals', 'Cart Total', $translated);
return $translated;
}
add_filter('gettext', 'change_cart_totals' );

Duplicate the cart-totals.php theme from woocommerce into your own theme, and replace this line:
<h2><?php esc_html_e( 'Cart totals', 'woocommerce' ); ?></h2>

Related

Change Checkout "Billing Details" text for a specific product in Woocommerce

How can I change the text "Billing Details" in Woocommerce checkout page only for a specific product?
I have tried:
/* Change the Billing Details checkout label to Your Details: */
function wc_billing_field_strings( $translated_text, $text, $domain )
{
switch ( $translated_text ) {
case 'Billing Details' :
$translated_text = __( 'Your Details', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );
But it's changing the text for all products… How could I do this for one specific product?
To change a translatable text in checkout page when there is a specific item in cart, use the following:
add_filter( 'gettext', 'change_conditionally_checkout_heading_text', 10, 3 );
function change_conditionally_checkout_heading_text( $translated, $text, $domain ) {
if( $text === 'Billing details' && is_checkout() && ! is_wc_endpoint_url() ){
// HERE set the desired specific product ID
$targeted_product_id = 1980;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $targeted_product_id == $cart_item['data']->get_id() ) {
return __( 'Your Details', $domain );
break; // Stop the loop
}
}
}
return $translated;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note: In Woocommerce checkout the text to change is "Billing details" without a capital in "Details"
First, override the Woocommerce plugin template and change inside it
wp-content/plugins/woocommerce/templates/checkout/form-checkout.php
find your necessary template and change the text.

Override External Product URL to "Add to Cart" product button

I work on site that use External products from Amazon, but want instead pointing users to that external URL, first to add to cart that product. I have this function, that change Default Button text for each product, to Add to cart.
function sv_wc_external_product_button( $button_text, $product ) {
if ( 'external' === $product->get_type() ) {
// enter the default text for external products
return $product->button_text ? $product->button_text : 'Add To Cart';
}
return $button_text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'sv_wc_external_product_button', 10, 2 );
But this function not add product to cart.
How to make this function to Add selected product to cart?
Thanks.
Updated 2020
This is a complete different way with simple products and a custom field external link.
In this answer we will use simple products instead of external products.
We add an "External URL" custom field in product option settings and we save the data.
Add a custom field on general product option settings for simple products only :
add_action( 'woocommerce_product_options_general_product_data', 'simple_product_with_external_url' );
function simple_product_with_external_url() {
global $product_object;
echo '<div class="options_group show_if_simple hidden">';
// External Url
woocommerce_wp_text_input( array(
'id' => '_ext_url_cust',
'label' => 'External Url',
'description' => 'Custom external URL',
'desc_tip' => 'true',
'placeholder' => 'Enter here your custom external URL'
) );
echo '</div>';
}
Save the custom field data if it's a simple product and not empty:
add_action( 'woocommerce_admin_process_product_object', 'save_simple_product_with_external_url' );
function save_simple_product_with_external_url( $product ) {
if( $product->is_type('simple') && isset($_POST['_ext_url_cust']) ) {
$product->update_meta_data( '_ext_url_cust', sanitize_url($_POST['_ext_url_cust']) );
}
}
2) This will not work on shop pages and archives pages, if we don't set in WooCommerce the cart redirection when adding a product to cart.
So we will replace add-to-cart button (just for our simple products with a custom link redirection) on shop pages and archives pages by a linked custom button to single product pages.
Replacing add-to-cart button in shop pages and archives pages (for simple products with custom external url):
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
$external_url = $product->get_meta('_ext_url_cust');
if ( ! empty($external_url) ) {
$html = sprintf( '%s', $product->get_permalink(), __("Read More", "woocommerce") );
}
return $html;
}
3) If the custom field value is not empty, the product is added to cart first and then redirected to the external URL (our custom field value in single product pages)
External URL redirection after adding to cart (when custom field is not empty in simple products):
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_simple_product_with_external_url' );
function redirect_simple_product_with_external_url( $url ) {
if( isset($_REQUEST['add-to-cart']) && absint( $_REQUEST['add-to-cart'] ) > 0 )
return get_post_meta( absint( $_REQUEST['add-to-cart'] ), '_ext_url_cust', true );
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce version 3+
Use https://stackoverflow.com/a/44036965/3730754 instead.
You should try to use woocommerce_product_add_to_cart_url filter hook to change the add-to-cart link (here for grouped products), this way:
add_filter( 'woocommerce_product_add_to_cart_url', 'override_external_product_url', 10, 2 );
function override_external_product_url( $url, $product ){
if ( 'external' === $product->get_type() ) {
//Get product ID -- WooCommerce compatibility
if ( method_exists( $product, 'get_id' ) ) {
$product_id = $product->get_id();
} else {
$product_id = $product->id;
}
// custom add to cart url example
$url = home_url( "/product/?add-to-cart=$product_id");
}
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Update: But this will not add to cart this external product before redirecting to an external url even if it works displaying the add-to-cart url (as add-to-cart is ajax driven).
I fixed myself. For External products, to replace default "Buy This Product" with other generic text, add this functions into functions.php file into theme:
add_filter( 'woocommerce_product_add_to_cart_text' ,
'wpf_custom_add_cart_text_archive',11);
function wpf_custom_add_cart_text_archive() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Add to Cart', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
}
add_filter( 'woocommerce_product_single_add_to_cart_text',
'wpf_custom_add_cart_text',11);
and this one:
function wpf_custom_add_cart_text() {
return __( 'Add to Cart', 'woocommerce' );
}
to replace text everywhere.

How to change 'select an option' text from 'WooCommerce Product Add-ons' plugin

i'm using the woocommerce product Add-ons plugin and i want to change the default value that appears on dropdowns that says 'select an option':
I managed to find all other english texts in the plugins editor and changed them but this single line is bugging me because i can't seem to find where it is initialized.
EDIT
This is the exact plugin i'm using:
Woocommerce product Add-Ons
Any help would be much appreciated!
The quickest way would be to add a line on your functions.php child theme where you can change text:
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text, $untranslated_text, $domain )
{
if ($untranslated_text == 'Select an option...') {
$translated_text = __( 'ENTER HERE THE NEW TEXT', $domain );
}
return $translated_text;
}
Bear in mind that you might need to remove the ... at the end of the "Select an option" text.
Hope that helps
Use this code and place it into functions.php
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text, $untranslated_text, $domain )
{
if ($untranslated_text == 'Select an option...') {
$translated_text = __( 'PAST YOUR TEXT HERE', $domain );
}
if ($untranslated_text == 'None') {
$translated_text = __( 'PAST YOUR TEXT HERE', $domain );
}
return $translated_text;
}

WooCommerce checkout page change Your order details Shipping text

woocommerce checkout page change Your order details Shipping text label.
i want attached image please check it
You can use the WordPress gettex() function that will replace the concerned text in checkout page:
add_filter( 'gettext', 'customizing_checkout_text', 10, 3 );
function customizing_checkout_text( $translated_text, $untranslated_text, $domain )
{
if ( $untranslated_text == 'Shipping' && is_checkout() ) {
$translated_text = __( 'Here goes your custom text', $domain );
}
return $translated_text;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
You can try this to resolve your issue:
Put this code in your theme's function.php file.
add_filter('ngettext', 'translate_shipping');
function translate_shipping($translated) {
$translated = str_ireplace('Shipping', 'Your Custom Text', $translated);
return $translated;
}

woocommerce custom "Add to cart" text/link

How can I change the "Add to cart" button text/link in woocommerce (v2.4)?
I tried to add this code to my functions.php, but it doesn´t seem to work:
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
$text = _( 'Learn More', 'woocommerce' );
echo 'Learn more';
}
It was asked for WC 2.4. If you need it for newer versions you should use
add_filter( 'woocommerce_product_add_to_cart_text',function(){
return __( 'Learn more','your-textdomain' );
} );
Tested with WooCOmmerce 5.6
You have written correct code, but before using woocommerce_after_shop_loop_item hook, you have to remove "add to cart" button using same hook as shown below.
Step 1 - Remove "add to cart" button from shop
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_filter( 'woocommerce_is_purchasable', false );
}
add_action('init','remove_loop_button');
Step 2 -Add new button that links to product page for each product
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
echo do_shortcode('Learn more');
}
To change you add to cart button text paste this code in your theme functions.php
add_filter( 'add_to_cart_text', 'woo_custom_single_add_to_cart_text' ); // < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_single_add_to_cart_text' ); // 2.1 +
function woo_custom_single_add_to_cart_text() {
return __( 'Learn More', 'woocommerce' );
}
This line of code needs to be added to the functions.php
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
This is for removing the product from product listing page.
#Lorenz I think you should go through this https://www.wpblog.com/add-to-cart-button-in-woocommerce-store/
// To change add to cart text on product Shop/archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
function woocommerce_custom_product_add_to_cart_text() {
return __( 'Buy Now', 'woocommerce' );
}
// To change add to cart text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Buy Now', 'woocommerce' );
}
Source: Webtalkhub

Categories