Removing or hiding WooCommerce add to cart buttons - php

I would like to hide add to cart button and to show a custom text instead of button.
I am trying following hook to remove button:
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');

Here is the way that you are looking for (I think).
The first function will replace on shop pages the add-to-cart buttons by normal buttons linked to their single product pages, like below:
The second function will replace the add-to-cart button (and quantities) by your custom text as this:
Here is that code:
// Shop and archives pages: we replace the button add to cart by a link to the product
add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2 );
function custom_text_replace_button( $button, $product ) {
$button_text = __("View product", "woocommerce");
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
// replacing add to cart button and quantities by a custom text
add_action( 'woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 1, 0 );
function replacing_template_single_add_to_cart() {
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The text replacement
add_action( 'woocommerce_single_product_summary', function(){
// set below your custom text
$text = __("My custom text goes here", "woocommerce");
// Temporary style CSS
$style_css = 'style="border: solid 1px red; padding: 0 6px; text-align: center;"';
// Output your custom text
echo '<p class="custom-text" '.$style_css.'>'.$text.'</a>';
}, 30 );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works

1. In case you want to completely disable Add to cart button, add this code to your theme's functions.php file.
add_filter( 'woocommerce_is_purchasable', false );
2. To add some HTML content after Add to cart button, try this code.
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
echo '<p>Hi, I'm the text after Add to cart Button.</p>';
}

I was able to do this with a Wordpress plugin, i found later https://wordpress.org/plugins/woo-options/

Related

Woocommerce hide add to cart button EXCEPT for variable products

Im trying to hide the add to cart button in woocommerce for all products except a varibale product i have. I have tried the following which leaves the variable select options (which is what i want) but it hides the add to cart button (which i don't want).
add_filter( 'woocommerce_is_purchasable', '__return_false' );
add_action( 'woocommerce_single_product_summary', 'hide_add_to_cart_button_variable_product', 1, 0 );
function hide_add_to_cart_button_variable_product() {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
}
Is there a way to do this?
All my products are simple products except for this single variable products, so maybe there is a function to hide the cart button for all simples except variables?
add_action('woocommerce_single_product_summary', 'wp66176371_remove_product_description_add_cart_button', 1 );
function wp66176371_remove_product_description_add_cart_button() {
global $product;
if ( !empty($product) && $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
I think you can just edit the simple.php template file in the add-to-cart directory so it wouldn't show the button for simple products.

How to add a shortcode below add to cart in Woocommerce

I need to add a shortcode under "Add to cart" button in Woocommerce product pages. This shortcode is displaying a custom field from a plugin. I've tried the code below to output in a product page but it doesn't get outputted:
add_filter( 'woocommerce_after_shop_loop_item', 'custom_woocommerce_short_description' );
function custom_woocommerce_short_description( $woocommerce_after_shop_loop_item ) {
global $post;
$woocommerce_after_shop_loop_item = $woocommerce_after_shop_loop_item . do_shortcode('[wpuf-meta name="instructions_to_customer"]') ;
return $woocommerce_after_shop_loop_item;
}
I figured it out if anyone needs it:
add_action( 'woocommerce_after_add_to_cart_button', 'misha_after_add_to_cart_btn' );
function misha_after_add_to_cart_btn(){
echo do_shortcode( '[wpuf-meta name="instructions_to_customer"]' );
}

How to remove order total from cart and checkout page woocommerce

I would like to remove order total block on cart and checkout page.
I am not able to find any action or filter to remove order total.
I don't want to use css to hide this column.
Using hooks:
1) Remove cart totals:
// On cart page
add_action( 'woocommerce_cart_collaterals', 'remove_cart_totals', 9 );
function remove_cart_totals(){
// Remove cart totals block
remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );
// Add back "Proceed to checkout" button (and hooks)
echo '<div class="cart_totals">';
do_action( 'woocommerce_before_cart_totals' );
echo '<div class="wc-proceed-to-checkout">';
do_action( 'woocommerce_proceed_to_checkout' );
echo '</div>';
do_action( 'woocommerce_after_cart_totals' );
echo '</div><br clear="all">';
}
2) Remove checkout totals:
// On checkout page
add_action( 'woocommerce_checkout_order_review', 'remove_checkout_totals', 1 );
function remove_checkout_totals(){
// Remove cart totals block
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If its input field you can remove its value like
$('#div_id').val('');
If its div or span you can do
$("div").empty();
S('#div_id div').html('');

Changing Woocommerce "Add to cart" button to "view product" button

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

Hiding prices on the Woocommerce shop pages

I'm using WordPress with a WooCommerce plugin and I would like to hide the prices from the shop page (Eg. $20 - $50). I have tried researching it but I haven't found much relating to this problem.
I only want to hide the prices on the shop page, not the individual product pages.
Any help provided will be much appreciated.
You can use this simple hooked function that will remove all product prices from Woocommerce archive pages as shop, product category archives and product tag archives pages:
add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
If you want to only target Shop pages, you will have to make it this way:
add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
if( ! is_shop() ) return; // only on shop pages
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
Updated: You may also want to 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>';
}

Categories