assets
3 years ago
loop-product-support
3 years ago
admin-product.php
3 years ago
attribute-hooks.php
3 years ago
frontend.php
3 years ago
helper.php
3 years ago
swatches.php
3 years ago
frontend.php
267 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Swatches; |
| 4 | |
| 5 | use ShopEngine; |
| 6 | use ShopEngine\Traits\Singleton; |
| 7 | use ShopEngine\Core\Register\Module_List; |
| 8 | |
| 9 | defined('ABSPATH') || exit; |
| 10 | |
| 11 | class Frontend |
| 12 | { |
| 13 | use Singleton; |
| 14 | |
| 15 | public function init() { |
| 16 | |
| 17 | $sett = Module_List::instance()->get_settings('swatches'); |
| 18 | |
| 19 | // Todo: Need to remove the old codes of swatches |
| 20 | add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 21 | add_filter('woocommerce_dropdown_variation_attribute_options_html', [$this, 'get_swatch_html'], 100, 2); |
| 22 | add_filter('shopengine_filter_html_swatch_hook', [$this, 'swatch_html'], 5, 4); |
| 23 | |
| 24 | if(isset($sett['show_color_swatch_on_loop']['value']) && $sett['show_color_swatch_on_loop']['value'] === 'yes'){ |
| 25 | add_action('woocommerce_after_shop_loop_item', [$this, 'show_archive_product_loop_swatch'], 15); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | add_action('wp_ajax_shopengine_swatch_image_on_loop_products', [$this, 'swatch_image_on_loop_products']); |
| 30 | add_action("wp_ajax_nopriv_shopengine_swatch_image_on_loop_products", [$this, "swatch_image_on_loop_products"]); |
| 31 | } |
| 32 | |
| 33 | public function enqueue_scripts() { |
| 34 | wp_enqueue_style('shopengine-css-front', Swatches::asset_source('css', 'frontend.css'), [], ShopEngine::version()); |
| 35 | wp_enqueue_script('shopengine-js-front', Swatches::asset_source('js', 'frontend.js'), ['jquery'], ShopEngine::version(), true); |
| 36 | |
| 37 | wp_localize_script('shopengine-js-front', 'frontendApiSettings', [ |
| 38 | 'nonce' => esc_js(wp_create_nonce('shopengine_swatch_image_on_loop_products')), |
| 39 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 40 | ]); |
| 41 | } |
| 42 | |
| 43 | public function show_archive_product_loop_swatch(){ |
| 44 | global $product; |
| 45 | |
| 46 | if($product->get_type() === 'variable'){ |
| 47 | |
| 48 | $attributes = $product->get_attributes(); |
| 49 | |
| 50 | if(!empty($attributes)){ |
| 51 | $output_div = '<div class="shopengine_loop_swatches_wrap" data-product-id="'. esc_attr($product->get_id()) .'">'; |
| 52 | echo wp_kses($output_div, \ShopEngine\Utils\Helper::get_kses_array()); |
| 53 | foreach($attributes as $attr_key => $attribute){ |
| 54 | |
| 55 | $current_attr_key = Helper::get_tax_attribute($attr_key); |
| 56 | if(isset($current_attr_key->attribute_type) && $current_attr_key->attribute_type === 'shopengine_color'){ |
| 57 | |
| 58 | $selected_attr = $product->get_variation_default_attribute( $attr_key ); |
| 59 | $output_div = '<div class="shopengine_swatches shopengine-loop-swatches" data-attribute_name="attribute_'. esc_attr($attr_key) .'">'; |
| 60 | echo wp_kses($output_div, \ShopEngine\Utils\Helper::get_kses_array()); |
| 61 | |
| 62 | if(!empty($attribute->get_terms())){ |
| 63 | foreach($attribute->get_terms() as $attr_option){ |
| 64 | |
| 65 | $name = apply_filters('woocommerce_variation_option_name', $attr_option->name); |
| 66 | $current_selected = $selected_attr === $attr_option->slug ? 'selected' : ''; |
| 67 | |
| 68 | $color = get_term_meta($attr_option->term_id, 'shopengine_color', true); |
| 69 | list($r, $g, $b) = sscanf($color, "#%02x%02x%02x"); |
| 70 | |
| 71 | echo sprintf( |
| 72 | '<span class="swatch swatch_color_loop swatch_color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s</span>', |
| 73 | esc_attr($attr_option->slug), |
| 74 | esc_attr($current_selected), |
| 75 | esc_attr($color), |
| 76 | esc_attr("rgba($r,$g,$b,0.5)"), |
| 77 | esc_attr($attr_option->slug), |
| 78 | esc_html($name), |
| 79 | ); |
| 80 | |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | echo wp_kses('</div>', \ShopEngine\Utils\Helper::get_kses_array()); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | echo wp_kses('</div>', \ShopEngine\Utils\Helper::get_kses_array()); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function swatch_image_on_loop_products(){ |
| 95 | |
| 96 | $nonce = isset($_POST['nonce']) ? sanitize_key($_POST['nonce']) : ''; |
| 97 | |
| 98 | $selectedData = isset($_POST['selectedData']) ? map_deep( wp_unslash( $_POST['selectedData'] ) , 'sanitize_text_field' ) : ''; |
| 99 | $product_id = isset($_POST['productId']) ? sanitize_text_field( wp_unslash($_POST['productId']) ) : ''; |
| 100 | |
| 101 | if(!wp_verify_nonce($nonce, 'shopengine_swatch_image_on_loop_products')) { |
| 102 | wp_send_json_error(esc_html__('Request denied', 'shopengine')); |
| 103 | } |
| 104 | |
| 105 | $product = wc_get_product($product_id); |
| 106 | $variations = $product->get_available_variations(); |
| 107 | |
| 108 | if(!empty($variations)){ |
| 109 | foreach($variations as $variation){ |
| 110 | $attributes = $variation['attributes']; |
| 111 | $variation_match_found = false; |
| 112 | |
| 113 | foreach( $attributes as $attr_key => $value ){ |
| 114 | |
| 115 | $current_attr_key = Helper::get_tax_attribute(ltrim($attr_key, 'attribute_')); |
| 116 | |
| 117 | if(isset($current_attr_key->attribute_type) && $current_attr_key->attribute_type === 'shopengine_color'){ |
| 118 | |
| 119 | $attribute_match_found = false; |
| 120 | |
| 121 | // Variation has given value |
| 122 | if(!empty($value)){ |
| 123 | foreach($selectedData as $selectedDataItem){ |
| 124 | if($selectedDataItem[0] === $attr_key && $selectedDataItem[1] === $value){ |
| 125 | $attribute_match_found = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if(!$attribute_match_found){ |
| 130 | $variation_match_found = false; |
| 131 | break; |
| 132 | } |
| 133 | $variation_match_found = true; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if($variation_match_found){ |
| 140 | wp_send_json_success( array( |
| 141 | 'variation_img_html' => Helper::get_product_thumbnail_by_image_id( $variation['image_id'], $product ), |
| 142 | ) ); |
| 143 | wp_die(); |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | wp_send_json_error(esc_html__('No image found', 'shopengine')); |
| 150 | wp_die(); |
| 151 | } |
| 152 | |
| 153 | public function get_swatch_html($html, $args) { |
| 154 | $swatch_types = Swatches::instance()->get_available_types(); |
| 155 | $attr = Helper::get_tax_attribute($args['attribute']); |
| 156 | |
| 157 | // Return if this is normal attribute |
| 158 | if(empty($attr)) { |
| 159 | return $html; |
| 160 | } |
| 161 | |
| 162 | if(!array_key_exists($attr->attribute_type, $swatch_types)) { |
| 163 | return $html; |
| 164 | } |
| 165 | |
| 166 | $options = $args['options']; |
| 167 | $product = $args['product']; |
| 168 | $attribute = $args['attribute']; |
| 169 | $class = "variation-selector variation-select-{$attr->attribute_type}"; |
| 170 | $swatches = ''; |
| 171 | |
| 172 | $args['tooltip'] = $this->is_tooltip_enabled(); |
| 173 | |
| 174 | if(empty($options) && !empty($product) && !empty($attribute)) { |
| 175 | $attributes = $product->get_variation_attributes(); |
| 176 | $options = $attributes[$attribute]; |
| 177 | } |
| 178 | |
| 179 | if(array_key_exists($attr->attribute_type, $swatch_types)) { |
| 180 | if(!empty($options) && $product && taxonomy_exists($attribute)) { |
| 181 | $terms = wc_get_product_terms($product->get_id(), $attribute, ['fields' => 'all']); |
| 182 | |
| 183 | foreach($terms as $term) { |
| 184 | if(in_array($term->slug, $options)) { |
| 185 | $swatches .= apply_filters('shopengine_filter_html_swatch_hook', '', $term, $attr->attribute_type, $args); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if(!empty($swatches)) { |
| 191 | $class .= ' hidden'; |
| 192 | $swatches = '<div class="shopengine_swatches" data-attribute_name="attribute_' . esc_attr($attribute) . '">' . $swatches . '</div>'; |
| 193 | $html = '<div class="' . esc_attr($class) . '">' . $html . '</div>' . $swatches; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return $html; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | public function swatch_html($html, $term, $type, $args) { |
| 202 | |
| 203 | $selected = sanitize_title($args['selected']) == $term->slug ? 'selected' : ''; |
| 204 | $name = esc_html(apply_filters('woocommerce_variation_option_name', $term->name)); |
| 205 | $tooltip = ''; |
| 206 | |
| 207 | if(!empty($args['tooltip'])) { |
| 208 | $tooltip = '<span class="shopengine_swatch__tooltip">' . ($term->description ? $term->description : $name) . '</span>'; |
| 209 | } |
| 210 | |
| 211 | switch($type) { |
| 212 | case Swatches::PA_COLOR: |
| 213 | $color = get_term_meta($term->term_id, $type, true); |
| 214 | |
| 215 | |
| 216 | list($r, $g, $b) = sscanf($color, "#%02x%02x%02x"); |
| 217 | $html = sprintf( |
| 218 | '<span class="swatch swatch_color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s%s</span>', |
| 219 | esc_attr($term->slug), |
| 220 | $selected, |
| 221 | esc_attr($color), |
| 222 | "rgba($r,$g,$b,0.5)", |
| 223 | esc_attr($term->slug), |
| 224 | $name, |
| 225 | $tooltip |
| 226 | ); |
| 227 | break; |
| 228 | |
| 229 | case Swatches::PA_IMAGE: |
| 230 | $image = get_term_meta($term->term_id, $type, true); |
| 231 | $image = $image ? wp_get_attachment_image_src($image) : ''; |
| 232 | $image = $image ? $image[0] : Helper::get_dummy(); |
| 233 | $html = sprintf( |
| 234 | '<span class="swatch swatch_image swatch-%s %s" data-value="%s"><img src="%s" alt="%s">%s%s</span>', |
| 235 | esc_attr($term->slug), |
| 236 | $selected, |
| 237 | esc_attr($term->slug), |
| 238 | esc_url($image), |
| 239 | esc_attr($name), |
| 240 | $name, |
| 241 | $tooltip |
| 242 | ); |
| 243 | break; |
| 244 | |
| 245 | case Swatches::PA_LABEL: |
| 246 | $label = get_term_meta($term->term_id, $type, true); |
| 247 | $label = $label ? $label : $name; |
| 248 | $html = sprintf( |
| 249 | '<span class="swatch swatch_label swatch-%s %s" data-value="%s">%s%s</span>', |
| 250 | esc_attr($term->slug), |
| 251 | $selected, |
| 252 | esc_attr($term->slug), |
| 253 | esc_html($label), |
| 254 | $tooltip |
| 255 | ); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | return $html; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | public function is_tooltip_enabled() { |
| 264 | |
| 265 | return true; |
| 266 | } |
| 267 | } |