elementor / elementor/elementor
Hide "Add to cart" button using WooCommerce hooks with "Add to cart" element
- Dominant language
- PHP
- Stars
- 7.1k
- Forks
- 1.6k
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 193
Description
## Prerequisites
- [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [x] The issue still exists against the latest stable version of Elementor.
## Description
I need to hide the "Add to cart" button when a user is not logged. We are using the "Add to cart" element in our page. After a search, it seems like we need to use these lines to make WooCommerce not display the "Add to cart" 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', 30 );
```
It works in the Archive page but not in the Single Product page.
## Steps to reproduce
1. Create a Single Product page
2. Add a "Add to cart" element in the page
3. Add the 2 lines of code in functions.php
## Isolating the problem
- [x] This bug happens with only Elementor plugin active (and Elementor Pro).
- [x] This bug happens with a default WordPress theme active.
- [x] I can reproduce this bug consistently using the steps above.
After investigation, I think this is a bug in Elementor Pro in `woocommerce/widgets/product-add-to-cart.php` (around line 40) :
```
get_type() ); ?>">
```
I think calling the WooCommerce function directly like this is the reason why I cannot remove the "Add to cart" button when a user is not logged in.
To disable the "Add to cart" button, I had to call each hook manually for each type of product.
I suggest that:
```
```
is replaced by the right way of calling hooks in WooCommerce. Here is a reference to WooCommerce template hooks:
https://github.com/woocommerce/woocommerce/blob/23710744c01ded649d6a94a4eaea8745e543159f/includes/wc-template-hooks.php#L175
As of now, to be able to remove the "Add to cart" button without altering Elementor Pro, I had to call the `remove_action` twice like this for the Single Product (this might be incomplete but works in my case):
```
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
```
## Environment
System Info
```
== Server Environment ==
Operating System: Linux
Software: Apache/2.4.46 (Unix) OpenSSL/1.0.2g
MySQL version: mariadb.org binary distribution v5.6.36-82.2
PHP Version: 7.4.14
PHP Max Input Vars: 10000
PHP Max Post Size: 256M
GD Installed: Yes
ZIP Installed: Yes
Write Permissions: All right
Elementor Library: Connected
== WordPress Environment ==
Version: 5.6.1
Site URL: https://...
Home URL: https://...
WP Multisite: No
Max Upload Size: 256 MB
Memory limit: 256M
Permalink Structure: /%postname%/
Language: en-CA
Timezone: America/Toronto
Debug Mode: Active
== Theme ==
Name: Theme
Version: 1.0
Author: XXX
Child Theme: Yes
Parent Theme Name: Hello Elementor
Parent Theme Version: 2.3.1
Parent Theme Author: Elementor Team
== User ==
Role: administrator
WP Profile lang: en_CA
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
== Active Plugins ==
Advanced Custom Fields PRO
Version: 5.9.3
Author: Elliot Condon
Elementor
Version: 3.1.1
Author: Elementor.com
Elementor Pro
Version: 3.1.0
Author: Elementor.com
Gravity Forms
Version: 2.4.22
Author: Gravity Forms
Mailchimp for WooCommerce
Version: 2.5.0
Author: Mailchimp
Slider Revolution
Version: 6.3.9
Author: ThemePunch
User Switching
Version: 1.5.6
Author: John Blackbourn & contributors
Variation Swatches for WooCommerce
Version: 1.1.7
Author: Emran Ahmed
Wholesale For WooCommerce
Version: 1.5.0
Author: wpexpertsio
WooCommerce
Version: 4.9.2
Author: Automattic
WooCommerce Product Search
Version: 3.5.1
Author: itthinx
WooCommerce Stripe Gateway
Version: 4.8.0
Author: WooCommerce
WordPress Backup & Security Plugin - BlogVault
Version: 4.54
Author: Backup by BlogVault
WP All Export - User Export Add-On Pro
Version: 1.0.4
Author: Soflyy
WP All Export Pro
Version: 1.6.3
Author: Soflyy
WP All Import - User Import Add-On Pro
Version: 1.1.4
Author: Soflyy
WP All Import - WooCommerce Add-On Pro
Version: 3.2.5
Author: Soflyy
WP All Import Pro
Version: 4.6.5
Author: Soflyy
WP Image Zoom
Version: 1.45.1
Author: SilkyPress
WP Mail SMTP
Version: 2.6.0
Author: WPForms
YITH WooCommerce Ajax Product Filter Premium
Version: 3.11.7
Author: YITH
YITH WooCommerce Brands Add-on
Version: 1.4.0
Author: YITH
Yoast SEO
Version: 15.6.2
Author: Team Yoast
== Elementor - Compatibility Tag ==
Elementor Pro: compatible
== Elementor Pro - Compatibility Tag ==
== Elements Usage ==
archive : 1
archive-posts : 1
heading : 1
footer : 1
heading : 3
image : 1
text-editor : 3
wp-widget-nav_menu : 1
header : 1
icon-list : 2
image : 1
nav-menu : 1
shortcode : 2
page : 4
divider : 2
heading : 12
text-editor : 10
product : 1
accordion : 1
divider : 2
global : 1
heading : 1
text-editor : 1
woocommerce-product-add-to-cart : 1
woocommerce-product-content : 1
woocommerce-product-images : 1
woocommerce-product-price : 1
woocommerce-product-related : 1
woocommerce-product-short-description : 1
woocommerce-product-title : 1
section : 5
heading : 3
icon-list : 1
text-editor : 1
woocommerce-menu-cart : 1
woocommerce-products : 1
wp-widget-woocommerce_product_search_widget : 1
widget : 0
divider : 43
woocommerce-breadcrumb : 2
wp-widget-woocommerce_product_search_widget : 3
wp-page : 11
button : 10
divider : 7
global : 3
heading : 35
html : 1
image : 4
image-carousel : 4
image-gallery : 2
posts : 2
text-editor : 36
woocommerce-products : 3
wp-widget-rev-slider-widget : 1
single-post : 2
heading : 1
theme-post-content : 1
product-archive : 1
divider : 1
heading : 2
sidebar : 1
theme-archive-title : 1
wc-archive-products : 1
woocommerce-breadcrumb : 1
== Log ==
Log: showing 19 of 192020-11-25 14:30:56 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor',
'from' => '3.0.13',
'to' => '3.0.14',
)]
2020-11-25 14:31:04 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor Pro',
'from' => '3.0.6',
'to' => '3.0.7',
)]
2020-11-26 11:50:21 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor Pro',
'from' => '3.0.7',
'to' => '3.0.8',
)]
2021-01-04 10:34:01 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor',
'from' => '3.0.14',
'to' => '3.0.15',
)]
2021-01-04 10:34:01 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor Pro',
'from' => '3.0.8',
'to' => '3.0.9',
)]
2021-01-06 14:25:51 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor',
'from' => '3.0.15',
'to' => '3.0.16',
)]
2021-01-22 14:58:39 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor Pro',
'from' => '3.0.9',
'to' => '3.0.10',
)]
2021-02-05 15:14:49 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor',
'from' => '3.0.16',
'to' => '3.1.1',
)]
2021-02-17 10:50:04 [info] elementor-pro::elementor_pro_updater Started
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_media_carousel Start
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_media_carousel Finished
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_reviews Start
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_reviews Finished
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_testimonial_carousel Start
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_testimonial_carousel Finished
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_slides Start
2021-02-17 10:50:04 [info] Elementor Pro/Upgrades - _v_3_1_0_slides Finished
2021-02-17 10:50:04 [info] Elementor data updater process has been completed. [array (
'plugin' => 'Elementor Pro',
'from' => '3.0.10',
'to' => '3.1.0',
)]
2021-02-17 10:50:04 [info] Elementor data updater process has been queued. [array (
'plugin' => 'Elementor Pro',
'from' => '3.0.10',
'to' => '3.1.0',
)]
```
Contributor guide
Assessment
This issue has not been assessed yet.