PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 1.0.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v1.0.0
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / modules / swatches / frontend.php
shopengine / modules / swatches Last commit date
assets 5 years ago admin-product.php 5 years ago attribute-hooks.php 5 years ago frontend.php 5 years ago helper.php 5 years ago swatches.php 5 years ago
frontend.php
145 lines
1 <?php
2
3 namespace ShopEngine\Modules\Swatches;
4
5 use ShopEngine\Traits\Singleton;
6
7 defined('ABSPATH') || exit;
8
9 class Frontend
10 {
11
12 use Singleton;
13
14 public function init() {
15
16 add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
17 add_filter('woocommerce_dropdown_variation_attribute_options_html', [$this, 'get_swatch_html'], 100, 2);
18 add_filter('shopengine_filter_html_swatch_hook', [$this, 'swatch_html'], 5, 4);
19 }
20
21
22 public function enqueue_scripts() {
23 wp_enqueue_style('shopengine-css-front', Swatches::asset_source('css', 'frontend.css'), [], Swatches::MODULE_VERSION);
24 wp_enqueue_script('shopengine-js-front', Swatches::asset_source('js', 'frontend.js'), ['jquery'], Swatches::MODULE_VERSION, true);
25 }
26
27
28 public function get_swatch_html($html, $args) {
29
30 $swatch_types = Swatches::instance()->get_available_types();
31
32 $attr = Helper::get_tax_attribute($args['attribute']);
33
34
35 // Return if this is normal attribute
36 if(empty($attr)) {
37 return $html;
38 }
39
40
41 if(!array_key_exists($attr->attribute_type, $swatch_types)) {
42 return $html;
43 }
44
45
46 $options = $args['options'];
47 $product = $args['product'];
48 $attribute = $args['attribute'];
49 $class = "variation-selector variation-select-{$attr->attribute_type}";
50 $swatches = '';
51
52 $args['tooltip'] = $this->is_tooltip_enabled();
53
54 if(empty($options) && !empty($product) && !empty($attribute)) {
55 $attributes = $product->get_variation_attributes();
56 $options = $attributes[$attribute];
57 }
58
59 if(array_key_exists($attr->attribute_type, $swatch_types)) {
60 if(!empty($options) && $product && taxonomy_exists($attribute)) {
61 $terms = wc_get_product_terms($product->get_id(), $attribute, ['fields' => 'all']);
62
63 foreach($terms as $term) {
64 if(in_array($term->slug, $options)) {
65 $swatches .= apply_filters('shopengine_filter_html_swatch_hook', '', $term, $attr->attribute_type, $args);
66 }
67 }
68 }
69
70 if(!empty($swatches)) {
71 $class .= ' hidden';
72 $swatches = '<div class="shopengine_swatches" data-attribute_name="attribute_' . esc_attr($attribute) . '">' . $swatches . '</div>';
73 $html = '<div class="' . esc_attr($class) . '">' . $html . '</div>' . $swatches;
74 }
75 }
76
77 return $html;
78 }
79
80
81 public function swatch_html($html, $term, $type, $args) {
82
83 $selected = sanitize_title($args['selected']) == $term->slug ? 'selected' : '';
84 $name = esc_html(apply_filters('woocommerce_variation_option_name', $term->name));
85 $tooltip = '';
86
87 if(!empty($args['tooltip'])) {
88 $tooltip = '<span class="shopengine_swatch__tooltip">' . ($term->description ? $term->description : $name) . '</span>';
89 }
90
91 switch($type) {
92 case Swatches::PA_COLOR:
93 $color = get_term_meta($term->term_id, $type, true);
94 list($r, $g, $b) = sscanf($color, "#%02x%02x%02x");
95 $html = sprintf(
96 '<span class="swatch swatch_color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s%s</span>',
97 esc_attr($term->slug),
98 $selected,
99 esc_attr($color),
100 "rgba($r,$g,$b,0.5)",
101 esc_attr($term->slug),
102 $name,
103 $tooltip
104 );
105 break;
106
107 case Swatches::PA_IMAGE:
108 $image = get_term_meta($term->term_id, $type, true);
109 $image = $image ? wp_get_attachment_image_src($image) : '';
110 $image = $image ? $image[0] : Helper::get_dummy();
111 $html = sprintf(
112 '<span class="swatch swatch_image swatch-%s %s" data-value="%s"><img src="%s" alt="%s">%s%s</span>',
113 esc_attr($term->slug),
114 $selected,
115 esc_attr($term->slug),
116 esc_url($image),
117 esc_attr($name),
118 $name,
119 $tooltip
120 );
121 break;
122
123 case Swatches::PA_LABEL:
124 $label = get_term_meta($term->term_id, $type, true);
125 $label = $label ? $label : $name;
126 $html = sprintf(
127 '<span class="swatch swatch_label swatch-%s %s" data-value="%s">%s%s</span>',
128 esc_attr($term->slug),
129 $selected,
130 esc_attr($term->slug),
131 esc_html($label),
132 $tooltip
133 );
134 break;
135 }
136
137 return $html;
138 }
139
140
141 public function is_tooltip_enabled() {
142
143 return true;
144 }
145 }