Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. function wc_dropdown_variation_attribute_options( $args = array() ) {
  2. global $product;
  3. $variations = $product->get_available_variations();
  4.  
  5. $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
  6. 'options' => false,
  7. 'attribute' => false,
  8. 'product' => false,
  9. 'selected' => false,
  10. 'name' => '',
  11. 'id' => '',
  12. 'class' => '',
  13. 'show_option_none' => __( 'Choose an option', 'woocommerce' ),
  14. ) );
  15. $options = $args['options'];
  16. $product = $args['product'];
  17. $attribute = $args['attribute'];
  18. $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
  19. $id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
  20. $class = $args['class'];
  21. if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
  22. $attributes = $product->get_variation_attributes();
  23. $options = $attributes[ $attribute ];
  24.  
  25. }
  26. $html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">';
  27. if ( $args['show_option_none'] ) {
  28. $html .= '<option value="">' . esc_html( $args['show_option_none'] ) . '</option>';
  29. }
  30. if ( ! empty( $options ) ) {
  31. if ( $product && taxonomy_exists( $attribute ) ) {
  32. // Get terms if this is a taxonomy - ordered. We need the names too.
  33. $terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );
  34. foreach ( $terms as $term ) {
  35. if ( in_array( $term->slug, $options ) ) {
  36. $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>';
  37. }
  38. }
  39. } else {
  40. foreach ( $options as $option ) {
  41.  
  42. foreach ($variations as $variation) {
  43. if($variation['attributes'][$name] == $option) {
  44. $stock = esc_html($variation['max_qty']);
  45. }
  46. }
  47. if( $stock == 0) {
  48. $stock_text = ' - (Out of Stock)';
  49. $class = 'option-out-of-stock';
  50. $disabled = 'disabled';
  51. } elseif ($stock < 5 ) {
  52. $stock_text = ' - Only ' . $stock . ' left!';
  53. $class= 'option-hurry';
  54. $disabled = '';
  55. } elseif ($stock < 6) {
  56. $stock_text = ' - Only a few left!';
  57. $class = 'option-few';
  58. $disabled = '';
  59. } else {
  60. $stock_text = ' - (In Stock)';
  61. $class = '';
  62. $disabled = '';
  63. }
  64. // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
  65. $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
  66. $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . ' class="'.$class.'" '.$disabled.'>' . $option . $stock_text .'</option>';
  67. }
  68. }
  69. }
  70. $html .= '</select>';
  71.  
  72. echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement