I want to edit the external button text of a WooCommerce product in functions.php. The following code generates the button:
<button type="submit" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"><?php echo esc_html( $button_text ); ?></button>
I already tried multiple code from other people but every solution does not seem to work. The code I think is most close is the one below:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text' );
function custom_add_to_cart_text( $button_text ) {
// Change the button text here
$s = array(
'Bekijk' => 'Bekijk bij BOL.COM ➞'
);
return strtr($button_text, $s);
}
But somehow the button text stays the same, how can I fix this? :)
I already tried multiple codes from other people but nothing seems to work...
// Change add to cart text on product archives page
add_filter('woocommerce_product_add_to_cart_text', 'woocommerce_add_to_cart_button_text', 10, 2);
// Change add to cart text on single product page
add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_add_to_cart_button_text', 10, 2);
function woocommerce_add_to_cart_button_text($button_text, $product) {
if ('external' == $product->get_type()) {
$button_text = __('Add to Cart Button Text for affiliate', 'woocommerce');
}
return $button_text;
}
You can add the above code snippet into active theme functions.php to change the button text for external products in hop page and single product page
Related
I have seen that there are many questions about changing the "Add to cart" button in WooCommerce and "Enable AJAX add to cart buttons on archives". My question is aimed at mixing (image attached). How can we preserve the first "Add to cart", and replace the second one with a "Checkout" + direct URL?
I don't want the customer to go to the Checkout directly (very good article about that at Jeroen Sormani's webiste), but I would like to offer them the option to do so after adding the product to the cart (My products can only be purchased individually: it makes no sense to have one more "Add to cart").
Stack overflow has some tips like "How to change url in "View Cart" button in Woocommerce?", but it does not include the rename we want.
I have Code Snippets installed because I know you have to touch php code, but I don't know how to mix it up to get this.
Thank you very much for your attention.
Add this code to the Code Snippets plugin or your theme's functions.php file. (I recommend using a child theme)
Change text and url of 'view cart' button on shop and product archive pages.
// Change text and url after click of add to cart button on shop and product archive pages.
add_filter( 'woocommerce_get_script_data', 'so70502187_replace_add_to_cart_button', 10, 2 );
function so70502187_replace_add_to_cart_button( $params, $handle ) {
switch ($handle) {
case 'wc-add-to-cart':
$params['cart_url'] = esc_url( wc_get_checkout_url() );
$params['i18n_view_cart'] = esc_attr__( 'Checkout', 'woocommerce' );
break;
}
return $params;
}
Change text and url on 'item added to cart' notification on single product pages.
// Change text and url on 'item added to cart' notification on single product pages.
add_filter( 'wc_add_to_cart_message_html', 'so70502187_single_replace_add_to_cart_button');
function so70502187_single_replace_add_to_cart_button() {
if ( 'yes' !== get_option( 'woocommerce_cart_redirect_after_add' ) ) {
return sprintf( '%s %s', esc_url( wc_get_checkout_url() ), esc_html__( 'Checkout', 'woocommerce' ), esc_html__( 'The item has been added to your cart.', 'woocommerce' ) );
}
}
Tested and works.
I have created a custom admin product checkbox and if this checkbox is active I would like to replace the 'Add To Cart' button for these products on any shop archive/category page to 'View Product' instead and the button should take the viewer to the product page.
Other shop items that do not have the checkbox active should behave normally and display the 'Add To Cart' button with its functionality preserved.
I have got the checkbox working on the admin end with no issues with the following code :
// Display Checkbox
add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
function product_custom_fields_add(){
global $post;
echo '<div class="product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_no_addcart_product',
'desc' => __('show or hide add to cart', 'woocommerce'),
'label' => __('Hide Add To Cart', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
// Save Checkbox
add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
function product_custom_fields_save($post_id){
// Custom Product Text Field
$no_addcart_product = isset( $_POST['_no_addcart_product'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_no_addcart_product', esc_attr( $no_addcart_product ));
}
I have come up with this code below to try and add the if condition based on the checkbox status.
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( $product->get_meta('_no_addcart_product') === 'yes' )
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="'. $product->get_permalink().'">' . $button_text.'</a>';
return $button;
}
It works partially in changing the button text and the link for any product that has the checkbox active correctly. However the issue is that it also removes the text from all other shop items and leaves a blank button for any item that hasn't got the checkbox active (see image below). The links of these products are also changed to the product pages and not 'Add To Cart' as I would like them to remain.
I have read through a lot of threads that change the button globally for all products but haven't been able to find any conditional logic applied only to certain products that could help me figure this one out. So any help would be greatly appreciated. Many thanks in advance.
Updated
The problem in your last function code is related to your IF statement that needs brackets:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if ( $product->get_meta('_no_addcart_product') === 'yes' ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="'. $product->get_permalink().'">' . $button_text.'</a>';
}
return $button;
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
Trying to change the woocommerce button text from "add to cart" to "read more" and redirect it so that clicking the button takes the user to the individual product page. So far, the link works but all the text on the button says is "Button" when I need it to say "Read More". I'll place the code below, can anyone please tell me what the problem is.
/*STEP 1 - REMOVE ADD TO CART BUTTON ON PRODUCT ARCHIVE (SHOP) */
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
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('<br>[button link="' . esc_attr($link) . '"]Read More[/button]');
}
Try this alternative that will replace add to cart button by a linked button to the product in Shop and archives pages
// Replace add to cart button by a linked button to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Not needed for variable products
if( $product->is_type( 'variable' ) ) return $button;
// Button text here
$button_text = __( "View product", "woocommerce" );
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
Use this code to replace the default “Add to Cart” button with “Read More” (or anything you like) linking to the single product page.
// First, remove Add to Cart Button
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
// Second, add View Product Button
add_action( 'woocommerce_after_shop_loop_item', 'shop_view_product_button', 10);
function shop_view_product_button() {
global $product;
$link = $product->get_permalink();
echo 'View Product';
}
You can add this PHP Snippet at the very bottom of your active child theme (or main theme) functions.php file. Code Source
Using the below code I was able to change the Text of "Add to Cart" button
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 __( 'My Button Text', 'woocommerce' );
}
Similarly, is it possible to replace Add to Cart Button with a Cart Icon or a Cart Icon with Custom Text?
Not sure if my answer is the best practice but for sure it is the easiest i can find so far, just go to Woocommerce folder and find "add-to-cart.php" then in it;s loop just replace esc_html( $product->add_to_cart_text() ) by sprintf('<i class="fa fa-shopping-basket"></i>') -- in case you want to add just a shopping basket icon , just put your html.
I am trying to change the redirect URL in the woocommerce for the 'add to cart' button in pages other than the single product page. The URL for the single product page can be changed with this solution. However this solution will not work for the for any other pages, to change the redirect after pressing the 'add to cart' button. Any help is greatly appreciated.
To make woocommerce_add_to_cart_redirect work on pages like shop, categories or tags archives pages, you need first in backend WooCommerce > Settings > Products > Display:
Disable the checkbox "Enable AJAX add to cart buttons on archives".
Then the hooked the code will work with woocommerce_add_to_cart_redirect hook on all woocommerce pages.
function wc_add_to_cart_custom_redirect() {
// Here the redirection
return site_url('/mypage/');
}
add_filter( 'woocommerce_add_to_cart_redirect', 'wc_add_to_cart_custom_redirect' );
You can use this in your Appearence>Theme Editor>function.php
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 = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
This will change your Add to cart to View porducts and the Url would be that specific products's Url