Title: Make some variations attributes optional
Last modified: May 8, 2024

---

# Make some variations attributes optional

 *  Resolved [aquariuspl](https://wordpress.org/support/users/aquariuspl/)
 * (@aquariuspl)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/make-some-variations-attributes-optional/)
 * Hi,
   I’m using Variation Swatches plugin by AOV UP. It’s convert simple view of
   wordpress to image swatch like you can see below. I’ve got some variations of
   product that are like “subvariations” and I want to make it optional. Woocommerce
   won’t pass this product to cart if all of them are ont chosen. I’ve got some 
   code that makes attributes which are not set as visible in variations optional:
 * ![](https://i0.wp.com/iili.io/Jre1vvs.jpg?ssl=1)
 *     ```wp-block-code
       /**
        * List available attributes on the product page in a drop-down selection
        */
       function list_attributes_on_product_page() {
           global $product;
           $attributes = $product->get_attributes();
   
           if ( ! $attributes ) {
               return;
           }
   
           //from original script, but here we want to use it for variable products
           /*if ($product->is_type( 'variable' )) {
               return;
           }*/
   
           echo '<div style="padding-bottom:15px;">';
   
           foreach ( $attributes as $attribute ) {
   
               //If product is variable, and attribute is used for variation: woocommerce already handle this input - so it can also be used with attributes of simple products (not variables)
           if($product->is_type( 'variable' ) && $attribute['variation']) {
               continue;
           }
   
               //get taxonomy for the attribute - eg: Size
               $taxonomy = get_taxonomy($attribute['name']);
   
               //get terms - eg: small
               $options = wc_get_product_terms( $product->get_id(), $attribute['name'], array( 'fields' => 'all' ) );
               $label = str_replace('Product ', '', $taxonomy->label);
               //display select input
               ?>
               <div style="padding-bottom:8px;">
                   <label for="attribute[<?php echo $attribute['id']; ?>]"><?php echo $label; ?></label>
                   <br />
                   <!-- add required attribute or not, handle default with "selected" attribute depending your needs -->
                   <select name="attribute[<?php echo $attribute['id']; ?>]" id="attribute[<?php echo $attribute['id']; ?>]">
                       <option value disabled selected>Choose an option</option>
                       <?php foreach ( $options as $pa ): ?>
                           <option value="<?php echo $pa->name; ?>"><?php echo $pa->name; ?></option>
                       <?php endforeach; ?>
                   </select>
               </div>
               <?php
           }
   
           echo '</div>';
       }
       add_action('woocommerce_before_add_to_cart_button', 'list_attributes_on_product_page');
   
       /**
        * Add selected attributes to cart items
        */
       add_filter('woocommerce_add_cart_item_data', 'add_attributes_to_cart_item', 10, 3 );
       function add_attributes_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
           $attributes = $_POST['attribute'] ?? null;
   
           if (empty( $attributes ) ) {
               return $cart_item_data;
           }
   
           $cart_item_data['attributes'] = serialize($attributes);
   
           return $cart_item_data;
       }
   
       /**
        * Display attributes in cart
        */
       add_filter( 'woocommerce_get_item_data', 'display_attributes_in_cart', 10, 2 );
       function display_attributes_in_cart( $item_data, $cart_item ) {
           if ( empty( $cart_item['attributes'] ) ) {
               return $item_data;
           }
   
           foreach (unserialize($cart_item['attributes']) as $attributeID => $value) {
               $attribute = wc_get_attribute($attributeID);
               $item_data[] = array(
                   'key'     => $attribute->name,
                   'value'   => $value,
                   'display' => '',
               );
           }
   
           return $item_data;
       }
   
       /**
        * Add attribute data to order items
        */
       add_action( 'woocommerce_checkout_create_order_line_item', 'add_attributes_to_order_items', 10, 4 );
       function add_attributes_to_order_items( $item, $cart_item_key, $values, $order ) {
           if ( empty( $values['attributes'] ) ) {
               return;
           }
   
           foreach (unserialize($values['attributes']) as $attributeID => $value) {
               $attribute = wc_get_attribute($attributeID);
               $item->add_meta_data( $attribute->name, $value );
           }
       }
       ```
   
 * but then they are not parsed by Variations Swatches Plugin:
 * ![](https://i0.wp.com/iili.io/Jre1Ikb.jpg?ssl=1)
 * When using this part of code Woocommerce passing to cart this product, but I 
   can’t show attributes like I want to (I mean parse them trough Variations Swatches
   Plugin). This is the part of Variations Swatches Plugin which handle showing 
   swatches on product page:
 *     ```wp-block-code
       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);
       ```
   
 * Any clues? Help?
    -  This topic was modified 2 years, 1 month ago by [aquariuspl](https://wordpress.org/support/users/aquariuspl/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fmake-some-variations-attributes-optional%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [ckadenge (woo-hc)](https://wordpress.org/support/users/ckadenge/)
 * (@ckadenge)
 * [2 years, 1 month ago](https://wordpress.org/support/topic/make-some-variations-attributes-optional/#post-17743490)
 * Hi there [@aquariuspl](https://wordpress.org/support/users/aquariuspl/),
 * Thanks for reaching out.
 * I’m afraid this goes beyond the scope of the support we can provide, as it’s 
   a third-party plugin.
 * I would recommend reaching out to [the developers of the Variation Swatches Plugin directly for assistance](https://aovup.com/contact/?utm_source=footer-banner).
   They would be best equipped to help you modify the plugin’s behavior to suit 
   your needs.
 * If you have any other questions or need further help with WooCommerce features,
   feel free to ask.
 *  [anastas10s](https://wordpress.org/support/users/anastas10s/)
 * (@anastas10s)
 * [2 years ago](https://wordpress.org/support/topic/make-some-variations-attributes-optional/#post-17772884)
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   we’ll be here if and/or when you are ready to continue.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Make some variations attributes optional’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [attributes](https://wordpress.org/support/topic-tag/attributes/)
 * [variations](https://wordpress.org/support/topic-tag/variations/)

 * 2 replies
 * 3 participants
 * Last reply from: [anastas10s](https://wordpress.org/support/users/anastas10s/)
 * Last activity: [2 years ago](https://wordpress.org/support/topic/make-some-variations-attributes-optional/#post-17772884)
 * Status: resolved