Advertisement
Guest User

variable.php

a guest
Jun 5th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. <?php
  2. /**
  3. * Variable product add to cart
  4. *
  5. * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/variable.php.
  6. *
  7. * HOWEVER, on occasion WooCommerce will need to update template files and you
  8. * (the theme developer) will need to copy the new files to your theme to
  9. * maintain compatibility. We try to do this as little as possible, but it does
  10. * happen. When this occurs the version of the template file will be bumped and
  11. * the readme will list any important changes.
  12. *
  13. * @see https://docs.woocommerce.com/document/template-structure/
  14. * @package WooCommerce/Templates
  15. * @version 3.4.1
  16. */
  17.  
  18. defined( 'ABSPATH' ) || exit;
  19.  
  20. global $product;
  21.  
  22. $swatches_use_variation_images = woodmart_get_opt( 'swatches_use_variation_images' );
  23.  
  24. $grid_swatches_attribute = woodmart_grid_swatches_attribute();
  25.  
  26. $attribute_keys = array_keys( $attributes );
  27.  
  28. do_action( 'woocommerce_before_add_to_cart_form' ); ?>
  29.  
  30. <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( json_encode( $available_variations ) ) ?>">
  31. <?php do_action( 'woocommerce_before_variations_form' ); ?>
  32.  
  33. <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
  34. <p class="stock out-of-stock"><?php esc_html_e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
  35. <?php else : ?>
  36. <table class="variations" cellspacing="0">
  37. <tbody>
  38. <?php $loop = 0; foreach ( $attributes as $attribute_name => $options ) : $loop++; ?>
  39. <?php
  40. $swatches = woodmart_has_swatches( $product->get_id(), $attribute_name, $options, $available_variations, $swatches_use_variation_images);
  41. ?>
  42. <tr>
  43. <td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
  44. <td class="value <?php if ( ! empty( $swatches ) ): ?>with-swatches<?php endif; ?>">
  45. <?php if ( ! empty( $swatches ) ): ?>
  46. <div class="swatches-select" data-id="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>">
  47. <?php
  48. if ( is_array( $options ) ) {
  49.  
  50. if ( isset( $_REQUEST[ 'attribute_' . $attribute_name ] ) ) {
  51. $selected_value = $_REQUEST[ 'attribute_' . $attribute_name ];
  52. } elseif ( isset( $selected_attributes[ $attribute_name ] ) ) {
  53. $selected_value = $selected_attributes[ $attribute_name ];
  54. } else {
  55. $selected_value = '';
  56. }
  57.  
  58. // Get terms if this is a taxonomy - ordered
  59. if ( taxonomy_exists( $attribute_name ) ) {
  60.  
  61. $terms = wc_get_product_terms( $product->get_id(), $attribute_name, array( 'fields' => 'all' ) );
  62.  
  63. $swatch_size = woodmart_wc_get_attribute_term( $attribute_name, 'swatch_size' );
  64.  
  65. $_i = 0;
  66. $options_fliped = array_flip( $options );
  67. foreach ( $terms as $term ) {
  68. if ( ! in_array( $term->slug, $options ) ) {
  69. continue;
  70. }
  71. $key = $options_fliped[$term->slug];
  72.  
  73. $style = '';
  74. $class = 'woodmart-swatch ';
  75. if( ! empty( $swatches[$key]['color'] )) {
  76. $class .= 'colored-swatch woodmart-tooltip';
  77. $style = 'background-color:' . $swatches[$key]['color'];
  78. } else if( ! empty( $swatches[$key]['image'] )) {
  79. $class .= 'image-swatch woodmart-tooltip';
  80. $style = 'background-image: url(' . $swatches[$key]['image'] . ')';
  81. } else if( ! empty( $swatches[$key]['not_dropdown'] ) ) {
  82. $class .= ' text-only';
  83. }
  84.  
  85. if( $swatches_use_variation_images && $grid_swatches_attribute == $attribute_name && isset( $swatches[$key]['image_src'] ) ) {
  86. $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $swatches[$key]['variation_id'] ), 'woocommerce_thumbnail');
  87. if ( !empty( $thumb ) ) {
  88. $style = 'background-image: url(' . $thumb[0] . ')';
  89. $class .= ' variation-image-used image-swatch';
  90. }
  91. }
  92.  
  93. $class .= ' swatch-size-' . $swatch_size;
  94.  
  95. echo '<div class="' . esc_attr( $class ) . '" data-value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . ' style="' . esc_attr( $style ) .'">' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</div>';
  96.  
  97. $_i++;
  98. }
  99.  
  100. } else {
  101.  
  102. foreach ( $options as $option ) {
  103. echo '<div data-value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
  104. }
  105.  
  106. }
  107. }
  108. ?>
  109.  
  110. </div>
  111.  
  112. <?php endif; ?>
  113.  
  114. <?php
  115.  
  116. wc_dropdown_variation_attribute_options( array(
  117. 'options' => $options,
  118. 'attribute' => $attribute_name,
  119. 'product' => $product,
  120. ) );
  121.  
  122. 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>' ) ) : '';
  123. ?>
  124.  
  125. </td>
  126. </tr>
  127. <?php endforeach;?>
  128. </tbody>
  129. </table>
  130.  
  131. <div class="single_variation_wrap">
  132. <?php
  133. /**
  134. * Hook: woocommerce_before_single_variation.
  135. */
  136. do_action( 'woocommerce_before_single_variation' );
  137.  
  138. /**
  139. * Hook: woocommerce_single_variation. Used to output the cart button and placeholder for variation data.
  140. * @since 2.4.0
  141. * @hooked woocommerce_single_variation - 10 Empty div for variation data.
  142. * @hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
  143. */
  144. do_action( 'woocommerce_single_variation' );
  145.  
  146. /**
  147. * Hook: woocommerce_after_single_variation.
  148. */
  149. do_action( 'woocommerce_after_single_variation' );
  150. ?>
  151. </div>
  152.  
  153. <?php endif; ?>
  154.  
  155. <?php do_action( 'woocommerce_after_variations_form' ); ?>
  156. </form>
  157.  
  158. <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement