If you use the Abandoned Cart Recovery for WooCommerce plugin (or its similar plugin), you’ve probably noticed that many products are added by bots. The main problem is that some of these bots are googlebot.
Why does Googlebot add items to the cart?
Googlebot crawls the site by clicking on all available links, including “Add to cart” buttons. If the button is implemented as a regular link
<a href=“?add-to-cart=123”>
the bot may accidentally click on it, thus adding the item to the cart.
I’ve come up with a simple solution that will save crowding budget and completely solve this problem.
First, you need to add the following lines to the robots.txt file:
Disallow: /cart/
Disallow: /checkout/
Disallow: /*?add-to-cart=*
You should also add the following lines to the functions.php file (of the child theme):
if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('/Googlebot|bingbot|Baiduspider|YandexBot/i', $_SERVER['HTTP_USER_AGENT'])) {
if (isset($_GET['add-to-cart'])) {
header('HTTP/1.1 403 Forbidden');
exit;
}
if ((defined('DOING_AJAX') && DOING_AJAX) || (wp_doing_ajax())) {
if (isset($_POST['add-to-cart'])) {
wp_send_json_error(['error' => 'Bots are not allowed to add products to cart.'], 403);
}
}
}
}
add_action('init', 'prevent_bot_cart_addition');
Leave a Reply