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.
Related
I am trying to change the Add To Cart text on the product page button to Buy Now
I've tried using the function that's being posted everywhere to solve the issue but somehow that doesn't seem to do the trick, also using a WordPress plugin is not changing the text.
So far I've found the following function
// 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' );
}
// To change add to cart text on product 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' );
}
Adding that to my Code Inserter creates an error and breaks the website.
I think that Divi is overruling the function from somewhere. Any tips or ideas?
I am applying the below code to change the URL of “View Cart” button on the Shop page.
// Change View Cart Button URL from /cart to /checkout
add_filter( 'woocommerce_add_to_cart_redirect', 'cart_to_checkout' );
function cart_to_checkout( $url ) {
return wc_get_checkout_url();
}
What is the filter that I need to use to achieve the same for “View Cart” button in the “Woocommerce error message” on the Single Product Page?
Woocommerce error message” appears something like this: “The maximum allowed quantity for ‘Product X’ is 3 (you currently have 3 in your cart). “View Cart”
So, I need to change the URL of the “View Cart” button in the above message.
And also, all the View Cart buttons must point to Checkout Page and not Cart Page.
Thanks!
As this notice is hard coded in WC_Cart add_to_cart() method (from line 1075 to 1083):
throw new Exception(
sprintf(
'%s %s',
wc_get_cart_url(),
__( 'View cart', 'woocommerce' ),
/* translators: 1: quantity in stock 2: current quantity */
sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) )
)
);
The only way to change the url link is using woocommerce_get_cart_url filter hook located inside the function wc_get_cart_url() (used in this notice) as follow for single product pages only:
add_filter( 'woocommerce_get_cart_url', 'filter_get_cart_url' );
function filter_get_cart_url( $url ) {
// Only on single product pages
if( is_product() )
$url = wc_get_checkout_url();
return $url;
}
To change all url links that are using wc_get_cart_url() to checkout url, you will use instead:
add_filter( 'woocommerce_get_cart_url', 'wc_get_checkout_url' );
Addition: To make it work everywhere except on cart and checkout pages, use the following:
add_filter( 'woocommerce_get_cart_url', 'filter_get_cart_url' );
function filter_get_cart_url( $url ) {
// Except on cart and checkout pages
if( ! ( is_cart() || is_checkout() ) )
$url = wc_get_checkout_url();
return $url;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related: Change the "view cart" product overlay button on product loops in Woocommerce
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.
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 using woocommerce plugin in a wordpress site to sell some products. There are two types of products. some products will have add to cart button, but some will have call to order button instead of add to cart. But Both type of products will show the price and discounted price.
I am using some code to change the text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
return __( 'Call to order', 'woocommerce' );
}
that works. But I when I am using this code to change the url or say stop the button to add to cart that is not working.
add_filter( 'woocommerce_product_add_to_cart_url', 'woo_more_info_link' );
function woo_more_info_link( $link ) {
global $product; // switches link in all cases, i.e. in plugins
$link = get_permalink( $product->id );
return $link;
}
I actually want that the add to cart button should not working and should say only call to order.
Can anybody tell me how to stop add to cart from working.