PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.4
ShopBuilder – WooCommerce Builder For Elementor v3.2.4
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Elementor / Widgets / Single / ProductAddToCart.php

ProductAddToCart.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.4, at app/Elementor/Widgets/Single/ProductAddToCart.php

306 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main ProductDescription class.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Elementor\Widgets\Single;
9
10 use RadiusTheme\SB\Helpers\Fns;
11 use RadiusTheme\SB\Abstracts\ElementorWidgetBase;
12 use RadiusTheme\SB\Elementor\Widgets\Controls\AddToCartSettings;
13 use RadiusTheme\SBPRO\Elementor\Widgets\Controls\CatalogButtonSettings;
14 use RadiusTheme\SBPRO\Modules\CatalogMode\CatalogModeFns;
15 use RadiusTheme\SBPRO\Modules\CatalogMode\CatalogModeFrontEnd;
16
17 // Do not allow directly accessing this file.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit( 'This script cannot be accessed directly.' );
20 }
21
22 /**
23 * Product Description class
24 */
25 class ProductAddToCart extends ElementorWidgetBase {
26 /**
27 * Construct function
28 *
29 * @param array $data default array.
30 * @param mixed $args default arg.
31 */
32 public function __construct( $data = [], $args = null ) {
33 $this->rtsb_name = esc_html__( 'Product add to cart', 'shopbuilder' );
34 $this->rtsb_base = 'rtsb-product-add-to-cart';
35 parent::__construct( $data, $args );
36 }
37 /**
38 * Widget Field
39 *
40 * @return array
41 */
42 public function widget_fields() {
43 if ( Fns::is_catalog_mode() ) {
44 return CatalogButtonSettings::settings( $this );
45 } else {
46 return AddToCartSettings::widget_fields( $this );
47 }
48 }
49 /**
50 * Set Widget Keyword.
51 *
52 * @return array
53 */
54 public function get_keywords() {
55 return [ 'Cart' ] + parent::get_keywords();
56 }
57 /**
58 * Widget Field.
59 *
60 * @return void
61 */
62 public function the_hooks() {
63 $controllers = $this->get_settings_for_display();
64 add_filter( 'rtsb/module/flash_sale_countdown/show_counter', '__return_false', 99 );
65 $product = Fns::get_product();
66 $is_visible_qty = Fns::is_visible_qty_input( $product );
67
68 if ( ! empty( $controllers['quantity_style'] ) && $is_visible_qty ) {
69 if ( in_array( $controllers['quantity_style'], [ 'style-1', 'style-2' ], true ) ) {
70 add_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_start' ] );
71 add_action( 'woocommerce_after_quantity_input_field', [ $this, 'quantity_wrapper_end' ] );
72 }
73
74 if ( in_array( $controllers['quantity_style'], [ 'style-3', 'style-4' ], true ) && $is_visible_qty ) {
75 add_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_with_inner_border' ] );
76 add_action( 'woocommerce_after_quantity_input_field', [ $this, 'close_quantity_wrapper' ] );
77 }
78 }
79
80 if ( $this->is_edit_mode() ) {
81 if ( class_exists( Rtwpvs\Controllers\Hooks::class ) ) {
82 remove_filter( 'woocommerce_ajax_variationX_threshold', [ Rtwpvs\Controllers\Hooks::class, 'ajax_variation_threshold' ], 99 );
83 }
84
85 add_filter( 'woocommerce_ajax_variation_threshold', [ $this, 'override_ajax_variation_threshold' ], 99 );
86 }
87 }
88 /**
89 * Start quantity wrapper.
90 *
91 * @return void
92 */
93 public function reset_hooks() {
94 $controllers = $this->get_settings_for_display();
95 $product = Fns::get_product();
96 $is_visible_qty = Fns::is_visible_qty_input( $product );
97 if ( ! empty( $controllers['quantity_style'] ) && $is_visible_qty ) {
98 if ( in_array( $controllers['quantity_style'], [ 'style-1', 'style-2' ], true ) ) {
99 remove_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_start' ] );
100 remove_action( 'woocommerce_after_quantity_input_field', [ $this, 'quantity_wrapper_end' ] );
101 }
102
103 if ( in_array( $controllers['quantity_style'], [ 'style-3', 'style-4' ], true ) && $is_visible_qty ) {
104 remove_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_with_inner_border' ] );
105 remove_action( 'woocommerce_after_quantity_input_field', [ $this, 'close_quantity_wrapper' ] );
106 }
107 }
108 }
109 /**
110 * Start quantity wrapper.
111 *
112 * @return void
113 */
114 public function quantity_wrapper_start() {
115 $controllers = $this->get_settings_for_display();
116 ?>
117 <!-- Quantity Wrapper Start -->
118 <div class="rtsb-quantity-box-group rtsb-quantity-box-group-<?php echo esc_attr( $controllers['quantity_style'] ); ?>"">
119 <button type="button" class="rtsb-quantity-btn rtsb-quantity-minus">
120 <?php Fns::print_html( Fns::icons_manager( $controllers['decrement_icon'] ), true ); ?>
121 </button>
122 <?php
123 }
124
125 /**
126 * End quantity wrapper.
127 *
128 * @return void
129 */
130 public function quantity_wrapper_end() {
131 $controllers = $this->get_settings_for_display();
132 ?>
133 <button type="button" class="rtsb-quantity-btn rtsb-quantity-plus">
134 <?php Fns::print_html( Fns::icons_manager( $controllers['increment_icon'] ), true ); ?>
135 </button>
136 </div>
137 <!-- Quantity Wrapper End -->
138 <?php
139 }
140
141 /**
142 * Start quantity wrapper with inner border.
143 *
144 * @return void
145 */
146 public function quantity_wrapper_with_inner_border() {
147 $controllers = $this->get_settings_for_display();
148 $inner_border = ! empty( $controllers['show_inner_border'] ) ? 'show-inner-border' : '';
149 ?>
150 <!-- Quantity Wrapper Start -->
151 <div class="rtsb-quantity-box-group rtsb-quantity-box-group-<?php echo esc_attr( $controllers['quantity_style'] . ' ' . $inner_border ); ?>">
152 <div class="rtsb-qty-btns-group">
153 <button type="button" class="rtsb-quantity-btn rtsb-quantity-plus">
154 <?php Fns::print_html( Fns::icons_manager( $controllers['increment_icon'] ), true ); ?>
155 </button>
156 <button type="button" class="rtsb-quantity-btn rtsb-quantity-minus">
157 <?php Fns::print_html( Fns::icons_manager( $controllers['decrement_icon'] ), true ); ?>
158 </button>
159 </div>
160 <?php
161 }
162
163 /**
164 * Close quantity wrapper.
165 *
166 * @return void
167 */
168 public function close_quantity_wrapper() {
169 ?>
170 </div>
171 <!-- Quantity Wrapper End -->
172 <?php
173 }
174 /**
175 * Render catalog mode contact buttons
176 *
177 * @param int $product_id Product ID.
178 * @return void
179 */
180 public function catalog_mode_buttons( $product_id ) {
181 $settings = CatalogModeFns::get_options();
182 $contact_buttons = $settings['contact_buttons'] ?? [];
183 if ( empty( $contact_buttons ) || ! is_array( $contact_buttons ) ) {
184 return;
185 }
186
187 echo '<div class="rtsb-product-add-to-cart">';
188 echo '<div class="rtsb-catalog-mode-product-page-buttons">';
189
190 foreach ( $contact_buttons as $button_type ) {
191 $this->render_contact_button( $button_type, $product_id, $settings );
192 }
193 echo '</div>';
194 echo '</div>';
195 }
196 /**
197 * Render specific contact button
198 *
199 * @param string $button_type Button type.
200 * @param int $product_id Product ID.
201 * @param array $settings Plugin settings.
202 * @return void
203 */
204 public function render_contact_button( $button_type, $product_id, $settings ) {
205 switch ( $button_type ) {
206 case 'custom':
207 CatalogModeFrontEnd::render_custom_button( $product_id, $settings );
208 break;
209 case 'whatsapp':
210 CatalogModeFrontEnd::render_whatsapp_button( $product_id, $settings );
211 break;
212 case 'call':
213 CatalogModeFrontEnd::render_call_button( $settings );
214 break;
215 case 'email':
216 CatalogModeFrontEnd::render_email_button( $settings );
217 break;
218 case 'inquiry':
219 CatalogModeFrontEnd::render_inquiry_button( $product_id, $settings );
220 break;
221 }
222 }
223
224 /**
225 * Override WooCommerce AJAX variation threshold.
226 *
227 * @return int
228 */
229 public function override_ajax_variation_threshold() {
230 return 1;
231 }
232
233 /**
234 * Elementor Edit mode need some extra js for isotop reinitialize
235 *
236 * @return void
237 */
238 public function editor_script() {
239 if ( $this->is_edit_mode() ) {
240 ?>
241 <script type="text/javascript">
242 function addToCartAction() {
243 var singleAddToCart = jQuery('.rtsb-product-add-to-cart');
244
245 if (singleAddToCart.length > 0) {
246 singleAddToCart
247 .find(
248 '.stock.available-on-pre-order, .stock.available-on-backorder'
249 )
250 .remove();
251 }
252 }
253 if (typeof elementorFrontend !== 'undefined') {
254 elementorFrontend.hooks.addAction(
255 'frontend/element_ready/rtsb-product-add-to-cart.default',
256 () => {
257 addToCartAction();
258 }
259 );
260 }
261 </script>
262 <?php
263 }
264 }
265
266 /**
267 * Render Function
268 *
269 * @return void
270 */
271 protected function render() {
272 global $product;
273 $_product = $product;
274 $product = Fns::get_product();
275 $controllers = $this->get_settings_for_display();
276 $controllers['cart_icon_html'] = Fns::icons_manager( $controllers['cart_icon'] );
277 $add_to_cart_visibility = rtsb()->has_pro() && Fns::is_guest_feature_disabled( 'hide_add_to_cart', '' );
278 $controllers['visibility'] = $add_to_cart_visibility ? ' rtsb-not-visible' : ' rtsb-is-visible';
279
280 $this->the_hooks();
281 $this->theme_support();
282
283 if ( rtsb()->has_pro() && ! empty( $controllers['enable_single_ajax_add_to_cart'] ) ) {
284 wp_enqueue_script( 'wc-add-to-cart' );
285 }
286
287 $data = [
288 'template' => 'elementor/single-product/add-to-cart',
289 'controllers' => $controllers,
290 'product_type' => $product ? $product->get_type() : '',
291 ];
292 if ( $this->is_builder_mode() && Fns::is_catalog_mode() ) {
293 $this->catalog_mode_buttons( $product->get_id() );
294 } else {
295 Fns::load_template( $data['template'], $data );
296 }
297
298 $this->editor_cart_icon_script();
299 add_filter( 'rtsb/module/flash_sale_countdown/show_counter', '__return_true', 99 );
300 $this->reset_hooks();
301 $this->theme_support( 'render_reset' );
302 $this->editor_script();
303 $product = $_product; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
304 }
305 }
306