· 8 years ago · Feb 15, 2018, 07:32 PM
1<td><?php
2 $date_values = get_the_terms ( $product->get_id(), 'pa_date');
3 foreach( $date_values as $dates ) {
4 echo $dates->name;
5} ?></td>
6
7<td>
8 <?php foreach($value['attributes'] as $key => $val ) {
9 $val = str_replace(array('-','_'), ' ', $val);
10 printf( '<span class="attr attr-%s">%s</span>', $key, ucwords($val) );
11 } ?>
12 </td>
13
14function woocommerce_variable_add_to_cart(){
15 global $product, $post;
16
17 $variations = find_valid_variations();
18
19 // Check if the special 'price_grid' meta is set, if it is, load the default template:
20 if ( get_post_meta($post->ID, 'price_grid', true) ) {
21 // Enqueue variation scripts
22 wp_enqueue_script( 'wc-add-to-cart-variation' );
23
24 // Load the template
25 wc_get_template( 'single-product/add-to-cart/variable.php', array(
26 'available_variations' => $product->get_available_variations(),
27 'attributes' => $product->get_variation_attributes(),
28 'selected_attributes' => $product->get_variation_default_attributes()
29 ) );
30 return;
31 }
32
33 // Cool, lets do our own template!
34 ?>
35 <table class="variations variations-grid" cellspacing="0">
36 <thead>
37 <tr>
38 <td> Date/Location </td>
39 <td> Price </td>
40 <td> Quantity </td>
41 <td> Availability </td>
42 <td> </td>
43 </tr>
44 </thead>
45 <tbody>
46 <?php
47 foreach ($variations as $key => $value) {
48 if( !$value['variation_is_visible'] ) continue;
49 ?>
50 <tr>
51 <td>
52 <?php foreach($value['attributes'] as $key => $val ) {
53 $val = str_replace(array('-','_'), ' ', $val);
54 printf( '<span class="attr attr-%s">%s</span>', $key, ucwords($val) );
55 } ?>
56 </td>
57 <td>
58 <?php echo $product->get_price(); ?>
59 </td>
60 <td>
61 <?php woocommerce_quantity_input(); ?>
62 </td>
63 <td class="stock">
64 <?php if (!$value['is_in_stock'] ) { ?>
65 <p class="stock out-of-stock"><?php _e( 'Not Available', 'woocommerce' ); ?></p>
66 <?php } else { ?>
67 <p class="stock in-stock"><?php _e( 'Available', 'woocommerce' ); ?></p>
68 </td>
69 <td>
70 <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
71 <?php
72 if(!empty($value['attributes'])){
73 foreach ($value['attributes'] as $attr_key => $attr_value) {
74 ?>
75 <input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
76 <?php
77 }
78 }
79 ?>
80 <button type="submit" class="single_add_to_cart_button btn btn-primary"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
81 <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
82 <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
83 <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
84 </form>
85 <?php } ?>
86 </td>
87 </tr>
88 <?php } ?>
89 </tbody>
90 </table>
91 <?php
92}
93
94function find_valid_variations() {
95 global $product;
96
97 $variations = $product->get_available_variations();
98 $attributes = $product->get_attributes();
99 $new_variants = array();
100
101 // Loop through all variations
102 foreach( $variations as $variation ) {
103
104 // Peruse the attributes.
105
106 // 1. If both are explicitly set, this is a valid variation
107 // 2. If one is not set, that means any, and we must 'create' the rest.
108
109 $valid = true; // so far
110 foreach( $attributes as $slug => $args ) {
111 if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
112 // Exists
113
114 } else {
115 // Not exists, create
116 $valid = false; // it contains 'anys'
117 // loop through all options for the 'ANY' attribute, and add each
118 foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
119 $attribute = trim( $attribute );
120 $new_variant = $variation;
121 $new_variant['attributes']["attribute_$slug"] = $attribute;
122 $new_variants[] = $new_variant;
123 }
124
125 }
126 }
127
128 // This contains ALL set attributes, and is itself a 'valid' variation.
129 if( $valid )
130 $new_variants[] = $variation;
131
132 }
133
134 return $new_variants;
135}