Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active June 8, 2023 16:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogists/42265d45b6aa2b7774dd0c6a9c3aa641 to your computer and use it in GitHub Desktop.
Save woogists/42265d45b6aa2b7774dd0c6a9c3aa641 to your computer and use it in GitHub Desktop.
[Theming Snippets] Hide loop read more buttons for out of stock items
/**
* Hide loop read more buttons for out of stock items
*/
if (!function_exists('woocommerce_template_loop_add_to_cart')) {
function woocommerce_template_loop_add_to_cart() {
global $product;
if ( ! $product->is_in_stock() || ! $product->is_purchasable() ) return;
wc_get_template('loop/add-to-cart.php');
}
}
@lawleypopdesign
Copy link

This breaks the ajax functionality on the add to cart button for single products on the Storefront theme.

(After implementing the code, the page reloads entirely and brings me back to the top of the page when I click the add to cart button for single products on the archive pages. Without the code, the button works as intended; the product is added to the cart WITHOUT a full page refresh.)

@dannyfoo
Copy link

Using OceanWP with Code Snippets and it returned:

The snippet has been deactivated due to an error on line 6:
Cannot redeclare function woocommerce_template_loop_add_to_cart.

@shanomurphy
Copy link

shanomurphy commented Feb 8, 2021

This breaks the ajax functionality on the add to cart button for single products on the Storefront theme.

(After implementing the code, the page reloads entirely and brings me back to the top of the page when I click the add to cart button for single products on the archive pages. Without the code, the button works as intended; the product is added to the cart WITHOUT a full page refresh.)

Yes this is breaking the default woocommerce ajax functionality of the 'Add to cart' buttons.

Though I don't like removing elements this way a quick fix alternative is to simply hide the out-of-stock item buttons with CSS:

.products .outofstock .button {
    display: none;
}

@djdadspins
Copy link

djdadspins commented Aug 20, 2022

This code works from Tom Usborne:

add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
    if ( 'Read more' == $text ) {
        $text = __( 'More Info', 'woocommerce' );
    }

    return $text;
} );

@salweb
Copy link

salweb commented May 29, 2023

It's possible to add a else option that write out of stok text?

@Droidchris79
Copy link

It worked great for months, but now it doesn't. On Chrome, products are added twice when this code is activated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment