Advertisement
Guest User

WooCommerce Variations

a guest
May 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. // Display variations dropdowns on shop page for variable products
  2. add_filter( 'woocommerce_loop_add_to_cart_link', 'woo_display_variation_dropdown_on_shop_page' );
  3. function woo_display_variation_dropdown_on_shop_page() {
  4.     global $product;
  5.     if ( $product->is_type( 'variable' )) {
  6.    
  7.     $attribute_keys = array_keys( $product->get_variation_attributes() );
  8.     ?>
  9.     <form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->id ); ?>" data-product_variations="<?php echo htmlspecialchars( json_encode( $product->get_available_variations() ) ) ?>">
  10.         <?php do_action( 'woocommerce_before_variations_form' ); ?>
  11.  
  12.             <table class="variations" cellspacing="0">
  13.                 <tbody>
  14.                     <?php foreach ( $product->get_variation_attributes() as $attribute_name => $options ) : ?>
  15.                         <tr>
  16.                             <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
  17.                             <td class="value">
  18.                                 <?php
  19.                                     $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name );
  20.                                     wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) );
  21.                                     echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
  22.                                 ?>
  23.                             </td>
  24.                         </tr>
  25.                     <?php endforeach;?>
  26.                 </tbody>
  27.             </table>
  28.             <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
  29.             <div class="single_variation_wrap">
  30.                 <?php
  31.                     /**
  32.                      * woocommerce_before_single_variation Hook.
  33.                      */
  34.                     do_action( 'woocommerce_before_single_variation' );
  35.                     /**
  36.                      * woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
  37.                      * @since 2.4.0
  38.                      * @hooked woocommerce_single_variation - 10 Empty div for variation data.
  39.                      * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
  40.                      */
  41.                     do_action( 'woocommerce_single_variation' );
  42.                     /**
  43.                      * woocommerce_after_single_variation Hook.
  44.                      */
  45.                     do_action( 'woocommerce_after_single_variation' );
  46.                 ?>
  47.             </div>
  48.             <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
  49.  
  50.         <?php do_action( 'woocommerce_after_variations_form' ); ?>
  51.     </form>
  52.     <?php }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement