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
Related
For non logged in users I use the php snippet below. Visitors see a "Where to buy" button and a "Ask a demo" button instead of the Woocommerce Add to cart button. Logged in users / customers with a specific role gets the "Add to cart button" instead of the 2 buttons for not logged in customers. There is also a viewonly role who has a login and can download materials but don't have an Add to cart button on single product pages. However with the code below the viewonly users can still see the regular Add to cart button. Any idea how i can fix this?
add_action( 'init', 'disable_add_to_cart' );
function disable_add_to_cart() {
if ( !is_user_logged_in() OR $user_role == 'viewonly' ) {
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
function hide_price( $price ) {
if ( !is_user_logged_in() ) {
$price = '';
$user_role = 'viewonly';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'hide_price' );
add_filter( 'woocommerce_cart_item_price', 'hide_price' );
add_filter( 'woocommerce_cart_item_subtotal', 'hide_price' );
add_filter( 'woocommerce_order_formatted_line_subtotal', 'hide_price' );
add_filter( 'woocommerce_cart_subtotal', 'hide_price' );
add_filter( 'woocommerce_cart_totals_order_total_html', 'hide_price' );
}
add_action( 'init', 'hide_price_add_cart_not_logged_in' );
function print_login_to_see() {
echo '<div class="var-prod-container"><a class="btn-where-to-buy" href="/where-to-buy">' . __('Where to buy', 'company') . '</a>';
echo '<a class="btn-ask-a-demo" href="/#uagb-tabs__tab1">' . __('Ask a demo', 'company') . '</a>';
echo '</div>';
}
function hide_price_add_cart_not_logged_in() {
if ( ! is_user_logged_in() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_single_product_summary', 'print_login_to_see', 31 );
}
}```
To replace the buttons, all you have to do is use the actions you removed and place your button function into it. I am using the author role for testing purposes, but this will check if the user is not logged in and what their role is. Then, remove the add to cart button from the shop page loop and also the single product page. Once they are removed, we add what we want into where we just removed the button from.
If you need to, just change the conditions within the if statement and that will change who sees which set of buttons.
Now all you have to do is style your buttons the way you'd like them!
$user_role = wp_get_current_user(
function print_login_to_see() {
echo '<div class="var-prod-container"><a class="btn-where-to-buy" href="/where-to-buy">' . __('Where to buy', 'company') . '</a>';
echo '<a class="btn-ask-a-demo" href="/#uagb-tabs__tab1">' . __('Ask a demo', 'company') . '</a>';
echo '</div>';
}
if ( current_user_can( 'viewonly' ) || !is_user_logged_in() ) { // Set the user role to the specific role you want to replace the buttons for.
// Remove button from products in the loop and single product page
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', 30 );
// Add the actions you just removed, but insert your button function
add_action( 'woocommerce_after_shop_loop_item', 'print_login_to_see');
add_action( 'woocommerce_single_product_summary', 'print_login_to_see', 30 );
}
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.
In my functions.php file I have some remove_actions and add_filters that run for woocommerce but the problem is these functions run for all woocommerce product types.
I'd like to wrap a simple if statement around the hooks/filters I have to only run for grouped product pages the problem is I dont know how woocommerce refers to these pages heres what I have at the moment.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
Heres what i'd like to do but I dont know the correct reference for $grouped_product.
if ($grouped_product) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
I have two add_filter and one remove action i'd like to append in my functions.php to only execute on grouped product pages I just need the correct reference in the first set of brackets in the second code block above.
Tried this code but it doesn't work..
if (is_product() and $product->is_type('grouped')) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
functions php file
<?php
function theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
//Remove cart options from under short description.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
//Filter below adds new tab and returns cart options within tab.
add_filter( 'woocommerce_product_tabs', 'woo_paym_product_tab' );
function woo_paym_product_tab( $tabs ) {
// Adds the new tab
$tabs['paym-plans'] = array( 'title' => __( 'Contract Deals', 'woocommerce' ), 'priority' => 10, 'callback' => 'woo_paym_product_tab_content' );
return $tabs;
}
function woo_paym_product_tab_content() {
// The new tab content
woocommerce_template_single_add_to_cart();
}
//Filter below changes tab priority to display price plans first.
add_filter( 'woocommerce_product_tabs', 'sb_woo_move_description_tab', 98);
function sb_woo_move_description_tab($tabs) {
$tabs['paym-plans']['priority'] = 10;
$tabs['description']['priority'] = 20;
$tabs['additional_information']['priority'] = 30;
return $tabs;
}
?>
Solved the problem trick was to change it to a filter.
add_filter( 'woocommerce_single_product_summary', 'filter_grouped_cart');
function filter_grouped_cart(){
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
}
Checking $product->is_type('grouped') will not help here...
Please check if the code provides any help to you..
global $post;
if( $post->post_parent != 0 ){
echo 'is part of a group';
}
I need help!
I removed the "add to cart" button from my main store page because "see items" makes more sense in this case.
here is the code I added to my child functions.php theme:
`
function remove_loop_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');
add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
global $product;
$link = $product->get_permalink();
$text = _( 'See Items');
echo 'See Items';
}
`
It swaps out the button correctly on the main shop page but in the single post, the upsell also shows "see items" when I want it to say "add to cart" ... I have tried everything.
UPDATE: solved i think? it works, anyway :-)
// add add to cart back to upsell
' remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
function woocommerce_output_upsells() {
remove_action('woocommerce_after_shop_loop_item', 'replace_add_to_cart');
add_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}
}
} '
I am looking for a function to change the Add to Cart text on a woocommerce button but only if the product in question has a specific tag. ie if the product has the tag "preorder" the button text changes to "Pre Order Now"
Changing the text globally can be achieved with this;
http://docs.woothemes.com/document/change-add-to-cart-button-text/
Thanks.
You can check for that particular term with has_term.
//For single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
global $product;
if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
return __( 'Pre order Now!', 'woocommerce' );
endif;
}
//For Archive page
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
return __( 'Pre order Now!', 'woocommerce' );
else:
return __( 'Add to Cart', 'woocommerce' );
endif;
}
Let me know the output.
It's been a while, but try something along the following lines:
<?php
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
/**
* custom_woocommerce_template_loop_add_to_cart
*/
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_tag = $product->product_tag;
switch ( $product_tag ) {
case 'preorder':
return __( 'Pre order Now!', 'woocommerce' );
break;
default:
return __( 'Add to cart', 'woocommerce' );
}
}
Basically what you're trying to do is do a conditional where you check if the product tag is "preorder". I am not sure if this exact code would work if there are multiple tags.
Edit:
You might have to use this filter to change it on the product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
Add this into your child theme function.php file
add_filter( 'woocommerce_product_add_to_cart_text',
'woocommerce_add_to_cart_button_text_archives' );
function woocommerce_add_to_cart_button_text_archives() {
return __( 'Add to Cart Button Text', 'woocommerce' );
You can fix it by adding a few lines of codes in functions.php:
// Change the add to cart text on single product pages
add_filter( 'woocommerce_product_single_add_to_cart_text’, ‘woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
return __( 'ADD TO CART', 'woocommerce' );
}
//Change the add to cart text on product archives
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
return __( 'ADD TO CART', 'woocommerce' );
}
http://visupporti.com/woocommerce-how-to-change-add-to-cart-text/