سلام مجدد
کد زیر را اضافه کنید
کارکرد کد زیر به اینصورت هست که اولین متغیر در لیست تغییرات بصورت پیشفرض انتخاب میشود و اگر متغیر اول در دسترس نبود یا فروش متوقف بود آیتم بعدی بعنوان آیتم پیشفرض متغیر انتخاب میشود
add_filter( 'woocommerce_product_get_default_attributes', 'sevenhost_default_attribute', 10, 1 );
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'sevenhost_attribute_args_v2', 10, 1 );
function sevenhost_attribute_args_v2( $args = array() ) {
$args[ 'show_option_none' ] = false;
return $args;
}
function sevenhost_default_attribute( $defaults ) {
global $product;
if ( !$product ) {
return $defaults;
}
if ( $product->post_type !== 'product' ) {
return $defaults;
}
$sortby = apply_filters( 'sevenhost_custom_sortby', 'position' );
$thensort = apply_filters( 'sevenhost_custom_then_sortby', 'default' );
$hide_oos = 'yes' == get_option( 'woocommerce_hide_out_of_stock_items' );
if ( empty( $sortby ) ) {
$sortby = 'position';
}
if ( !$product->is_type( 'variable' ) ) {
return $defaults;
}
$children = $product->get_children();
$attributes = array();
foreach( $children as $key => $child ) {
$_child = wc_get_product( $child );
$position = array_search( $key, array_keys( $children ) );
$stock_qty = $_child->get_stock_quantity();
$sales = $_child->get_total_sales();
$stock_status = $_child->is_in_stock();
if ( $hide_oos && !$stock_status ) {
continue;
}
if ( $_child->get_status() == 'publish' ) {
$attributes[] = apply_filters( 'sevenhost_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 ) );
}
}
$secondary_sort = false;
switch( $sortby ) {
case 'price-low':
$secondary_sort = true;
$attributes = sevenhost_multidimensional_sort( $attributes, 'price-low' );
break;
case 'price-high':
$secondary_sort = true;
$attributes = sevenhost_multidimensional_sort( $attributes, 'price-high' );
break;
case 'position':
$attributes = sevenhost_multidimensional_sort( $attributes, 'position' );
break;
case 'id' :
$attributes = sevenhost_multidimensional_sort( $attributes, 'id' );
break;
default:
$secondary_sort = apply_filters( 'sevenhost_do_secondary_sort', true );
$attributes = apply_filters( 'sevenhost_trigger_sort', $attributes );
break;
}
if ( empty( $attributes ) ) {
return $defaults;
}
if ( $secondary_sort ) {
$attributes = sevenhost_secondary_sort( $attributes, $thensort, $sortby );
}
$stock_status = array();
$count = count( $attributes );
for( $i = 0; $i < $count; $i++ ) {
$_prod = wc_get_product( $attributes[$i]['id'] );
$stock = $_prod->get_stock_status();
if ( $stock == 'outofstock' ) {
$stock_status[$i] = 'outofstock';
} else {
$stock_status[$i] = 'instock';
}
}
if ( count( array_unique( $stock_status ) ) > 1 && count( array_unique( $stock_status ) ) < count( $attributes ) ) {
foreach( $stock_status as $key => $value ) {
if ( $value == 'outofstock' ) {
unset( $attributes[$key] );
}
}
}
$attributes = array_values($attributes);
$_prod = !empty( $attributes[0]['id'] ) ? wc_get_product( $attributes[0]['id'] ) : false;
if ( empty( $_prod ) ) {
return apply_filters( 'sevenhost_attributes_return', $defaults );
}
$attr = sevenhost_populate_empty_attributes( $_prod->get_attributes(), $_prod );
$defaults = array();
foreach( $attr as $key => $value ) {
$defaults[$key] = $value;
}
return apply_filters( 'sevenhost_attributes_return', $defaults );
}
function sevenhost_secondary_sort( $attributes, $sortby, $origial_sort ) {
$attribute_split = array();
foreach( $attributes as $akey => $avalue ) {
$attribute_split[$avalue['price']][] = $avalue;
}
foreach( $attribute_split as $skey => $split ) {
switch ( apply_filters( 'sevenhost_secondary_sort_switch', $sortby ) ) {
//Sort using the Secondary filter - Currently defaults to Position, so don't change anything if set to Position
case 'then_sales':
$split = sevenhost_multidimensional_sort( $split, 'sales' );
break;
case 'then_id':
$split = sevenhost_multidimensional_sort( $split, 'id' );
break;
case 'then_stock' :
$split = sevenhost_multidimensional_sort( $split, 'stock' );
break;
default:
$split = apply_filters( 'sevenhost_trigger_sort', $split );
break;
}
$attribute_split[$skey] = $split;
}
$attributes = sevenhost_array_flatten( $attribute_split );
return apply_filters( 'sevenhost_secondary_sort_filter', $attributes );
}
function sevenhost_array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $value);
}
else {
$result[$key] = $value;
}
}
return $result;
}
function sevenhost_populate_empty_attributes( $attributes, $product ) {
foreach( $attributes as $a_key => $a_value ) {
if ( empty( $a_value ) ) {
$parent_id = wc_get_product( $product->get_id() )->get_parent_id();
if ( strpos( $a_key, 'pa_' ) !== false ) {
$attrs = wc_get_product_terms( $parent_id, $a_key, array( 'fields' => 'names' ) );
} else {
$attrs = sevenhost_get_product_attributes( $parent_id, $a_key );
}
$attr = array_shift( $attrs );
if ( !empty( $attr ) ) {
$attributes[$a_key] = strtolower( str_replace( ' ', '_', $attr ) );
}
}
}
return apply_filters( 'sevenhost_empty_attribute_return', $attributes );
}
function sevenhost_get_product_attributes( $product_id, $a_key ) {
$attributes = get_post_meta( $product_id, '_product_attributes', true )[$a_key];
$attribute_array = array();
if ( !empty( $attributes['value'] ) ) {
$attribute_array = explode( '|', str_replace( ' | ', '|', $attributes['value'] ) );
}
return $attribute_array;
}
function sevenhost_multidimensional_sort( $array, $check ) {
usort( $array, 'sevenhost_sortByPosition' );
return apply_filters( 'sevenhost_sort_filter', $array );
}
function sevenhost_sortByPosition($a, $b) {
return $a['position'] - $b['position'];
}
add_filter( 'woocommerce_hide_invisible_variations', 'sevenhost_hide_invisible_variants' );
function sevenhost_hide_invisible_variants() {
return apply_filters( 'sevenhost_hide_unavailable_variants', true );
}