D
D
DeeUs2018-02-21 11:53:01
WordPress
DeeUs, 2018-02-21 11:53:01

How to add a variation selection form to products in a category?

How to add a variation selection form on product cards in a category?
Now variations can only be selected on the product page, I would like to select the desired variations and throw the product into the cart without going to the product page
Display the form turned out, but it looks strange ( label url-encoded ) and in the drop-down list there is only "select option"

screen
5a8d31ee51278696878963.png

I wrote the following in functions.php:
the code
add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );

     function woo_display_variation_dropdown_on_shop_page() {

         global $product;
         $attributes = $product->get_attributes();
         $attribute_keys = array_keys( $attributes );

         do_action( 'woocommerce_before_add_to_cart_form' ); ?>

     <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ) ?>">
         <?php do_action( 'woocommerce_before_variations_form' ); ?>

         <?php if ( empty( $product->get_available_variations() ) && false !== $product->get_available_variations() ) : ?>
         <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
         <?php else : ?>
         <table class="variations" cellspacing="0">
             <tbody>
                 <?php foreach ( $attributes as $attribute_name => $options ) : ?>
             <tr>
                 <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
                 <td class="value">
                     <?php
                     $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( stripslashes( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) ) : $product->get_variation_default_attribute( $attribute_name );
                     wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
                     echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) : '';
                     ?>
                 </td>
             </tr>
                 <?php endforeach;?>
             </tbody>
         </table>

         <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

         <div class="single_variation_wrap">
             <?php

             do_action( 'woocommerce_before_single_variation' );

             do_action( 'woocommerce_single_variation' );

             do_action( 'woocommerce_after_single_variation' );
             ?>
         </div>

         <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
         <?php endif; ?>

         <?php do_action( 'woocommerce_after_variations_form' ); ?>
     </form>
<?php
     }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DeeUs, 2018-02-21
@DeeUs

Solution found!
https://gist.github.com/shshanker/4c7876916e113f4c...

A
Ainur Valiev, 2018-02-21
@vaajnur

echo urldecode('%D0%BD%D0%BE%D0%...')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question