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 | } |