shopengine-swatches.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Swatches\Loop_Product_Support; |
| 4 | |
| 5 | |
| 6 | use ShopEngine\Modules\Swatches\Swatches; |
| 7 | use WC_Product_Simple; |
| 8 | |
| 9 | class Shopengine_Swatches |
| 10 | { |
| 11 | private $attribute_taxonomy = []; |
| 12 | private $attribute_options = []; |
| 13 | |
| 14 | private $color_attribute = 'shopengine_color'; |
| 15 | private $image_attribute = 'shopengine_image'; |
| 16 | private $label_attribute = 'shopengine_label'; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | add_action('wp_enqueue_scripts', [$this, 'add_enqueue']); |
| 21 | add_action('shopengine_swatches_anywhere', [$this, 'print_attributes'], 10, 2); |
| 22 | } |
| 23 | |
| 24 | public function add_enqueue() |
| 25 | { |
| 26 | wp_enqueue_style('shopengine-swatches-loop-css', Swatches::get_module_uri() . 'loop-product-support/assets/swatches.css', ['wp-color-picker'], time()); |
| 27 | wp_enqueue_script('shopengine-swatches-loop-js', Swatches::get_module_uri() . 'loop-product-support/assets/swatches.js', ['jquery'], '1515155', true); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | public static function getInstance() |
| 32 | { |
| 33 | static $instance = null; |
| 34 | if (null === $instance) { |
| 35 | $instance = new Shopengine_Swatches(); |
| 36 | } |
| 37 | |
| 38 | return $instance; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * @param $product \WC_Product |
| 44 | * @param $attributes_to_show array |
| 45 | * @return void |
| 46 | */ |
| 47 | public function print_attributes($product, $attributes_to_show = ['pa_color']) |
| 48 | { |
| 49 | if (!$product->is_type('variable')) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $variation_attributes = $product->get_variation_attributes(); |
| 54 | |
| 55 | foreach ($attributes_to_show as $attribute_to_show) { |
| 56 | |
| 57 | $selected_attributes = $variation_attributes[$attribute_to_show]; |
| 58 | |
| 59 | $attributes = $product->get_available_variations(); |
| 60 | $attribute_data = []; |
| 61 | |
| 62 | foreach ($attributes as $attribute) { |
| 63 | |
| 64 | $attr_keys = array_keys($attribute['attributes']); |
| 65 | |
| 66 | foreach ($attr_keys as $attr_key) { |
| 67 | $attr_value = $attribute['attributes'][$attr_key]; |
| 68 | if (in_array($attr_value, $selected_attributes)) { |
| 69 | |
| 70 | $attribute_data[$attr_value]['src'] = $attribute['image']['src']; |
| 71 | // $attribute_data[$attr_value]['srcset'] = $attribute['image']['srcset']; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $options = wc_get_product_terms($product->get_id(), $attribute_to_show, array('fields' => 'all')); |
| 79 | $attr = $this->get_attribute_taxonomy($attribute_to_show); |
| 80 | $swatches = ''; |
| 81 | foreach ($options as $option) { |
| 82 | if (isset($attribute_data[$option->slug])) { |
| 83 | $swatches .= $this->get_swatches_html($option, $attr->attribute_type); |
| 84 | } |
| 85 | } |
| 86 | if($swatches){ |
| 87 | shopengine_content_render('<div class="shopengine_swatches shopengine_swatches_in_loop shopengine_'.$attribute_to_show.'" data-attribute=' . "'" . json_encode($attribute_data, true) . "'" . ' data-attribute_name="attribute_' . esc_attr($attribute_to_show) . '">' . $swatches . '</div>'); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | private function get_attribute_taxonomy($taxonomy) |
| 94 | { |
| 95 | if (isset($this->attribute_taxonomy[$taxonomy])) { |
| 96 | return $this->attribute_taxonomy[$taxonomy]; |
| 97 | } |
| 98 | |
| 99 | global $wpdb; |
| 100 | |
| 101 | $attr = substr($taxonomy, 3); |
| 102 | return $this->attribute_taxonomy[$taxonomy] = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s", $attr)); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | private function get_swatches_html($term, $type) |
| 107 | { |
| 108 | if (isset($this->attribute_options[$term->slug])) { |
| 109 | return $this->attribute_options[$term->slug]; |
| 110 | } |
| 111 | |
| 112 | $selected = ''; |
| 113 | $name = $term->name; |
| 114 | $tooltip = ''; |
| 115 | $html = ''; |
| 116 | |
| 117 | switch ($type) { |
| 118 | case $this->color_attribute: |
| 119 | $color = get_term_meta($term->term_id, $type, true); |
| 120 | list($r, $g, $b) = sscanf($color, "#%02x%02x%02x"); |
| 121 | $html = sprintf( |
| 122 | '<span class="swatch swatch_color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s%s</span>', |
| 123 | esc_attr($term->slug), |
| 124 | $selected, |
| 125 | esc_attr($color), |
| 126 | "rgba($r,$g,$b,0.5)", |
| 127 | esc_attr($term->slug), |
| 128 | $name, |
| 129 | $tooltip |
| 130 | ); |
| 131 | break; |
| 132 | |
| 133 | case $this->image_attribute: |
| 134 | $image = get_term_meta($term->term_id, $type, true); |
| 135 | $image = $image ? wp_get_attachment_image_src($image) : ''; |
| 136 | $image = $image ? $image[0] : ''; |
| 137 | $html = sprintf( |
| 138 | '<span class="swatch swatch_image swatch-%s %s" data-value="%s"><img src="%s" alt="%s">%s%s</span>', |
| 139 | esc_attr($term->slug), |
| 140 | $selected, |
| 141 | esc_attr($term->slug), |
| 142 | esc_url($image), |
| 143 | esc_attr($name), |
| 144 | $name, |
| 145 | $tooltip |
| 146 | ); |
| 147 | break; |
| 148 | |
| 149 | case $this->label_attribute: |
| 150 | $label = get_term_meta($term->term_id, $type, true); |
| 151 | $label = $label ? $label : $name; |
| 152 | $html = sprintf( |
| 153 | '<span class="swatch swatch_label swatch-%s %s" data-value="%s">%s%s</span>', |
| 154 | esc_attr($term->slug), |
| 155 | $selected, |
| 156 | esc_attr($term->slug), |
| 157 | esc_html($label), |
| 158 | $tooltip |
| 159 | ); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | $this->attribute_options[$term->slug] = $html; |
| 164 | return $html; |
| 165 | } |
| 166 | } |
| 167 |