PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.1
ShopBuilder – WooCommerce Builder For Elementor v3.2.1
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.1, at app/Elementor/Widgets/Single/ProductAddToCart.php

247 lines 7.3 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
14 // Do not allow directly accessing this file.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 'This script cannot be accessed directly.' );
17 }
18
19 /**
20 * Product Description class
21 */
22 class ProductAddToCart extends ElementorWidgetBase {
23 /**
24 * Construct function
25 *
26 * @param array $data default array.
27 * @param mixed $args default arg.
28 */
29 public function __construct( $data = [], $args = null ) {
30 $this->rtsb_name = esc_html__( 'Product add to cart', 'shopbuilder' );
31 $this->rtsb_base = 'rtsb-product-add-to-cart';
32 parent::__construct( $data, $args );
33 }
34 /**
35 * Widget Field
36 *
37 * @return array
38 */
39 public function widget_fields() {
40 return AddToCartSettings::widget_fields( $this );
41 }
42 /**
43 * Set Widget Keyword.
44 *
45 * @return array
46 */
47 public function get_keywords() {
48 return [ 'Cart' ] + parent::get_keywords();
49 }
50 /**
51 * Widget Field.
52 *
53 * @return void
54 */
55 public function the_hooks() {
56 $controllers = $this->get_settings_for_display();
57 add_filter( 'rtsb/module/flash_sale_countdown/show_counter', '__return_false', 99 );
58 $product = Fns::get_product();
59 $is_visible_qty = Fns::is_visible_qty_input( $product );
60
61 if ( ! empty( $controllers['quantity_style'] ) && $is_visible_qty ) {
62 if ( in_array( $controllers['quantity_style'], [ 'style-1', 'style-2' ], true ) ) {
63 add_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_start' ] );
64 add_action( 'woocommerce_after_quantity_input_field', [ $this, 'quantity_wrapper_end' ] );
65 }
66
67 if ( in_array( $controllers['quantity_style'], [ 'style-3', 'style-4' ], true ) && $is_visible_qty ) {
68 add_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_with_inner_border' ] );
69 add_action( 'woocommerce_after_quantity_input_field', [ $this, 'close_quantity_wrapper' ] );
70 }
71 }
72
73 if ( $this->is_edit_mode() ) {
74 if ( class_exists( Rtwpvs\Controllers\Hooks::class ) ) {
75 remove_filter( 'woocommerce_ajax_variationX_threshold', [ Rtwpvs\Controllers\Hooks::class, 'ajax_variation_threshold' ], 99 );
76 }
77
78 add_filter( 'woocommerce_ajax_variation_threshold', [ $this, 'override_ajax_variation_threshold' ], 99 );
79 }
80 }
81 /**
82 * Start quantity wrapper.
83 *
84 * @return void
85 */
86 public function reset_hooks() {
87 $controllers = $this->get_settings_for_display();
88 $product = Fns::get_product();
89 $is_visible_qty = Fns::is_visible_qty_input( $product );
90 if ( ! empty( $controllers['quantity_style'] ) && $is_visible_qty ) {
91 if ( in_array( $controllers['quantity_style'], [ 'style-1', 'style-2' ], true ) ) {
92 remove_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_start' ] );
93 remove_action( 'woocommerce_after_quantity_input_field', [ $this, 'quantity_wrapper_end' ] );
94 }
95
96 if ( in_array( $controllers['quantity_style'], [ 'style-3', 'style-4' ], true ) && $is_visible_qty ) {
97 remove_action( 'woocommerce_before_quantity_input_field', [ $this, 'quantity_wrapper_with_inner_border' ] );
98 remove_action( 'woocommerce_after_quantity_input_field', [ $this, 'close_quantity_wrapper' ] );
99 }
100 }
101 }
102 /**
103 * Start quantity wrapper.
104 *
105 * @return void
106 */
107 public function quantity_wrapper_start() {
108 $controllers = $this->get_settings_for_display();
109 ?>
110 <!-- Quantity Wrapper Start -->
111 <div class="rtsb-quantity-box-group rtsb-quantity-box-group-<?php echo esc_attr( $controllers['quantity_style'] ); ?>"">
112 <button type="button" class="rtsb-quantity-btn rtsb-quantity-minus">
113 <?php Fns::print_html( Fns::icons_manager( $controllers['decrement_icon'] ), true ); ?>
114 </button>
115 <?php
116 }
117
118 /**
119 * End quantity wrapper.
120 *
121 * @return void
122 */
123 public function quantity_wrapper_end() {
124 $controllers = $this->get_settings_for_display();
125 ?>
126 <button type="button" class="rtsb-quantity-btn rtsb-quantity-plus">
127 <?php Fns::print_html( Fns::icons_manager( $controllers['increment_icon'] ), true ); ?>
128 </button>
129 </div>
130 <!-- Quantity Wrapper End -->
131 <?php
132 }
133
134 /**
135 * Start quantity wrapper with inner border.
136 *
137 * @return void
138 */
139 public function quantity_wrapper_with_inner_border() {
140 $controllers = $this->get_settings_for_display();
141 $inner_border = ! empty( $controllers['show_inner_border'] ) ? 'show-inner-border' : '';
142 ?>
143 <!-- Quantity Wrapper Start -->
144 <div class="rtsb-quantity-box-group rtsb-quantity-box-group-<?php echo esc_attr( $controllers['quantity_style'] . ' ' . $inner_border ); ?>">
145 <div class="rtsb-qty-btns-group">
146 <button type="button" class="rtsb-quantity-btn rtsb-quantity-plus">
147 <?php Fns::print_html( Fns::icons_manager( $controllers['increment_icon'] ), true ); ?>
148 </button>
149 <button type="button" class="rtsb-quantity-btn rtsb-quantity-minus">
150 <?php Fns::print_html( Fns::icons_manager( $controllers['decrement_icon'] ), true ); ?>
151 </button>
152 </div>
153 <?php
154 }
155
156 /**
157 * Close quantity wrapper.
158 *
159 * @return void
160 */
161 public function close_quantity_wrapper() {
162 ?>
163 </div>
164 <!-- Quantity Wrapper End -->
165 <?php
166 }
167
168 /**
169 * Override WooCommerce AJAX variation threshold.
170 *
171 * @return int
172 */
173 public function override_ajax_variation_threshold() {
174 return 1;
175 }
176
177 /**
178 * Elementor Edit mode need some extra js for isotop reinitialize
179 *
180 * @return void
181 */
182 public function editor_script() {
183 if ( $this->is_edit_mode() ) {
184 ?>
185 <script type="text/javascript">
186 function addToCartAction() {
187 var singleAddToCart = jQuery('.rtsb-product-add-to-cart');
188
189 if (singleAddToCart.length > 0) {
190 singleAddToCart
191 .find(
192 '.stock.available-on-pre-order, .stock.available-on-backorder'
193 )
194 .remove();
195 }
196 }
197 if (typeof elementorFrontend !== 'undefined') {
198 elementorFrontend.hooks.addAction(
199 'frontend/element_ready/rtsb-product-add-to-cart.default',
200 () => {
201 addToCartAction();
202 }
203 );
204 }
205 </script>
206 <?php
207 }
208 }
209
210 /**
211 * Render Function
212 *
213 * @return void
214 */
215 protected function render() {
216 global $product;
217 $_product = $product;
218 $product = Fns::get_product();
219 $controllers = $this->get_settings_for_display();
220 $controllers['cart_icon_html'] = Fns::icons_manager( $controllers['cart_icon'] );
221 $add_to_cart_visibility = rtsb()->has_pro() && Fns::is_guest_feature_disabled( 'hide_add_to_cart', '' );
222 $controllers['visibility'] = $add_to_cart_visibility ? ' rtsb-not-visible' : ' rtsb-is-visible';
223
224 $this->the_hooks();
225 $this->theme_support();
226
227 if ( rtsb()->has_pro() && ! empty( $controllers['enable_single_ajax_add_to_cart'] ) ) {
228 wp_enqueue_script( 'wc-add-to-cart' );
229 }
230
231 $data = [
232 'template' => 'elementor/single-product/add-to-cart',
233 'controllers' => $controllers,
234 'product_type' => $product ? $product->get_type() : '',
235 ];
236
237 Fns::load_template( $data['template'], $data );
238
239 $this->editor_cart_icon_script();
240 add_filter( 'rtsb/module/flash_sale_countdown/show_counter', '__return_true', 99 );
241 $this->reset_hooks();
242 $this->theme_support( 'render_reset' );
243 $this->editor_script();
244 $product = $_product; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
245 }
246 }
247