assets
4 years ago
loop-product-support
4 years ago
admin-product.php
4 years ago
attribute-hooks.php
4 years ago
frontend.php
4 years ago
helper.php
4 years ago
swatches.php
4 years ago
frontend.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Swatches; |
| 4 | |
| 5 | use ShopEngine; |
| 6 | use ShopEngine\Traits\Singleton; |
| 7 | |
| 8 | defined('ABSPATH') || exit; |
| 9 | |
| 10 | class Frontend |
| 11 | { |
| 12 | |
| 13 | use Singleton; |
| 14 | |
| 15 | public function init() { |
| 16 | |
| 17 | add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 18 | add_filter('woocommerce_dropdown_variation_attribute_options_html', [$this, 'get_swatch_html'], 100, 2); |
| 19 | add_filter('shopengine_filter_html_swatch_hook', [$this, 'swatch_html'], 5, 4); |
| 20 | } |
| 21 | |
| 22 | |
| 23 | public function enqueue_scripts() { |
| 24 | wp_enqueue_style('shopengine-css-front', Swatches::asset_source('css', 'frontend.css'), [], ShopEngine::version()); |
| 25 | wp_enqueue_script('shopengine-js-front', Swatches::asset_source('js', 'frontend.js'), ['jquery'], ShopEngine::version(), true); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | public function get_swatch_html($html, $args) { |
| 30 | |
| 31 | $swatch_types = Swatches::instance()->get_available_types(); |
| 32 | |
| 33 | $attr = Helper::get_tax_attribute($args['attribute']); |
| 34 | |
| 35 | |
| 36 | // Return if this is normal attribute |
| 37 | if(empty($attr)) { |
| 38 | return $html; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | if(!array_key_exists($attr->attribute_type, $swatch_types)) { |
| 43 | return $html; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | $options = $args['options']; |
| 48 | $product = $args['product']; |
| 49 | $attribute = $args['attribute']; |
| 50 | $class = "variation-selector variation-select-{$attr->attribute_type}"; |
| 51 | $swatches = ''; |
| 52 | |
| 53 | $args['tooltip'] = $this->is_tooltip_enabled(); |
| 54 | |
| 55 | if(empty($options) && !empty($product) && !empty($attribute)) { |
| 56 | $attributes = $product->get_variation_attributes(); |
| 57 | $options = $attributes[$attribute]; |
| 58 | } |
| 59 | |
| 60 | if(array_key_exists($attr->attribute_type, $swatch_types)) { |
| 61 | if(!empty($options) && $product && taxonomy_exists($attribute)) { |
| 62 | $terms = wc_get_product_terms($product->get_id(), $attribute, ['fields' => 'all']); |
| 63 | |
| 64 | foreach($terms as $term) { |
| 65 | if(in_array($term->slug, $options)) { |
| 66 | $swatches .= apply_filters('shopengine_filter_html_swatch_hook', '', $term, $attr->attribute_type, $args); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if(!empty($swatches)) { |
| 72 | $class .= ' hidden'; |
| 73 | $swatches = '<div class="shopengine_swatches" data-attribute_name="attribute_' . esc_attr($attribute) . '">' . $swatches . '</div>'; |
| 74 | $html = '<div class="' . esc_attr($class) . '">' . $html . '</div>' . $swatches; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return $html; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | public function swatch_html($html, $term, $type, $args) { |
| 83 | |
| 84 | $selected = sanitize_title($args['selected']) == $term->slug ? 'selected' : ''; |
| 85 | $name = esc_html(apply_filters('woocommerce_variation_option_name', $term->name)); |
| 86 | $tooltip = ''; |
| 87 | |
| 88 | if(!empty($args['tooltip'])) { |
| 89 | $tooltip = '<span class="shopengine_swatch__tooltip">' . ($term->description ? $term->description : $name) . '</span>'; |
| 90 | } |
| 91 | |
| 92 | switch($type) { |
| 93 | case Swatches::PA_COLOR: |
| 94 | $color = get_term_meta($term->term_id, $type, true); |
| 95 | list($r, $g, $b) = sscanf($color, "#%02x%02x%02x"); |
| 96 | $html = sprintf( |
| 97 | '<span class="swatch swatch_color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s%s</span>', |
| 98 | esc_attr($term->slug), |
| 99 | $selected, |
| 100 | esc_attr($color), |
| 101 | "rgba($r,$g,$b,0.5)", |
| 102 | esc_attr($term->slug), |
| 103 | $name, |
| 104 | $tooltip |
| 105 | ); |
| 106 | break; |
| 107 | |
| 108 | case Swatches::PA_IMAGE: |
| 109 | $image = get_term_meta($term->term_id, $type, true); |
| 110 | $image = $image ? wp_get_attachment_image_src($image) : ''; |
| 111 | $image = $image ? $image[0] : Helper::get_dummy(); |
| 112 | $html = sprintf( |
| 113 | '<span class="swatch swatch_image swatch-%s %s" data-value="%s"><img src="%s" alt="%s">%s%s</span>', |
| 114 | esc_attr($term->slug), |
| 115 | $selected, |
| 116 | esc_attr($term->slug), |
| 117 | esc_url($image), |
| 118 | esc_attr($name), |
| 119 | $name, |
| 120 | $tooltip |
| 121 | ); |
| 122 | break; |
| 123 | |
| 124 | case Swatches::PA_LABEL: |
| 125 | $label = get_term_meta($term->term_id, $type, true); |
| 126 | $label = $label ? $label : $name; |
| 127 | $html = sprintf( |
| 128 | '<span class="swatch swatch_label swatch-%s %s" data-value="%s">%s%s</span>', |
| 129 | esc_attr($term->slug), |
| 130 | $selected, |
| 131 | esc_attr($term->slug), |
| 132 | esc_html($label), |
| 133 | $tooltip |
| 134 | ); |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | return $html; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | public function is_tooltip_enabled() { |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | } |