How do I WooCommerce empty cart message - php

I've tried editing the empty cart message, but the changes made do not seem to show on the front end.
From the usual text "No products in the cart." to "Oops! Looks like your cart is empty." I've also tried the code provided by numerous other similar questions asked, but none seem to work either.
I tried using the following code in the mini-cart.php file;
<p class="woocommerce-mini-cart__empty-message"><?php esc_html_e( 'Oops! Looks like your cart is empty.', 'woocommerce' ); ?></p>
and also tried using the following code in the functions.php file;
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'woocommerce_widget_shopping_cart_buttons', 'add_clear_cart_button', 10, 2 );
function add_clear_cart_button() {
?>
<a class="button" href="<?php echo $woocommerce->cart->get_cart_url(); ?>?empty-cart"><?php _e( 'Empty Cart', 'woocommerce' ); ?></a>
<?php
}

add_filter('gettext_woocommerce', 'translate_woocommerce_strings');
function translate_woocommerce_strings($string) {
if ('No products in the cart.' === $string) {
$string = esc_html__('Oops! Looks like your cart is empty.', 'woocommerce');
}
return $string;
}

Related

WooCommerce Subscriptions: Remove "Browse products" button on my account page

I want to remove the "Browse Products" button on the subscription page of My account area.
I found the output in the template file my-subscriptions.php.
But there is no filter to remove it without editing the template file.
Is there any other way to do that?
Maybe there is a way to change the link of the button (to a specific product) and the text?
This is the code for the link:
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
<?php esc_html_e( 'Browse products', 'woocommerce-subscriptions' ); ?>
</a>
add_action( 'wp_head', 'hide_browse_product_element', 100 );
function hide_browse_product_element() {
echo "<style> .no_subscriptions{display:none;} </style>";
}
Try this code snippet
If you want to change the text without overriding the template, try this
function change_browse_product_element( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Browse products' :
$translated_text = __( 'My Button Text', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'change_browse_product_element', 20, 3 );
From here
For changing the link, please use below code.
add_filter( 'woocommerce_return_to_shop_redirect', 'mujuonly_rediect_browse_product' );
function mujuonly_rediect_browse_product( $url ) {
return "https://www.google.com";
}
You can hide with css:
div.woocommerce-Message.woocommerce-Message--info.woocommerce-info
a.woocommerce-Button.button
{
visibility: hidden !important;
}
Anyway hide with css just hide all message box, so no idea if this will be useful for you or somebody.

Is this way using a Custom Field to change Add To Card Text in WooCommerce good?

I want to be able to display a specific Add To Card Text for each single product, if set.
There are some plugins out there, but they "just" cover Add To Card Text for categories or product types. That's why I tried this way using a Custom Field.
So I wrote this code directly in a template file. This code works, but is it good, safe, at the right place?
// file: /woocommerce-templates/single-product/add-to-cart/simple.php
<button "..." >
<?php
$text = get_post_meta($product->get_id(), 'add_to_cart_text', true);
if ($text) {
echo esc_html($text);
} else {
echo esc_html($product->single_add_to_cart_text()); // this line is default
}
?>
</button>
I would use this instead:
<?php
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_button_text', 20, 2 );
function custom_add_to_cart_button_text( $button_text, $product ) {
$custom_button_text = get_post_meta( $product->get_id(), 'add_to_cart_text', true );
// If there is custom text set for this product, then display it instead of the default text.
if ( ! empty( $custom_button_text ) ) {
$button_text = __( $custom_button_text, 'text-domain' );
}
return $button_text;
}
Paste it in your functions.php

Change cart totals title text on cart page in Woocommerce

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>

Rename Add to Cart buttons for Out Of Stock Products in WooCommerce 3

I would like to rename 'Add to Cart' to 'Sold Out' when product is sold out in WooCommerce.
This is my code:
add_filter( 'add_to_cart_text', 'woo_custom_single_add_to_cart_text' );
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() {
if( $product->get_stock_quantity() == 0 )
return __( 'Sold Out', 'woocommerce' );
} else {
return __( 'Add to cart ', 'woocommerce' );
}
But it's not working.
What I am doing wrong please?
UPDATE (Working now with variable products and variations)
There is some errors in your code:
1.You need to to set the hooks arguments in your function as $button_text and $product.
2.The add_to_cart_text is deprecated and replaced by woocommerce_product_add_to_cart_text.
In this updated code I have added a jQuery script that catch the selected variation for variable products only (on single product pages) and to change the button text. Now this is working for all cases.
add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {
$sold_out = __( "Sold Out", "woocommerce" );
$availability = $product->get_availability();
$stock_status = $availability['class'];
// Only for variable products on single product pages
if ( $product->is_type('variable') && is_product() )
{
?>
<script>
jQuery(document).ready(function($) {
$('select').blur( function(){
if( '' != $('input.variation_id').val() && $('p.stock').hasClass('out-of-stock') )
$('button.single_add_to_cart_button').html('<?php echo $sold_out; ?>');
else
$('button.single_add_to_cart_button').html('<?php echo $button_text; ?>');
console.log($('input.variation_id').val());
});
});
</script>
<?php
}
// For all other cases (not a variable product on single product pages)
elseif ( ! $product->is_type('variable') && ! is_product() )
{
if($stock_status == 'out-of-stock')
$button_text = $sold_out.' ('.$stock_status.')';
else
$button_text.=' ('.$stock_status.')';
}
return $button_text;
}
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
It looks like you forgot to declare $product as a global.
Your filter function should start with global $product;

Combined WooCommerce Checkout/Cart page and adding cross-sell

I've combined the shortcodes for checkout and cart onto the Checkout page in two columns to reduce the number of clicks to finish payment.
However, I'm finding that hooks seem to act strangely with this arrangement.
For example, I'm trying to place the cross-sell section below the cart section. On the default cart page, it appears by default. Combining cart and checkout together makes it disappear.
The logical approach is to do this:
function add_cart_collaterals() {
if (is_checkout()) {
add_action( 'woocommerce_after_cart_contents', 'woocommerce_cross_sell_display' );
}
}
add_action('wp', 'add_cart_collaterals');
That did nothing.
Out of desperation, I then copied and adapted the cross-sell.php template code and put it directly into my child theme's functions.php file like this:
/* Display Cross-Sells below cart */
function show_cross_sell() {
if ( $cross_sells ) :
echo '<div class="cross-sells"><h2>';
_e( 'You may be interested in…', 'woocommerce' );
echo '</h2>';
woocommerce_product_loop_start();
foreach ( $cross_sells as $cross_sell ) :
$post_object = get_post( $cross_sell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' );
endforeach;
woocommerce_product_loop_end();
echo '</div>';
else : {
echo 'No cross sells to display';
}
endif;
}
add_action( 'woocommerce_after_cart_table', 'show_cross_sell', 10 );
But, only the else condition runs and displays "No cross sells to display". So maybe it lost scope on the $cross_sells object.
Is there any hope of my being able to achieve this?
WooCommerce support came through with an answer that helped me. I wasn't assigning $cross_sells to anything. I needed to assign it like this $cross_sells = array_filter( array_map( 'wc_get_product', WC()->cart->get_cross_sells() ), 'wc_products_array_filter_visible' );
But I'm still curious to know why the hook action wasn't working.
As to why you couldn't achieve it with the hook, it's probably because of the first line of code in the function that you hooked into that action:
function woocommerce_cross_sell_display( $limit = 2, $columns = 2, $orderby = 'rand', $order = 'desc' ) {
if ( is_checkout() ) {
return;
}

Categories