Ru

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.

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

Copy

BTC (Network BTC) - 1C2EWWeEXVhg93hJA9KovpkSd3Rn3BkcYm

Ethereum (Network ERC20) - 0x05037ecbd8bcd15631d780c95c3799861182e6b8

Similar posts

This website uses cookies. By clicking the 'Accept' button or continuing to use the website, you agree to the use of cookies.