Merge pull request #20293 from woocommerce/fix/20276

Fix variable attribute selection based on query string and form posts
This commit is contained in:
Mike Jolley 2018-05-29 15:54:57 +01:00 committed by GitHub
commit 5504f966a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 7 deletions

View File

@ -167,7 +167,13 @@ class WC_Product_Variation extends WC_Product_Simple {
$data = $this->get_variation_attributes();
}
return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url );
// Filter and encode values so this is not broken by add_query_arg.
$data = array_map( 'urlencode', array_filter( $data ) );
// Encode keys.
$keys = array_map( 'urlencode', array_keys( $data ) );
return add_query_arg( array_combine( $keys, $data ), $url );
}
/**

View File

@ -2767,6 +2767,12 @@ if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {
'show_option_none' => __( 'Choose an option', 'woocommerce' ),
) );
// Get selected value.
if ( false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product ) {
$selected_key = 'attribute_' . sanitize_title( $args['attribute'] );
$args['selected'] = isset( $_REQUEST[ $selected_key ] ) ? wc_clean( urldecode( wp_unslash( $_REQUEST[ $selected_key ] ) ) ) : $args['product']->get_variation_default_attribute( $args['attribute'] ); // WPCS: input var ok, CSRF ok, sanitization ok.
}
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];

View File

@ -12,7 +12,7 @@
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.4.0
* @version 3.4.1
*/
defined( 'ABSPATH' ) || exit;
@ -33,18 +33,14 @@ do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<tbody>
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
<td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></td>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . $attribute_name ] ) ? wc_clean( urldecode( wp_unslash( $_REQUEST[ 'attribute_' . $attribute_name ] ) ) ) : $product->get_variation_default_attribute( $attribute_name ); // WPCS: input var ok, CSRF ok, sanitization ok.
wc_dropdown_variation_attribute_options( array(
'options' => $options,
'attribute' => $attribute_name,
'product' => $product,
'selected' => $selected,
) );
echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';
?>
</td>