In meiner functions.php versuche ich, im Dropdown-Menü für meine Produktvarianten eine Ausverkaufsmeldung hinzuzufügen. Wenn ich zum Beispiel ein Hemd mit den Varianten klein, mittel und groß habe und das große nicht auf Lager ist, sollte der Benutzer im Dropdown-Menü sehen, dass die große Option deaktiviert ist und eine ausverkaufte Meldung neben ‘Large’ enthält. Die anderen Varianten sollen aktiv bleiben.
Das Problem, das ich mit meinem folgenden Code habe, ist folgendes:
- Der kommentierte Code deaktiviert die richtige Produktvariante, die nicht auf Lager ist, fügt jedoch nicht die Ausverkaufsmeldung hinzu.
- Der aktive Code fügt zwar die Ausverkaufsmeldung hinzu, deaktiviert jedoch alle Produktvarianten, obwohl nur eine Variante ausverkauft ist.
Wie kann ich den Code reparieren, um das zu tun, was ich brauche?
/**
* Disable out of stock variations
* https://github.com/woocommerce/woocommerce/blob/826af31e1e3b6e8e5fc3c1004cc517c5c5ec25b1/includes/class-wc-product-variation.php
* @return Boolean
*/
// function wcbv_variation_is_active( $active, $variation ) {
// if( ! $variation->is_in_stock() ) {
// return false;
// }
// return $active;
// }
// add_filter( 'woocommerce_variation_is_active', 'wcbv_variation_is_active', 10, 2 );
add_action( 'woocommerce_variation_is_active', 'woocommerce_sold_out_dropdown' );
function woocommerce_sold_out_dropdown() {
?>
<script type="text/javascript">
jQuery( document ).bind( 'woocommerce_update_variation_values', function() {
jQuery( '.variations select option' ).each( function( index, el ) {
var sold_out="<?php _e( "sold out', 'woocommerce' ); ?>';
var re = new RegExp( ' - ' + sold_out + '$' );
el = jQuery( el );
if ( el.is( ':disabled' ) ) {
if ( ! el.html().match( re ) ) el.html( el.html() + ' - ' + sold_out );
} else {
if ( el.html().match( re ) ) el.html( el.html().replace( re,'' ) );
}
} );
} );
</script>
<?php
}