· 8 years ago · Feb 18, 2018, 03:28 PM
1<td class="date">
2 <?php
3 $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
4 $date = array_shift( wc_get_product_terms( $product_id, 'pa_date', array( 'fields' => 'names' ) ) );
5 ?>
6</td>
7
8function woocommerce_variable_add_to_cart(){
9 global $product, $post, $woocommerce;
10
11 $attributes = $product->get_attributes();
12
13 $variations = find_valid_variations();
14
15 // Check if the special 'price_grid' meta is set, if it is, load the default template:
16 if ( get_post_meta($post->ID, 'price_grid', true) ) {
17 wp_enqueue_script( 'wc-add-to-cart-variation' );
18
19 wc_get_template( 'single-product/add-to-cart/variable.php', array(
20 'available_variations' => $product->get_available_variations(),
21 'attributes' => $product->get_variation_attributes(),
22 'selected_attributes' => $product->get_variation_default_attributes()
23 ) );
24 return;
25 }
26
27 ?>
28 <table class="variations variations-grid" cellspacing="0">
29 <thead>
30 <tr>
31 <td> Date | Location </td>
32 <td> Price </td>
33 <td> Quantity </td>
34 <td> Availability </td>
35 <td> </td>
36 </tr>
37 </thead>
38 <tbody>
39 <?php
40 foreach ($variations as $key => $value) {
41 if( !$value['variation_is_visible'] ) continue;
42 ?>
43 <tr>
44 <td class="date">
45 <?php
46 $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Added WC 3+ support
47 $date = array_shift( wc_get_product_terms( $product_id, 'pa_date', array( 'fields' => 'names' ) ) );
48 ?>
49 </td>
50 <td class="price">
51 <?php echo '<span>£</span>' . $product->get_price(); ?>
52 </td>
53 <td class="quantity">
54 <?php woocommerce_quantity_input(); ?>
55 </td>
56 <td class="stock">
57 <?php if (!$value['is_in_stock'] ) { ?>
58 <p class="stock out-of-stock"><?php _e( 'Places Not Available', 'woocommerce' ); ?></p>
59 <?php } else { ?>
60 <p class="stock in-stock"><?php _e( 'Places Available', 'woocommerce' ); ?></p>
61 </td>
62 <td class="add-to-cart">
63 <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
64 <?php
65 if(!empty($value['attributes'])){
66 foreach ($value['attributes'] as $attr_key => $attr_value) {
67 ?>
68 <input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
69 <?php
70 }
71 }
72 ?>
73 <button type="submit" class="single_add_to_cart_button button alt"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
74 <input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
75 <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
76 <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
77 </form>
78 <?php } ?>
79 </td>
80 </tr>
81 <?php } ?>
82 </tbody>
83 </table>
84 <?php
85}
86
87function find_valid_variations() {
88 global $product;
89
90 $variations = $product->get_available_variations();
91 $attributes = $product->get_attributes();
92 $new_variants = array();
93
94 foreach( $variations as $variation ) {
95
96 // Peruse the attributes.
97
98 // 1. If both are explicitly set, this is a valid variation
99 // 2. If one is not set, that means any, and we must 'create' the rest.
100
101 $valid = true; // so far
102 foreach( $attributes as $slug => $args ) {
103 if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
104 // Exists
105
106 } else {
107 // Not exists, create
108 $valid = false; // it contains 'anys'
109
110 foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
111 $attribute = trim( $attribute );
112 $new_variant = $variation;
113 $new_variant['attributes']["attribute_$slug"] = $attribute;
114 $new_variants[] = $new_variant;
115 }
116
117 }
118 }
119
120 // This contains ALL set attributes, and is itself a 'valid' variation.
121 if( $valid )
122 $new_variants[] = $variation;
123
124 }
125
126 return $new_variants;
127}
128
129<?php
130// Get the attributes
131$attributes = $product->get_attributes();
132// Start the loop
133foreach ( $attributes as $attribute ) : ?>
134
135<?php
136// Check and output, adopted from /templates/single-product/product-attributes.php
137 if ( $attribute['is_taxonomy'] ) {
138 $values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
139 echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
140 } else {
141 // Convert pipes to commas and display values
142 $values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
143 echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
144 }
145?>
146
147<?php endforeach; ?>
148
149if ( $attribute->is_taxonomy() ) {
150 $attribute_taxonomy = $attribute->get_taxonomy_object();
151 $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
152
153 foreach ( $attribute_values as $attribute_value ) {
154 $value_name = esc_html( $attribute_value->name );
155
156 if ( $attribute_taxonomy->attribute_public ) {
157 $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
158 } else {
159 $values[] = $value_name;
160 }
161 }
162}