As on the product page to make a selection by attribute when adding to cart?

If you write a custom template.
Quite simply, there are product and click add to cart. It works.
Tell me how to do the conclusion of a product's attributes in the select or do anything in order to have something to edit, and then sit don't know how to Google gives some variation.
To clarify

w4m7Z8H.png

and more..

If u tell me how to counter this amount of products to do, I have it laid out, and how to tweak it to products I can not understand

Thank you.
April 4th 20 at 00:37
1 answer
April 4th 20 at 00:39
Solution
Solved the problem by using these steps:
functions.php

add_action( 'woocommerce_before_add_to_cart_button', 'sizes_before_add_to_cart_button', 0 );
sizes_before_add_to_cart_button function() {
 global $product;

 // Only on simple products
 if( ! $product->is_type('simple') ) return;

 // When product sizes are available
 if ( $sizes = $product->get_attribute( 'size' ) ) :
 $sizes = explode(", ",$sizes);

 echo '<div class="lisatiedot__size__select" id="product-size-field">
 <label for="product-size">' . __('Valitse koko <br>') . '</label>
 <select class="select__size" name="product-size" id="product-size">
 <option value="">' . __("Choose your size") . '</option>';

 foreach( $sizes as $size ){
 echo '<option value="' . $size . '">' . $size . '</option>';
}

 echo '</select>
</div><br>';

endif;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 4 );
filter_add_to_cart_validation function( $passed, $product_id, $quantity, $variation_id = 0 ) {
 $product = wc_get_product($product_id);

 // Only on simple products and When product sizes are available
 if( ! $product->is_type('simple') && ! $product->get_attribute( 'size' ) )
 return $passed;

 if( isset( $_POST['product-size'] ) && empty( $_POST['product-size'] ) ) {
 wc_add_notice( __("Please choose your size.", 'woocommerce'), 'error' );
 $passed = false;
}
 return $passed;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_size_to_cart_item_data', 10, 3 );
add_size_to_cart_item_data function($cart_item_data, $product_id, $variation_id ){
 if( isset( $_POST['product-size'] ) ) {
 $cart_item_data['product-size'] = esc_attr( $_POST['product-size'] );
}
 return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_size_on_cart_item', 10, 2 );
display_size_on_cart_item function( $cart_item_data, $cart_item ) {
 if ( isset( $cart_item['product-size'] ) ){
 $cart_item_data[] = array(
 'name' => __('Size'),
 'value' => $cart_item['product-size'],
);
}
 return $cart_item_data;
}


And used this code to show the very sample:
single-product.php

<form class="cart" 
 action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" 
 method="post" 
enctype='multipart/form-data'>
 <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

<?php
 do_action( 'woocommerce_before_add_to_cart_quantity' );

 woocommerce_quantity_input( array(
 'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
 'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
 'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok ok input var.
 ) );

 do_action( 'woocommerce_after_add_to_cart_quantity' );
?>

 <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

 <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
 </form>

Find more questions by tags WooCommerce