Changing the HTML of the add to cart button in WooCommerce - wordpress

I have this piece of code which I use for my checkout/order button. Which is pretty convenient, I can style the button and add custom text and classes. I know this won't work with translated content but that is of no importance here because the website is and will stay in only 1 language.
// Filter for adding extra custom line to order button
add_filter('woocommerce_order_button_html', 'mbm_custom_button_html');
function mbm_custom_button_html($button_html)
{
$button_html = '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order">Lidmaatschap starten<br /><span class="extra-text-checkout-button">Betaal pas na gratis proefperiode</span></button>';
return $button_html;
}
I was wondering can I also use the same method for the add to cart button? But then something like add_filter('woocommerce_add_to_cart_button_html', 'mbm_custom_atc_button_html');
I tried to search it in the docs but could not find my answer.

For "Add to Cart" part, you may try this hooks: woocommerce_loop_add_to_cart_link and woocommerce_product_single_add_to_cart_text.
I believe, the first one provides a better solution for you. You may check the detailed usage via: https://stackoverflow.com/a/56179393/11003615 and http://hookr.io/filters/woocommerce_loop_add_to_cart_link/
Hope those helps. Best regards.

for loop page
add_filter('woocommerce_product_add_to_cart_text', 'custom_woocommerce_product_add_to_cart_text', 100);
for product single page
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_woocommerce_product_add_to_cart_text', 100);
function custom_woocommerce_product_add_to_cart_text(){
return 'Add to catd 111';
}

There does not exists filter to change the 'ADD TO CART' button HTML.
If you need to do changes in HTML of 'ADD TO CART' button, you need to override the templates from plugin to your theme.
For example, for simple product 'ADD TO CART' button HTML, you need to override /plugins/woocommerce/templates/single-product/add-to-cart/simple.php to /theme/woocommerce/single-product/add-to-cart/simple.php and do changes in simple.php which is in a theme folder.

Add below code in your active theme's function.php
add_filter( 'woocommerce_order_button_html', 'ro_custom_cart_button_html' );
function ro_custom_cart_button_html( $button_html ) {
$order_button_text = 'Submit';
//add your html below where there is button tag
$button_html = '<button type="submit" class="button alt"
name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr(
$order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' .
esc_html( $order_button_text ) . '</button>';
$button_html = str_replace( 'Place order', 'Submit', $button_html );
return $button_html;
}
Tested and works well

Related

How to change Add to basket text on the storefront theme

I cannot seem to replace the text inside the Add to basket buttons on the single product pages, im using a child theme with the storefront theme for WooCommerce.
Here's what i've tried:
add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_add_to_cart_text');
function woocommerce_custom_add_to_cart_text() {
return __('Add to cart', 'woocommerce');
}
How the button appears on the DOM:
<button type="submit" name="add-to-cart" value="117" class="single_add_to_cart_button button alt">Add to basket</button>
I do think the issue is theme-related as your code does work when I try it on a test site, though I don't have Storefront set as the theme.
You could try a string replace:
add_filter( 'gettext', 'change_woocommerce_strings', 999, 3 );
function change_woocommerce_strings( $changed, $text, $domain ) {
$changed = str_ireplace( 'Add to basket', 'Add to cart', $changed );
return $changed;
}

Adding custom button next to "Add to Cart" Button" Woocommerce

Hi guys im trying to add custom button next to "Add to Cart" button. I added it but i want to change button link for every product how can i do that here is my code: ( i took from another question, it works )
add_action('woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );
function add_a_custom_button() {
global $product;
// Not for variable and grouped products that doesn't have an "add to cart" button
if( $product->is_type('variable') || $product->is_type('grouped') ) return;
// Output the custom button linked to the product
echo '<div style="margin-bottom:10px;">
<a class="button custom-button" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>
</div>';
}
Another way:
Create a new Wordpress custom field for the pages you want to use a custom url link on:
Custom field name: nothanks_link_redirect
Custom field value: https://yourlink.com
Tweak as you need and put this in your child theme's functions.php :
/** WooCommerce custom field - 'No Thanks' Button **/
function nothanks_redirect_button() {
global $post;
$product_id = $post->ID;
$NoThanksLinkRedirectValue = get_post_meta($product_id,'nothanks_link_redirect',true);
if(!$NoThanksLinkRedirectValue) return;
echo '<a class="nothanks-button" style="margin-left: 20px" href="'.$NoThanksLinkRedirectValue.'" target="_self">No Thanks</a>';
}
add_action('woocommerce_after_add_to_cart_button','nothanks_redirect_button');
Enjoy!

How to change "Read more" button text on Wordpress posts

After updating plugins, my "Read more" button on posts changed it's name. In Latvian language it was "Lasīt vairāk", while in Russian "Читать дальше". It's just in plain English. The image of how it looks now is via this link. Plugin updates basically wiped out the padding and the name of the button.
Tried modifying functions.php with
// Replaces the excerpt "Read More" text by a link
function modify_read_more_link() {
return '<a class="read-article" href="' . get_permalink() . '">Your Read
More Link Text</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return '<a class="read-article" href="'. get_permalink($post->ID) . '">
Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
Tried loco translate and modifying internal translations of Elementor-related plugins. The closest I could get is that the elements is named "eael-post-elements-readmore-btn". Styling the element with CSS doesn't do anything. Padding or margin do not work. It's locked. Can anyone provide a hint?
The button should be in Latvian and Russian language, not in English.
PS. Figured that it is Elementor posts plugin related overriding functions.php and translators as well. At this moment, can't figure how to CSS this thing. Stays static.
Add this in functions.php:
function develop_custom_excerpt_more($more) {
global $post;
// edit here if you like
return '... <a class="excerpt-read-more" href="'. get_permalink( $post->ID ) . '" title="'. __( 'Read ', 'domain_name' ) . esc_attr( get_the_title( $post->ID ) ).'">'. __( 'Show more »', 'domain_name' ) .'</a>';
}
add_filter( 'excerpt_more', 'develop_custom_excerpt_more' );
Figured that it is Elementor posts plugin related overriding functions.php and translators as well. The eael-post-elements-readmore-btn was not changing in padding nor in margin because line-height which by default is set to 1 could not allow space for expansion.

How to add WordPress back button for single page only

I would like to add a "back" button to a single page of my WordPress site. How can I do this? I found a lot of different code options but none for WordPress specifically that will work on one page.
I can't find the individual *.PHP file for a specific WordPress page, which I think would be required to implement something like this code:
<button type="button" onclick="history.back();"> Back </button>
I tried adding this to the "Text" tab in WordPress, but there apparently is no
history.back function built in.
try to add this code to your function.php
add_action( 'back_button', 'wpse221640_back_button' );
function wpse221640_back_button() {
if ( wp_get_referer() ) {
$back_text = __( '« Back' );
$button = "\n<button id='my-back-button' class='btn button my-back-button' onclick='javascript:history.back()'>$back_text</button>";
echo ( $button );
}
}
add this to your template page
<?php do_action('back_button'); ?>
Let me know.
add_shortcode( 'ekaa_back_btn' , 'ekaaBackButton');
function ekaaBackButton(){
if ( wp_get_referer() ) {
$button = "\n<button id='ekaa-back-button' class='btn button ekaa-back-button' onclick='javascript:history.back()'><i class='fas fa-arrow-left'></i>Back</button>";
echo ( $button );
}
}
this is work for me using WordPress Shortcode

How can I change the text of WooCommerce Bookings "Book now" button?

I am learning more and more php as i go along. I sorta get the gist of how php hooks work (actions, filters)
I was attempting to add a filter to my child functions page:
add_filter('woocommerce_booking_single_check_availability_text', 'wc_change_button_text');
function wc_change_button_text() {
return __( 'Add to cart', 'woocommerce' );
}
You could use WooCommerce filter hooks to change these, but I wouldn't recommend it because it'll take too long and create code bloat.
Here's what I would recommend: https://wordpress.org/plugins/woocommerce-multilingual/
I'm sure you may have already found a solution, but for anyone else that stumbles across this post, simply add this snippet to your child theme's functions file.
add_filter( 'woocommerce_loop_add_to_cart_link',
'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("Change Text Here", "woocommerce");
$button = '<a class="book-now button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
Tested and works 5.4.1

Resources