Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function get_swatch_html( $html, $args ) {
- global $woocommerce_loop;
- global $product;
- $attr = TA_WCVS()->get_tax_attribute( $args['attribute'] );
- //$attr = TA_WCVS()->$product->get_attributes();
- // Return if this is normal attribute
- if ( empty( $attr ) || ! $args['product'] instanceof WC_Product_Variable ) {
- return $html;
- }
- $options = $args['options'];
- $product = $args['product'];
- $attribute_tax_name = $args['attribute'];
- $class = "variation-selector variation-select-{$attr->attribute_type}";
- $swatches = '';
- $is_product_page = is_product();
- $defined_limit = apply_filters( 'tawcvs_swatch_limit_number', 0 );
- $out_of_stock_state = apply_filters( 'tawcvs_out_of_stock_state', '' );
- //If this product has disabled the variation swatches
- if ( $this->is_disabled_variation_swatches( $product ) ) {
- return $html;
- }
- if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute_tax_name ) ) {
- $attributes = $product->get_variation_attributes();
- $options = $attributes[ $attribute_tax_name ];
- }
- if ( empty( $attr->attribute_type ) ) {
- return $html;
- }
- // Add new option for tooltip to $args variable.
- $args['tooltip'] = apply_filters( 'tawcvs_tooltip_enabled', $this->is_tooltip_enabled() );
- //Get the product variation detail for each attribute
- //If there are more than one attributes, the first one will be applied
- $collected_variations = array();
- if ( TA_WC_Variation_Swatches::is_pro_addon_active() && ! $this->is_use_attribute_image_only() ) {
- $collected_variations = TA_WC_Variation_Swatches::get_detailed_product_variations( $product, $attribute_tax_name );
- }
- if ( ! empty( $options ) && taxonomy_exists( $attribute_tax_name ) ) {
- // Get terms if this is a taxonomy - ordered. We need the names too.
- $terms = $this->get_product_variation_term( $product, $defined_limit, $attribute_tax_name, $options );
- foreach ( $terms as $term ) {
- //Check if we have the product variable for this attribute
- if ( isset( $collected_variations[ $term->slug ] ) ) {
- $args['variation_product'] = $collected_variations[ $term->slug ];
- } else {
- unset( $args['variation_product'] );
- }
- $swatches .= apply_filters( 'tawcvs_swatch_html', '', $term, $attr->attribute_type, $args, $product );
- }
- //If we are on shop/archived page (not product page), we will check the defined limit number of variations
- //the product still have more variations -> show the view more icon
- if ( ( ! $is_product_page || $woocommerce_loop['name'] == 'related' )
- && 0 < $defined_limit
- && count( $options ) > $defined_limit ) {
- $swatches .= apply_filters( 'tawcvs_swatch_show_more_html', '', $product );
- }
- }
- if ( ! empty( $swatches ) ) {
- $class .= ' hidden';
- $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>';
- $html = '<div class="' . esc_attr( $class ) . '">' . $html . '</div>' . $swatches;
- }
- return $html;
- }
- add_filter('tawcvs_swatch_html', function( $html, $term, $type, $args) {
- $selected = sanitize_title( $args['selected'] ) == $term->slug ? 'selected' : '';
- $name = apply_filters( 'woocommerce_variation_option_name', $term->name ) ;
- $tooltip = '';
- /*if ($args['tooltip'] && $term->description) {
- $tooltip = '<span class="swatch__tooltip">' . $term->description . '</span>';
- }*/
- switch ( $type ) {
- case 'color':
- $color = get_term_meta( $term->term_id, 'color', true );
- list( $r, $g, $b ) = sscanf( $color, "#%02x%02x%02x" );
- $html = sprintf(
- '<span class="swatch swatch-color swatch-%s %s" style="background-color:%s;color:%s;" data-value="%s">%s%s</span>',
- esc_attr( $term->slug ),
- $selected,
- esc_attr( $color ),
- "rgba($r,$g,$b,0.5)",
- esc_attr( $term->slug ),
- $name,
- $tooltip
- );
- break;
- case 'image':
- $image = get_term_meta( $term->term_id, 'image', true );
- $image = $image ? wp_get_attachment_image_src($image, 'woocommerce_single') : '';
- $image = $image ? $image[0] : WC()->plugin_url() . '/assets/images/placeholder.png';
- $label = esc_attr( $name );
- $html = sprintf(
- '<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>',
- esc_attr( $term->slug ),
- $selected,
- esc_attr( $term->slug ),
- $term->description,
- esc_url( $image ),
- esc_attr( $name ),
- $label
- );
- break;
- case 'label':
- $label = get_term_meta( $term->term_id, 'label', true );
- $label = $label ? $label : $name;
- $html = sprintf(
- '<span class="swatch swatch-label swatch-%s %s" data-value="%s">%s%s</span>',
- esc_attr( $term->slug ),
- $selected,
- esc_attr( $term->slug ),
- $label,
- $tooltip
- );
- break;
- }
- return $html;
- }, 10, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement