Die Funktion zum Deaktivieren der nicht vorrätigen Produktvariante mit der Meldung “Ausverkauft” kann nicht aktiviert werden

Lesezeit: 2 Minuten

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
}

Mit diesem Code (Ihrem Code) erhalten Sie, was Sie benötigen:

// disable options for unavailable variants
add_filter( 'woocommerce_variation_is_active', 'wcbv_variation_is_active', 10, 2 );
function wcbv_variation_is_active( $active, $variation ) {
    if ( ! $variation->is_in_stock() ) {
        return false;
    }
    return $active;
}

Und jetzt können Sie den Namen jeder einzelnen Option basierend auf dem Lagerstatus ändern woocommerce_variation_option_name Haken so:

add_filter( 'woocommerce_variation_option_name','add_stock_status_after_option_name', 10, 1 );
function add_stock_status_after_option_name( $option ) {
    // only in frontend
    if ( is_admin() ) {
        return $option;
    }
    // get variable product object
    global $product;
    $variation_ids = $product->get_children();
    foreach ( $variation_ids as $variation_id ) {
        $variation = wc_get_product( $variation_id );
        $variation_attributes = $variation->get_variation_attributes();
        foreach ( $variation_attributes as $key => $value ) {
            // slugify option name
            $option_slug = sanitize_title( $option );
            // check if the current option is equal to the variation slug
            if ( $value == $option_slug ) {
                // check if it is out of stock
                if ( ! $variation->is_in_stock() ) {
                    return $option . ' (Out of stock)';
                }
            }
        }
    }
    return $option;
}

Der Code wurde getestet und funktioniert einwandfrei. Es muss zu der functions.php Ihres Themes hinzugefügt werden.

.

295090cookie-checkDie Funktion zum Deaktivieren der nicht vorrätigen Produktvariante mit der Meldung “Ausverkauft” kann nicht aktiviert werden

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy