Last active
July 10, 2021 10:12
-
-
Save FrancoStino/2faccd7fa0e14a7268cb55c6d07aab77 to your computer and use it in GitHub Desktop.
Add custom avaibility in stock status Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
* Add custom avaibility in stock status Woocommerce | |
*/ | |
// Add new stock status options | |
function filter_woocommerce_product_stock_status_options( $status ) { | |
// Add new statuses | |
$status['in_arrivo'] = __( 'In arrivo', 'woocommerce' ); | |
$status['10_gg'] = __( 'Disponibile in 10 giorni', 'woocommerce' ); | |
return $status; | |
} | |
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 ); | |
// Availability text | |
function filter_woocommerce_get_availability_text( $availability, $product ) { | |
switch( $product->get_stock_status() ) { | |
case 'in_arrivo': | |
$availability = __( 'In arrivo', 'woocommerce' ); | |
break; | |
case '10_gg': | |
$availability = __( 'Disponibile in 10 giorni', 'woocommerce' ); | |
break; | |
} | |
return $availability; | |
} | |
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 ); | |
// Availability class | |
function filter_woocommerce_get_availability_class( $class, $product ) { | |
switch( $product->get_stock_status() ) { | |
case 'in_arrivo': | |
$class = 'in_arrivo'; | |
break; | |
case '10_gg': | |
$class = '10_gg'; | |
break; | |
} | |
return $class; | |
} | |
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 ); | |
// admin stock html | |
function filter_woocommerce_admin_stock_html( $stock_html, $product ) { | |
if( $product->is_type( 'variable' ) ){ | |
foreach( $product->get_visible_children() as $variation_id ){ | |
$variation = new WC_Product_Variation( $variation_id ); | |
switch( $variation->get_stock_status() ) { | |
case 'in_arrivo': | |
$stock_html = '<mark class="in_arrivo" style="background:none;color:#eaa600;font-weight:700;line-height:1;">' . __( 'In arrivo', 'woocommerce' ) . '</mark>'; | |
break; | |
case '10_gg': | |
$stock_html = '<mark class="10_gg" style="background:none;color:#eaa600;font-weight:700;line-height:1;">' . __( 'Disponibile in 10 giorni', 'woocommerce' ) . '</mark>'; | |
break; | |
} | |
return $stock_html; | |
} | |
} elseif ( $product->is_type( 'simple' ) ){ | |
switch( $product->get_stock_status() ) { | |
case 'in_arrivo': | |
$stock_html = '<mark class="in_arrivo" style="background:none;color:#eaa600;font-weight:700;line-height:1;">' . __( 'In arrivo', 'woocommerce' ) . '</mark>'; | |
break; | |
case '10_gg': | |
$stock_html = '<mark class="10_gg" style="background:none;color:#eaa600;font-weight:700;line-height:1;">' . __( 'Disponibile in 10 giorni', 'woocommerce' ) . '</mark>'; | |
break; | |
} | |
return $stock_html; | |
} | |
} | |
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 ); | |
/* | |
* Style select and option dropdown stock status in head admin - Product option page - Woocommerce | |
*/ | |
add_action('admin_footer', 'my_custom_fonts'); | |
function my_custom_fonts() { | |
echo '<style> | |
[value*="instock"] { | |
background-color: green; | |
color: #fff; | |
} | |
[value*="outofstock"] { | |
background: red; | |
color: #fff; | |
} | |
[value*="onbackorder"], [value*="in_arrivo"], [value*="10_gg"] { | |
background: orange; | |
color: black; | |
} | |
</style>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment