· 8 years ago · Nov 17, 2017, 04:42 AM
1function find_valid_variations() {
2 global $product;
3
4 $variations = $product->get_available_variations();
5 $attributes = $product->get_attributes();
6 $new_variants = array();
7
8 // Loop through all variations
9 foreach( $variations as $variation ) {
10 // Peruse the attributes.
11
12 // 1. If both are explicitly set, this is a valid variation
13 // 2. If one is not set, that means any, and we must 'create' the rest.
14
15 $valid = true; // so far
16 foreach( $attributes as $slug => $args ) {
17 if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
18 // Exists
19
20 } else {
21 // Not exists, create
22 $valid = false; // it contains 'anys'
23 // loop through all options for the 'ANY' attribute, and add each
24 foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
25 $attribute = trim( $attribute );
26 $new_variant = $variation;
27 $new_variant['attributes']["attribute_$slug"] = $attribute;
28 $new_variants[] = $new_variant;
29 }
30
31 }
32 }
33
34 // This contains ALL set attributes, and is itself a 'valid' variation.
35 if( $valid )
36 $new_variants[] = $variation;
37
38 }
39
40 return $new_variants;
41}
42function list_price_variable(){
43 global $product, $post;
44
45 $variations = find_valid_variations();
46
47 // Check if the special 'price_grid' meta is set, if it is, load the default template:
48 if ( get_post_meta($post->ID, 'price_grid', true) ) {
49 // Enqueue variation scripts
50 wp_enqueue_script( 'wc-add-to-cart-variation' );
51
52 // Load the template
53 wc_get_template( 'single-product/add-to-cart/variable.php', array(
54 'available_variations' => $product->get_available_variations(),
55 'attributes' => $product->get_variation_attributes(),
56 'selected_attributes' => $product->get_variation_default_attributes()
57 ) );
58 return;
59 }
60
61 // Cool, lets do our own template!
62 ?>
63 <table class="variations variations-grid" cellspacing="0">
64 <tbody>
65 <?php
66 foreach ($variations as $key => $value) {
67 if( !$value['variation_is_visible'] ) continue;
68 ?>
69 <tr>
70 <td>
71 <?php foreach($value['attributes'] as $key => $val ) {
72 $val = str_replace(array('-','_'), ' ', $val);
73 $category_slug = str_replace('attribute_', '', $key);
74 $category = get_term_by('slug', ucwords($val), $category_slug);
75 $categoryName = $category->name.' ';
76 printf( '<span class="attr attr-%s">%s</span>', $key, $categoryName);
77 } ?>
78 </td>
79 <td>
80 <?php echo $value['price_html'];?>
81 </td>
82 </tr>
83 <?php } ?>
84 </tbody>
85 </table>
86 <?php
87}
88function wc_wc20_variation_price_format( $price, $product ) {
89 $price = list_price_variable();
90 return $price;
91}
92//add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
93add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );