Changeset 2763450
- Timestamp:
- 07/29/2022 11:54:53 AM (4 years ago)
- Location:
- force-default-variant-for-woocommerce
- Files:
-
- 10 added
- 3 edited
-
tags/1.6.0/languages (added)
-
tags/1.6.1 (added)
-
tags/1.6.1/functions.php (added)
-
tags/1.6.1/includes (added)
-
tags/1.6.1/includes/clear-removal.php (added)
-
tags/1.6.1/includes/settings.php (added)
-
tags/1.6.1/includes/variations.php (added)
-
tags/1.6.1/languages (added)
-
tags/1.6.1/readme.txt (added)
-
tags/1.6.1/woo-force-default-variant.php (added)
-
trunk/includes/variations.php (modified) (7 diffs)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/woo-force-default-variant.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
force-default-variant-for-woocommerce/trunk/includes/variations.php
r2750805 r2763450 1 1 <?php /* Variant DropDown menu changes */ 2 2 if ( hpy_fdv_wc_version_check( '3.0' ) ) { 3 add_filter( 'woocommerce_product_get_default_attributes', 'hpy_fdv_default_attribute', 10, 1 );4 add_filter( 'woocommerce_dropdown_variation_attribute_options_ html', 'hpy_fdv_attribute_v2', 20, 2 );3 add_filter( 'woocommerce_product_get_default_attributes', 'hpy_fdv_default_attribute', 10, 1 ); 4 add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'hpy_fdv_attribute_v2', 20, 2 ); 5 5 } else { 6 add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'hpy_fdv_attribute_args', 10, 1 );6 add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'hpy_fdv_attribute_args', 10, 1 ); 7 7 } 8 8 … … 17 17 */ 18 18 function hpy_fdv_attribute_args( $args = array() ) { 19 $sortby = !empty( get_option( 'hpy_variant_sort' ) ) ? get_option( 'hpy_variant_sort' ) : 'id';20 21 $product = $args['product'];22 $attribute = strtolower( $args['attribute'] );23 24 $product_class = new WC_Product_Variable( $product );25 $children = $product_class->get_visible_children();26 $i = 0;27 if ( !empty( $children ) ) {28 foreach ( $children as $child ) {29 $required = 'attribute_' . $attribute;30 $meta = get_post_meta( $child );31 $to_use = $meta[ $required ][ 0 ];32 $product_child = new WC_Product_Variation( $child );33 $prices[ $i ] = array( $product_child->get_price(), $to_use );34 $i ++;35 }36 37 if ( $sortby == 'price-low' || $sortby == 'price-high' ) {38 if ( isset( $prices ) ) {39 if ( $sortby == 'price-low' ) {40 sort( $prices );41 } else {42 rsort( $prices );43 }44 }45 }46 $args[ 'selected' ] = $prices[ 0 ][ 1 ];47 48 $args[ 'show_option_none' ] = '';49 }50 51 return $args;19 $sortby = !empty( get_option( 'hpy_variant_sort' ) ) ? get_option( 'hpy_variant_sort' ) : 'id'; 20 21 $product = $args['product']; 22 $attribute = strtolower( $args['attribute'] ); 23 24 $product_class = new WC_Product_Variable( $product ); 25 $children = $product_class->get_visible_children(); 26 $i = 0; 27 if ( !empty( $children ) ) { 28 foreach ( $children as $child ) { 29 $required = 'attribute_' . $attribute; 30 $meta = get_post_meta( $child ); 31 $to_use = $meta[ $required ][ 0 ]; 32 $product_child = new WC_Product_Variation( $child ); 33 $prices[ $i ] = array( $product_child->get_price(), $to_use ); 34 $i ++; 35 } 36 37 if ( $sortby == 'price-low' || $sortby == 'price-high' ) { 38 if ( isset( $prices ) ) { 39 if ( $sortby == 'price-low' ) { 40 sort( $prices ); 41 } else { 42 rsort( $prices ); 43 } 44 } 45 } 46 $args[ 'selected' ] = $prices[ 0 ][ 1 ]; 47 48 $args[ 'show_option_none' ] = ''; 49 } 50 51 return $args; 52 52 53 53 } … … 56 56 * Remove the Choose an Option text - if applicable 57 57 * 58 * @param $html59 58 * @param $args 60 59 * 61 60 * @return array|mixed|string|string[] 62 61 */ 63 function hpy_fdv_attribute_v2( $html, $args ) { 64 if ( get_option('hpy_disabled_auto_remove_dropdown') == 'yes' ) { 65 return $html; 66 } 67 68 $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); 69 $show_option_none_html = '<option value="">' . esc_html( $show_option_none_text ) . '</option>'; 70 71 $html = str_replace($show_option_none_html, '', $html); 72 73 return $html; 62 63 function hpy_fdv_attribute_v2( $args ) { 64 if ( get_option('hpy_disabled_auto_remove_dropdown') != 'yes' ) { 65 $args['show_option_none'] = false; 66 } 67 68 return $args; 74 69 } 75 70 76 71 function hpy_fdv_default_attribute( $defaults ) { 77 global $product;78 79 if ( !$product ) {80 return $defaults;81 }82 83 if ( $product->post_type !== 'product' ) {84 return $defaults;85 }86 87 $respect = get_option( 'hpy_variant_respect' );88 $sortby = apply_filters( 'hpy_fdv_custom_sortby', !empty( get_option( 'hpy_variant_sort' ) ) ? get_option( 'hpy_variant_sort' ) : 'id' );89 $thensort = apply_filters( 'hpy_fdv_custom_then_sortby', !empty( get_option( 'hpy_variant_then_sort' ) ) ? get_option( 'hpy_variant_then_sort' ) : 'default' );90 $hide_oos = 'yes' == get_option( 'woocommerce_hide_out_of_stock_items' );72 global $product; 73 74 if ( !$product ) { 75 return $defaults; 76 } 77 78 if ( $product->post_type !== 'product' ) { 79 return $defaults; 80 } 81 82 $respect = get_option( 'hpy_variant_respect' ); 83 $sortby = apply_filters( 'hpy_fdv_custom_sortby', !empty( get_option( 'hpy_variant_sort' ) ) ? get_option( 'hpy_variant_sort' ) : 'id' ); 84 $thensort = apply_filters( 'hpy_fdv_custom_then_sortby', !empty( get_option( 'hpy_variant_then_sort' ) ) ? get_option( 'hpy_variant_then_sort' ) : 'default' ); 85 $hide_oos = 'yes' == get_option( 'woocommerce_hide_out_of_stock_items' ); 91 86 $first_attribute = ''; 92 93 if ( $respect == 'yes' && !empty( $defaults ) ) {94 return $defaults;95 }96 97 if ( empty( $sortby ) ) {98 $sortby = 'id';99 }100 101 if ( !$product->is_type( 'variable' ) ) {102 return $defaults;103 }104 105 $children = $product->get_children();106 $attributes = array();107 108 foreach( $children as $key => $child ) {109 $_child = wc_get_product( $child );110 $position = array_search( $key, array_keys( $children ) );111 $menu_order = array();87 88 if ( $respect == 'yes' && !empty( $defaults ) ) { 89 return $defaults; 90 } 91 92 if ( empty( $sortby ) ) { 93 $sortby = 'id'; 94 } 95 96 if ( !$product->is_type( 'variable' ) ) { 97 return $defaults; 98 } 99 100 $children = $product->get_children(); 101 $attributes = array(); 102 103 foreach( $children as $key => $child ) { 104 $_child = wc_get_product( $child ); 105 $position = array_search( $key, array_keys( $children ) ); 106 $menu_order = array(); 112 107 $stock_qty = $_child->get_stock_quantity(); 113 $sales = $_child->get_total_sales();114 $stock_status = $_child->is_in_stock();108 $sales = $_child->get_total_sales(); 109 $stock_status = $_child->is_in_stock(); 115 110 $attrs = $_child->get_attributes(); 116 111 foreach( $attrs as $akey => $attr ) { … … 122 117 } 123 118 124 if ( $hide_oos && !$stock_status ) {125 //If Hide out of Stock is set, and this variant is out of stock, then skip.126 continue;127 }128 129 if ( $_child->get_status() == 'publish' ) {130 $attributes[] = apply_filters( 'hpy_fdv_build_attribute_filter', array( 'price' => !empty($_child->get_price()) ? $_child->get_price() : '0' , 'id' => $_child->get_id(), 'position' => $position, 'sales' => $sales, 'stock_level' => $stock_qty, 'menu_order' => $menu_order ) );131 }132 }133 134 $secondary_sort = false;135 136 switch( $sortby ) {137 138 case 'price-low':139 $secondary_sort = true;140 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'price-low' );141 break;142 143 case 'price-high':144 $secondary_sort = true;145 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'price-high' );146 break;147 148 case 'position':149 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'position' );150 break;151 152 case 'id' :153 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'id' );154 break;119 if ( $hide_oos && !$stock_status ) { 120 //If Hide out of Stock is set, and this variant is out of stock, then skip. 121 continue; 122 } 123 124 if ( $_child->get_status() == 'publish' ) { 125 $attributes[] = apply_filters( 'hpy_fdv_build_attribute_filter', array( 'price' => !empty($_child->get_price()) ? $_child->get_price() : '0' , 'id' => $_child->get_id(), 'position' => $position, 'sales' => $sales, 'stock_level' => $stock_qty, 'menu_order' => $menu_order ) ); 126 } 127 } 128 129 $secondary_sort = false; 130 131 switch( $sortby ) { 132 133 case 'price-low': 134 $secondary_sort = true; 135 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'price-low' ); 136 break; 137 138 case 'price-high': 139 $secondary_sort = true; 140 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'price-high' ); 141 break; 142 143 case 'position': 144 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'position' ); 145 break; 146 147 case 'id' : 148 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'id' ); 149 break; 155 150 156 151 case 'menu_order': 157 152 $attributes = hpy_fdv_multidimensional_sort( $attributes, 'menu_order', $first_attribute ); 158 153 break; 159 160 default:161 $secondary_sort = apply_filters( 'hpy_fdv_do_secondary_sort', true );162 $attributes = apply_filters( 'hpy_fdv_trigger_sort', $attributes );163 break;164 165 }166 167 if ( empty( $attributes ) ) {168 return $defaults;169 }170 171 if ( $secondary_sort ) {172 $attributes = hpy_fdv_secondary_sort( $attributes, $thensort, $sortby );173 }174 175 $stock_status = array();176 177 $count = count( $attributes );178 for( $i = 0; $i < $count; $i++ ) {179 $_prod = wc_get_product( $attributes[$i]['id'] );180 181 $stock_limit = get_option( 'hpy_variant_stockLimit' );182 183 if ( !empty( $stock_limit ) ) {184 $stock_qty = $_prod->get_stock_quantity();185 if ( $stock_qty < $stock_limit && !is_null( $stock_qty ) ) {186 $stock = 'outofstock';187 } else {188 $stock = $_prod->get_stock_status();189 }190 } else {191 $stock = $_prod->get_stock_status();192 }193 194 if ( $stock == 'outofstock' ) {195 $stock_status[$i] = 'outofstock';196 } else {197 $stock_status[$i] = 'instock';198 }199 }200 201 if ( count( array_unique( $stock_status ) ) > 1 && count( array_unique( $stock_status ) ) < count( $attributes ) ) {202 foreach( $stock_status as $key => $value ) {203 if ( $value == 'outofstock' ) {204 unset( $attributes[$key] );205 }206 }207 }208 209 $attributes = array_values($attributes);210 211 $_prod = !empty( $attributes[0]['id'] ) ? wc_get_product( $attributes[0]['id'] ) : false;212 213 if ( empty( $_prod ) ) {214 return apply_filters( 'hpy_fdv_attributes_return', $defaults );215 }216 217 $attr = hpy_fdv_populate_empty_attributes( $_prod->get_attributes(), $_prod );218 219 $defaults = array();220 221 foreach( $attr as $key => $value ) {154 155 default: 156 $secondary_sort = apply_filters( 'hpy_fdv_do_secondary_sort', true ); 157 $attributes = apply_filters( 'hpy_fdv_trigger_sort', $attributes ); 158 break; 159 160 } 161 162 if ( empty( $attributes ) ) { 163 return $defaults; 164 } 165 166 if ( $secondary_sort ) { 167 $attributes = hpy_fdv_secondary_sort( $attributes, $thensort, $sortby ); 168 } 169 170 $stock_status = array(); 171 172 $count = count( $attributes ); 173 for( $i = 0; $i < $count; $i++ ) { 174 $_prod = wc_get_product( $attributes[$i]['id'] ); 175 176 $stock_limit = get_option( 'hpy_variant_stockLimit' ); 177 178 if ( !empty( $stock_limit ) ) { 179 $stock_qty = $_prod->get_stock_quantity(); 180 if ( $stock_qty < $stock_limit && !is_null( $stock_qty ) ) { 181 $stock = 'outofstock'; 182 } else { 183 $stock = $_prod->get_stock_status(); 184 } 185 } else { 186 $stock = $_prod->get_stock_status(); 187 } 188 189 if ( $stock == 'outofstock' ) { 190 $stock_status[$i] = 'outofstock'; 191 } else { 192 $stock_status[$i] = 'instock'; 193 } 194 } 195 196 if ( count( array_unique( $stock_status ) ) > 1 && count( array_unique( $stock_status ) ) < count( $attributes ) ) { 197 foreach( $stock_status as $key => $value ) { 198 if ( $value == 'outofstock' ) { 199 unset( $attributes[$key] ); 200 } 201 } 202 } 203 204 $attributes = array_values($attributes); 205 206 $_prod = !empty( $attributes[0]['id'] ) ? wc_get_product( $attributes[0]['id'] ) : false; 207 208 if ( empty( $_prod ) ) { 209 return apply_filters( 'hpy_fdv_attributes_return', $defaults ); 210 } 211 212 $attr = hpy_fdv_populate_empty_attributes( $_prod->get_attributes(), $_prod ); 213 214 $defaults = array(); 215 216 foreach( $attr as $key => $value ) { 222 217 if ( !empty( $value ) ) { 223 218 $defaults[$key] = $value; 224 219 } 225 }226 227 return apply_filters( 'hpy_fdv_attributes_return', $defaults );220 } 221 222 return apply_filters( 'hpy_fdv_attributes_return', $defaults ); 228 223 } 229 224 230 225 function hpy_fdv_secondary_sort( $attributes, $sortby, $origial_sort ) { 231 232 $attribute_split = array();233 foreach( $attributes as $akey => $avalue ) {234 $attribute_split[$avalue['price']][] = $avalue;235 }236 237 foreach( $attribute_split as $skey => $split ) {238 switch ( apply_filters( 'hpy_fdv_secondary_sort_switch', $sortby ) ) {239 240 //Sort using the Secondary filter - Currently defaults to Position, so don't change anything if set to Position241 case 'then_sales':242 $split = hpy_fdv_multidimensional_sort( $split, 'sales' );243 break;244 245 case 'then_id':246 $split = hpy_fdv_multidimensional_sort( $split, 'id' );247 break;248 249 case 'then_stock' :250 $split = hpy_fdv_multidimensional_sort( $split, 'stock' );251 break;252 253 default:254 $split = apply_filters( 'hpy_fdv_trigger_sort', $split );255 break;256 257 }258 259 $attribute_split[$skey] = $split;260 }261 262 $attributes = hpy_fdv_array_flatten( $attribute_split );263 264 return apply_filters( 'hpy_fdv_secondary_sort_filter', $attributes );265 226 227 $attribute_split = array(); 228 foreach( $attributes as $akey => $avalue ) { 229 $attribute_split[$avalue['price']][] = $avalue; 230 } 231 232 foreach( $attribute_split as $skey => $split ) { 233 switch ( apply_filters( 'hpy_fdv_secondary_sort_switch', $sortby ) ) { 234 235 //Sort using the Secondary filter - Currently defaults to Position, so don't change anything if set to Position 236 case 'then_sales': 237 $split = hpy_fdv_multidimensional_sort( $split, 'sales' ); 238 break; 239 240 case 'then_id': 241 $split = hpy_fdv_multidimensional_sort( $split, 'id' ); 242 break; 243 244 case 'then_stock' : 245 $split = hpy_fdv_multidimensional_sort( $split, 'stock' ); 246 break; 247 248 default: 249 $split = apply_filters( 'hpy_fdv_trigger_sort', $split ); 250 break; 251 252 } 253 254 $attribute_split[$skey] = $split; 255 } 256 257 $attributes = hpy_fdv_array_flatten( $attribute_split ); 258 259 return apply_filters( 'hpy_fdv_secondary_sort_filter', $attributes ); 260 266 261 } 267 262 268 263 function hpy_fdv_array_flatten($array) { 269 if (!is_array($array)) {270 return FALSE;271 }272 $result = array();273 foreach ($array as $key => $value) {274 if (is_array($value)) {275 $result = array_merge($result, $value);276 }277 else {278 $result[$key] = $value;279 }280 }281 return $result;264 if (!is_array($array)) { 265 return FALSE; 266 } 267 $result = array(); 268 foreach ($array as $key => $value) { 269 if (is_array($value)) { 270 $result = array_merge($result, $value); 271 } 272 else { 273 $result[$key] = $value; 274 } 275 } 276 return $result; 282 277 } 283 278 284 279 function hpy_fdv_populate_empty_attributes( $attributes, $product ) { 285 286 foreach( $attributes as $a_key => $a_value ) {287 if ( empty( $a_value ) ) {288 $parent_id = wc_get_product( $product->get_id() )->get_parent_id();289 if ( strpos( $a_key, 'pa_' ) !== false ) {290 $attrs = wc_get_product_terms( $parent_id, $a_key, array( 'fields' => 'names' ) );291 } else {292 $attrs = hpy_fdv_get_product_attributes( $parent_id, $a_key );293 }294 $attr = array_shift( $attrs );295 296 if ( !empty( $attr ) ) {297 $attributes[$a_key] = strtolower( str_replace( ' ', '_', $attr ) );298 }299 }300 }301 302 return apply_filters( 'hpy_fdv_empty_attribute_return', $attributes );280 281 foreach( $attributes as $a_key => $a_value ) { 282 if ( empty( $a_value ) ) { 283 $parent_id = wc_get_product( $product->get_id() )->get_parent_id(); 284 if ( strpos( $a_key, 'pa_' ) !== false ) { 285 $attrs = wc_get_product_terms( $parent_id, $a_key, array( 'fields' => 'names' ) ); 286 } else { 287 $attrs = hpy_fdv_get_product_attributes( $parent_id, $a_key ); 288 } 289 $attr = array_shift( $attrs ); 290 291 if ( !empty( $attr ) ) { 292 $attributes[$a_key] = strtolower( str_replace( ' ', '_', $attr ) ); 293 } 294 } 295 } 296 297 return apply_filters( 'hpy_fdv_empty_attribute_return', $attributes ); 303 298 } 304 299 305 300 function hpy_fdv_get_product_attributes( $product_id, $a_key ) { 306 $attributes = get_post_meta( $product_id, '_product_attributes', true )[$a_key];307 308 $attribute_array = array();309 if ( !empty( $attributes['value'] ) ) {310 $attribute_array = explode( '|', str_replace( ' | ', '|', $attributes['value'] ) );311 }312 313 return $attribute_array;301 $attributes = get_post_meta( $product_id, '_product_attributes', true )[$a_key]; 302 303 $attribute_array = array(); 304 if ( !empty( $attributes['value'] ) ) { 305 $attribute_array = explode( '|', str_replace( ' | ', '|', $attributes['value'] ) ); 306 } 307 308 return $attribute_array; 314 309 } 315 310 316 311 function hpy_fdv_multidimensional_sort( $array, $check, $attribute = '' ) { 317 312 318 if ( $check == 'price-low' ) {319 usort( $array, 'hpy_fdv_sortByPrice' );320 } else if ( $check == 'price-high' ) {321 usort( $array, 'hpy_fdv_sortByPriceHigh' );322 } else if ( $check == 'position' ) {323 usort( $array, 'hpy_fdv_sortByPosition' );324 } else if ( $check == 'menu_order' ) {313 if ( $check == 'price-low' ) { 314 usort( $array, 'hpy_fdv_sortByPrice' ); 315 } else if ( $check == 'price-high' ) { 316 usort( $array, 'hpy_fdv_sortByPriceHigh' ); 317 } else if ( $check == 'position' ) { 318 usort( $array, 'hpy_fdv_sortByPosition' ); 319 } else if ( $check == 'menu_order' ) { 325 320 // usort( $array, 'hpy_fdv_sortByAttribute', $attribute ); 326 321 usort($array, function($a, $b) use ($attribute) { … … 328 323 }); 329 324 } else { 330 usort( $array, 'hpy_fdv_sortByID' );331 }332 333 return apply_filters( 'hpy_fdv_sort_filter', $array );334 325 usort( $array, 'hpy_fdv_sortByID' ); 326 } 327 328 return apply_filters( 'hpy_fdv_sort_filter', $array ); 329 335 330 } 336 331 337 332 function hpy_fdv_sortByPrice($a, $b) { 338 return $a['price'] - $b['price'];333 return $a['price'] - $b['price']; 339 334 } 340 335 341 336 function hpy_fdv_sortByPriceHigh($a, $b) { 342 return $b['price'] - $a['price'];337 return $b['price'] - $a['price']; 343 338 } 344 339 345 340 function hpy_fdv_sortByPosition($a, $b) { 346 return $a['position'] - $b['position'];341 return $a['position'] - $b['position']; 347 342 } 348 343 … … 352 347 353 348 function hpy_fdv_sortByID($a, $b) { 354 return $a['id'] - $b['id'];349 return $a['id'] - $b['id']; 355 350 } 356 351 … … 390 385 add_filter( 'woocommerce_hide_invisible_variations', 'hpy_fdv_hide_invisible_variants' ); 391 386 function hpy_fdv_hide_invisible_variants() { 392 return apply_filters( 'hpy_fdv_hide_unavailable_variants', true );393 } 387 return apply_filters( 'hpy_fdv_hide_unavailable_variants', true ); 388 } -
force-default-variant-for-woocommerce/trunk/readme.txt
r2750805 r2763450 1 1 === WooCommerce Force Default Variant === 2 Contributors: HappyKite, morrowmedia, philmorrow 2 Contributors: HappyKite, morrowmedia, philmorrow, obailey 3 3 Tags: WooCommerce, Variable product, WooCommerce variant, eCommerce 4 4 Requires at least: 4.2 5 5 Tested up to: 6.0 6 Stable tag: 1.6. 06 Stable tag: 1.6.1 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 31 31 32 32 == Changelog == 33 = 1.6.1 - 29th July 2022 = 34 * FIX - Issue in 1.6 that caused Out of Stock variations to appear in WooCommerce Hide Out of Stock setting is ticked. 35 33 36 = 1.6.0 - 1st July 2022 = 34 37 * FIX - Sort by Position fixed - This will now correctly sort by the variation order within the Variations tab on each Product page. -
force-default-variant-for-woocommerce/trunk/woo-force-default-variant.php
r2750805 r2763450 7 7 Author URI: http://www.happykite.co.uk/ 8 8 Text Domain: force-default-variant-for-woocommerce 9 Version: 1.6. 09 Version: 1.6.1 10 10 WC requires at least: 2.4 11 11 WC tested up to: 6.6.1
Note: See TracChangeset
for help on using the changeset viewer.