How to hide the "read more" button, out of stock product card, so that AJAX works when adding a product to the cart on a WooCommerce site.
April 10, 2022
upd: July 8, 2025
1. You need to copy the code below into the functions.php file, which is located in your theme folder (the code was taken from the official site woocommerce).
/**
* 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');
}
}
2. Create in the root folder of your theme, folders in sequence - woocommerce\loop\ and create in folder - loop, file - add-to-cart.php, after, copy the code below into it:
/**
* Loop Add to Cart
*
* This template can be overridden by copying it to yourtheme/woocommerce/loop/add-to-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
echo sprintf(
'<a href="%s" data-product_id="%s" class="%s custom_button_buy">%s</a>',
$product->add_to_cart_url(),
$product->get_id(),
'btn btn-black product_type_' . $product->get_type() . ' ' . ( $product->supports( 'ajax_add_to_cart' ) ? 'add_to_cart_button ajax_add_to_cart' : '' ),
$product->add_to_cart_text()
);
This code will replace the original WooCommerce plugin code. If this is not done, then AJAX will not work, since the original plugin file does not contain - "data-product_id="%s" и %s custom_button_buy.
Share
USDT (TRON (TRC20)): TTvJdwtL3VAZKSHbYi8B2eQEQDxbHUD4Ka
POL (Polygon PoS): 0x97377684b9a589eca92e2c6c8430e6dcf2bae8c2
ETH (Base Mainnet): 0x97377684b9a589eca92e2c6c8430e6dcf2bae8c2
ETH (ERC20): 0x97377684b9a589eca92e2c6c8430e6dcf2bae8c2
BTC (BTC): 12GkhJZWrdn23PUerGerN7nSZXHwWGm59U





Comment on