Advertisement
AquariusPL

Untitled

May 8th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.29 KB | None | 0 0
  1. public function get_swatch_html( $html, $args ) {
  2.         global $woocommerce_loop;
  3.                 global $product;
  4.  
  5.         $attr = TA_WCVS()->get_tax_attribute( $args['attribute'] );
  6.                 //$attr = TA_WCVS()->$product->get_attributes();
  7.  
  8.         // Return if this is normal attribute
  9.         if ( empty( $attr ) || ! $args['product'] instanceof WC_Product_Variable ) {
  10.             return $html;
  11.         }
  12.  
  13.         $options = $args['options'];
  14.         $product = $args['product'];
  15.         $attribute_tax_name = $args['attribute'];
  16.         $class = "variation-selector variation-select-{$attr->attribute_type}";
  17.         $swatches = '';
  18.         $is_product_page = is_product();
  19.         $defined_limit = apply_filters( 'tawcvs_swatch_limit_number', 0 );
  20.         $out_of_stock_state = apply_filters( 'tawcvs_out_of_stock_state', '' );
  21.  
  22.         //If this product has disabled the variation swatches
  23.         if ( $this->is_disabled_variation_swatches( $product ) ) {
  24.             return $html;
  25.         }
  26.  
  27.         if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute_tax_name ) ) {
  28.             $attributes = $product->get_variation_attributes();
  29.             $options = $attributes[ $attribute_tax_name ];
  30.         }
  31.  
  32.  
  33.         if ( empty( $attr->attribute_type ) ) {
  34.             return $html;
  35.         }
  36.  
  37.         // Add new option for tooltip to $args variable.
  38.         $args['tooltip'] = apply_filters( 'tawcvs_tooltip_enabled', $this->is_tooltip_enabled() );
  39.  
  40.         //Get the product variation detail for each attribute
  41.         //If there are more than one attributes, the first one will be applied
  42.         $collected_variations = array();
  43.  
  44.         if ( TA_WC_Variation_Swatches::is_pro_addon_active() && ! $this->is_use_attribute_image_only() ) {
  45.             $collected_variations = TA_WC_Variation_Swatches::get_detailed_product_variations( $product, $attribute_tax_name );
  46.         }
  47.  
  48.         if ( ! empty( $options ) && taxonomy_exists( $attribute_tax_name ) ) {
  49.             // Get terms if this is a taxonomy - ordered. We need the names too.
  50.             $terms = $this->get_product_variation_term( $product, $defined_limit, $attribute_tax_name, $options );
  51.  
  52.             foreach ( $terms as $term ) {
  53.  
  54.                 //Check if we have the product variable for this attribute
  55.                 if ( isset( $collected_variations[ $term->slug ] ) ) {
  56.                     $args['variation_product'] = $collected_variations[ $term->slug ];
  57.                 } else {
  58.                     unset( $args['variation_product'] );
  59.                 }
  60.  
  61.                 $swatches .= apply_filters( 'tawcvs_swatch_html', '', $term, $attr->attribute_type, $args, $product );
  62.  
  63.             }
  64.             //If we are on shop/archived page (not product page), we will check the defined limit number of variations
  65.             //the product still have more variations -> show the view more icon
  66.             if ( ( ! $is_product_page || $woocommerce_loop['name'] == 'related' )
  67.                  && 0 < $defined_limit
  68.                  && count( $options ) > $defined_limit ) {
  69.                 $swatches .= apply_filters( 'tawcvs_swatch_show_more_html', '', $product );
  70.             }
  71.         }
  72.  
  73.         if ( ! empty( $swatches ) ) {
  74.             $class .= ' hidden';
  75.                             $swatches = '<div id="attribute_' . esc_attr( $attribute_tax_name ) . '" class="tawcvs-swatches oss-' . $out_of_stock_state . '" data-attribute_name="attribute_' . esc_attr( $attribute_tax_name ) . '">' . $swatches . '</div>';
  76.             $html = '<div class="' . esc_attr( $class ) . '">' . $html . '</div>' . $swatches;
  77.         }
  78.         return $html;
  79.     }
  80.  
  81. add_filter('tawcvs_swatch_html', function( $html, $term, $type, $args) {
  82.     $selected = sanitize_title( $args['selected'] ) == $term->slug ? 'selected' : '';
  83.         $name     =  apply_filters( 'woocommerce_variation_option_name', $term->name ) ;
  84.         $tooltip  = '';
  85.  
  86.         /*if ($args['tooltip'] && $term->description) {
  87.             $tooltip = '<span class="swatch__tooltip">' . $term->description . '</span>';
  88.         }*/
  89.  
  90.         switch ( $type ) {
  91.             case 'color':
  92.                 $color = get_term_meta( $term->term_id, 'color', true );
  93.                 list( $r, $g, $b ) = sscanf( $color, "#%02x%02x%02x" );
  94.                 $html = sprintf(
  95.                     '<span class="swatch swatch-color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s%s</span>',
  96.                     esc_attr( $term->slug ),
  97.                     $selected,
  98.                     esc_attr( $color ),
  99.                     "rgba($r,$g,$b,0.5)",
  100.                     esc_attr( $term->slug ),
  101.                     $name,
  102.                     $tooltip
  103.                 );
  104.                 break;
  105.  
  106.             case 'image':
  107.                 $image = get_term_meta( $term->term_id, 'image', true );
  108.                                 $image = $image ? wp_get_attachment_image_src($image, 'woocommerce_single') : '';
  109.                 $image = $image ? $image[0] : WC()->plugin_url() . '/assets/images/placeholder.png';
  110.                 $label = esc_attr( $name );
  111.                                     $html  = sprintf(
  112.                     '<span class="swatch swatch-image swatch-%s %s" data-value="%s"><span class="swatch__tooltip">%s</span><span class="swatches_image-container"><img src="%s" alt="%s"></span>%s</span>',
  113.                     esc_attr( $term->slug ),
  114.                     $selected,
  115.                     esc_attr( $term->slug ),
  116.                                         $term->description,
  117.                     esc_url( $image ),
  118.                     esc_attr( $name ),
  119.                                         $label
  120.                                     );
  121.                 break;
  122.  
  123.             case 'label':
  124.                 $label = get_term_meta( $term->term_id, 'label', 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.                     $label,
  132.                     $tooltip
  133.                 );
  134.                 break;
  135.         }
  136.  
  137.         return $html;
  138. }, 10, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement