-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjet-woo-builder-custom-add-to-cart-icon.php
385 lines (323 loc) · 13.4 KB
/
jet-woo-builder-custom-add-to-cart-icon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Plugin Name: JetWooBuilder - Custom Add to Cart Icon
* Plugin URI:
* Description:
* Version: 1.0.1
* Author: Crocoblock
* Author URI: https://crocoblock.com/
* Text Domain: jet-woo-builder
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Domain Path: /languages
*/
use Elementor\Controls_Manager;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die();
}
class Jet_Woo_Builder_Custom_Add_To_Cart_Icon {
public $quantity = '';
public $icon = '';
public function __construct() {
define( 'JET_CATCI__FILE__', __FILE__ );
// enqueue plugin scripts and styles
add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'jet_woo_catci_scripts' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'jet_woo_catci_scripts' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'jet_woo_catci_styles' ] );
// register controls for Products Grid widget
add_action( 'elementor/element/jet-woo-products/section_general/after_section_end', [ $this, 'register_custom_add_to_cart_icon_controls' ], 10, 2 );
add_action( 'elementor/element/jet-woo-products/section_not_found_message_style/after_section_end', [ $this, 'register_custom_add_to_cart_icon_style_controls' ], 10, 2 );
// register controls for Products List widget
add_action( 'elementor/element/jet-woo-products-list/section_general/after_section_end', [ $this, 'register_custom_add_to_cart_icon_controls' ], 10, 2 );
add_action( 'elementor/element/jet-woo-products-list/section_not_found_message_style/after_section_end', [ $this, 'register_custom_add_to_cart_icon_style_controls' ], 10, 2 );
// register controls for Archive Add to Cart widget
add_action( 'elementor/element/jet-woo-builder-archive-add-to-cart/section_archive_add_to_cart_content/after_section_end', [ $this, 'register_custom_add_to_cart_icon_controls' ], 10, 2 );
add_action( 'elementor/element/jet-woo-builder-archive-add-to-cart/section_archive_add_to_cart_style/after_section_end', [ $this, 'register_custom_add_to_cart_icon_style_controls' ], 10, 2 );
// register controls for Single Add to Cart widget
add_action( 'elementor/element/jet-single-add-to-cart/section_add_to_cart_style/before_section_start', [ $this, 'register_custom_add_to_cart_icon_controls' ], 10, 2 );
add_action( 'elementor/element/jet-single-add-to-cart/section_add_to_cart_style/after_section_end', [ $this, 'register_custom_add_to_cart_icon_style_controls' ], 10, 2 );
// register controls for Wishlist widget
add_action( 'elementor/element/jet-wishlist/section_add_to_cart_style/before_section_start', [ $this, 'register_custom_add_to_cart_icon_controls' ], 10, 2 );
add_action( 'elementor/element/jet-wishlist/section_add_to_cart_style/after_section_end', [ $this, 'register_custom_add_to_cart_icon_style_controls' ], 10, 2 );
// handle custom icon settings
add_filter( 'jet-woo-builder/jet-woo-products-grid/settings', [ $this, 'get_products_grid_icon_settings' ], 10, 2 );
add_filter( 'jet-woo-builder/jet-woo-products-list/settings', [ $this, 'get_products_grid_icon_settings' ], 10, 2 );
add_filter( 'jet-woo-builder/jet-woo-archive-add-to-cart/settings', [ $this, 'get_products_grid_icon_settings' ], 10, 2 );
add_filter( 'jet-woo-builder/jet-woo-single-add-to-cart/settings', [ $this, 'get_products_grid_icon_settings' ], 10, 2 );
// trigger widget settings
add_action( 'jet-woo-builder/templates/jet-woo-products/custom-button-icon', [ $this, 'trigger_products_grid_settings' ] );
add_action( 'jet-woo-builder/templates/jet-woo-products-list/custom-button-icon', [ $this, 'trigger_products_grid_settings' ] );
// rewrite add to cart loop template
add_filter( 'woocommerce_loop_add_to_cart_link', [ $this, 'custom_add_to_cart_icon_for_woocommerce_loop_add_to_cart_link' ], 20, 3 );
// add wrapper for single add to cart button
add_action( 'woocommerce_after_add_to_cart_quantity', [ $this, 'open_wrapper_for_single_add_to_cart_button_with_custom_icon' ] );
add_action( 'woocommerce_after_add_to_cart_button', [ $this, 'close_wrapper_for_single_add_to_cart_button_with_custom_icon' ] );
add_action( 'woocommerce_before_add_to_cart_button', [ $this, 'trigger_open_wrapper_method_for_grouped_product' ] );
// add custom add to cart icon settings to providers settings list
add_filter( 'jet-smart-filters/providers/jet-woo-products-grid/settings-list', [ $this, 'add_custom_add_to_cart_icon_settings_to_list' ] );
add_filter( 'jet-smart-filters/providers/jet-woo-products-list/settings-list', [ $this, 'add_custom_add_to_cart_icon_settings_to_list' ] );
}
/**
* Enqueue plugin styles
*/
public function jet_woo_catci_styles() {
wp_enqueue_style( 'jet-woo-catci-styles', plugins_url( '/assets/css/styles.min.css', JET_CATCI__FILE__ ) );
}
/**
*
*/
public function jet_woo_catci_scripts() {
wp_enqueue_script( 'jet-woo-catci-scripts', plugins_url( '/assets/js/scripts.js', JET_CATCI__FILE__ ), [ 'jquery' ], '1.0.1', true );
}
/**
* Register custom Add to Cart icon controls
*
* @param $obj
*/
public function register_custom_add_to_cart_icon_controls( $obj ) {
$obj->start_controls_section(
'section_custom_add_to_cart_icon',
[
'label' => esc_html__( 'Custom Add to Cart Icon', 'jet-woo-builder' ),
]
);
$obj->add_control(
'enable_custom_add_to_cart_icon',
[
'label' => esc_html__( 'Enable Custom Icon', 'jet-woo-builder' ),
'type' => Controls_Manager::SWITCHER,
'frontend_available' => 'jet-wishlist' === $obj->get_name(),
]
);
$obj->__add_advanced_icon_control(
'custom_add_to_cart_icon',
[
'label' => esc_html__( 'Select Icon', 'jet-woo-builder' ),
'type' => Controls_Manager::ICON,
'label_block' => true,
'file' => '',
'default' => 'fa fa-shopping-cart',
'fa5_default' => [
'value' => 'fas fa-shopping-cart',
'library' => 'fa-solid',
],
'condition' => [
'enable_custom_add_to_cart_icon' => 'yes',
],
'frontend_available' => 'jet-wishlist' === $obj->get_name(),
]
);
$obj->add_control(
'icon_align',
[
'label' => esc_html__( 'Icon Position', 'jet-woo-builder' ),
'type' => Controls_Manager::SELECT,
'default' => 'row',
'options' => [
'row' => esc_html__( 'Before', 'jet-woo-builder' ),
'row-reverse' => esc_html__( 'After', 'jet-woo-builder' ),
],
'selectors' => [
'{{WRAPPER}} .jet-woo-button-content' => 'flex-direction: {{VALUE}};',
],
'condition' => [
'enable_custom_add_to_cart_icon' => 'yes',
],
]
);
$obj->add_control(
'icon_indent',
[
'label' => esc_html__( 'Icon Spacing', 'jet-woo-builder' ),
'type' => Controls_Manager::SLIDER,
'range' => [
'px' => [
'max' => 50,
],
],
'default' => [
'unit' => 'px',
'size' => 10,
],
'selectors' => [
'{{WRAPPER}} .jet-woo-button-content' => 'gap: {{SIZE}}{{UNIT}};',
],
'condition' => [
'enable_custom_add_to_cart_icon' => 'yes',
],
]
);
$obj->end_controls_section();
}
/**
* Register custom Add to Cart icon style controls
*
* @param $obj
*/
public function register_custom_add_to_cart_icon_style_controls( $obj ) {
$obj->start_controls_section(
'section_style_custom_icon',
[
'label' => esc_html__( 'Custom Icon', 'jet-woo-builder' ),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$obj->add_control(
'custom_icon_color',
[
'label' => esc_html__( 'Color', 'jet-woo-builder' ),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .jet-woo-button-content .button-icon' => 'color: {{VALUE}};',
'{{WRAPPER}} .jet-woo-button-content .button-icon svg' => 'fill: {{VALUE}};',
],
]
);
$obj->add_control(
'custom_icon_hover_color',
[
'label' => esc_html__( 'Hover Color', 'jet-woo-builder' ),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} a:hover .jet-woo-button-content .button-icon' => 'color: {{VALUE}};',
'{{WRAPPER}} a:hover .jet-woo-button-content .button-icon svg' => 'fill: {{VALUE}};',
'{{WRAPPER}} button:hover .jet-woo-button-content .button-icon' => 'color: {{VALUE}};',
'{{WRAPPER}} button:hover .jet-woo-button-content .button-icon svg' => 'fill: {{VALUE}};',
],
]
);
$obj->add_responsive_control(
'custom_icon_size',
[
'label' => esc_html__( 'Size', 'jet-woo-builder' ),
'type' => Controls_Manager::SLIDER,
'range' => [
'px' => [
'min' => 6,
'max' => 300,
],
],
'selectors' => [
'{{WRAPPER}} .jet-woo-button-content .button-icon' => 'font-size: {{SIZE}}{{UNIT}};',
],
'separator' => 'before',
]
);
$obj->end_controls_section();
}
/**
* Adding Add to Cart button custom icon settings.
*
* @param $settings
* @param $widget
*
* @return mixed
*/
public function get_products_grid_icon_settings( $settings, $widget ) {
if ( ! empty( $settings['enable_custom_add_to_cart_icon'] ) ) {
if ( isset( $settings['selected_custom_add_to_cart_icon'] ) || isset( $settings['custom_add_to_cart_icon'] ) ) {
$settings['selected_custom_add_to_cart_icon'] = htmlspecialchars( $widget->__render_icon( 'custom_add_to_cart_icon', '%s', '', false ) );
}
if ( is_a( $widget, 'Elementor\Jet_Woo_Builder_Archive_Add_To_Cart' ) || is_a( $widget, 'Elementor\Jet_Woo_Builder_Single_Add_To_Cart' ) ) {
if ( isset( $settings['show_quantity'] ) ) {
$this->quantity = 'yes' === $settings['show_quantity'];
}
$this->icon = htmlspecialchars_decode( $settings['selected_custom_add_to_cart_icon'] );
}
return $settings;
}
}
/**
* Set global variables for wc hook handling.
*
* @param $settings
*/
public function trigger_products_grid_settings( $settings ) {
$this->quantity = 'yes' === $settings['show_quantity'];
$this->icon = htmlspecialchars_decode( $settings['selected_custom_add_to_cart_icon'] );
}
/**
* Override loop add to cart template and show icon next to add to cart label
*
* @param $html
* @param $product
* @param $args
*
* @return string
*/
public function custom_add_to_cart_icon_for_woocommerce_loop_add_to_cart_link( $html, $product, $args ) {
if ( $product && ( $product->is_type( 'simple' ) || $product->is_type( 'variation' ) ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
if ( $this->quantity && $this->icon ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( [
'min_value' => $product->get_min_purchase_quantity(),
'max_value' => $product->get_max_purchase_quantity(),
], $product, false );
$html .= '<div class="jet-woo-btn-with-custom-icon-wrapper">';
$html .= '<button type="submit" class="alt ' . $args['class'] . '" data-product_id="' . $product->get_id() . '" data-quantity="1"><span class="jet-woo-button-content"><span class="button-icon">' . $this->icon . '</span><span class="button-label">' . esc_html( $product->add_to_cart_text() ) . '</span></span></button>';
$html .= '</div>';
$html .= '</form>';
} elseif ( $this->icon ) {
$html = sprintf(
'<div class="jet-woo-btn-with-custom-icon-wrapper"><a href="%s" data-quantity="%s" class="%s" %s><span class="jet-woo-button-content"><span class="button-icon">' . $this->icon . '</span><span class="button-label">%s</span></span></a></div>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() )
);
}
}
return $html;
}
/**
* Add open wrapper for single add to cart button if it has custom icon
*/
public function open_wrapper_for_single_add_to_cart_button_with_custom_icon() {
global $product;
$content = sprintf(
'<span class="jet-woo-button-content"><span class="button-icon">%s</span><span class="button-label">%s</span></span>',
$this->icon,
apply_filters( 'woocommerce_product_single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product )
);
if ( $this->icon ) {
echo '<div class="jet-woo-btn-with-custom-icon-wrapper" data-content="' . htmlspecialchars( $content ) . '">';
}
}
/**
* Add close wrapper for single add to cart button if it has custom icon
*/
public function close_wrapper_for_single_add_to_cart_button_with_custom_icon() {
if ( $this->icon ) {
echo '</div>';
}
}
/**
* Check if product is grouped and trigger open wrapper method for single product add to cart button.
*/
public function trigger_open_wrapper_method_for_grouped_product() {
global $product;
$_product = wc_get_product( $product->get_id() );
if ( $_product->is_type( 'grouped' ) ) {
$this->open_wrapper_for_single_add_to_cart_button_with_custom_icon();
}
}
/**
* Returns merged custom icon settings with JetSmartFilters providers settings list
*
* @param $list
*
* @return array
*/
public function add_custom_add_to_cart_icon_settings_to_list( $list ) {
$custom_icon_settings = [
'show_quantity',
'enable_custom_add_to_cart_icon',
'custom_add_to_cart_icon',
'selected_custom_add_to_cart_icon',
];
return array_merge( $list, $custom_icon_settings );
}
}
new Jet_Woo_Builder_Custom_Add_To_Cart_Icon();