Ausblenden der Schaltfläche „In den Einkaufswagen“ in Woocommerce-Produktvariationen für einen bestimmten Attributwert

Lesezeit: 2 Minuten

So aktualisieren Sie den Bestand ahnlicher Varianten nach dem Kauf
Jashar

In Woocommerce versuche ich, die Schaltfläche „In den Warenkorb“ für Variationen mit einem bestimmten ausgewählten Wert für eines der Attribute auszublenden. Es gibt zwei Attribute für jede Variante (pa_color und pa_size) Zum Beispiel haben wir für ein variables Produkt diese Optionen:

1) Red - XL
2) Red - XXL
3) Blue - M
4) Blue - XL

Ich möchte die Schaltfläche „In den Einkaufswagen“ ausblenden XL Benutzer konnten also keine Optionen mit XL zum Einkaufswagen hinzufügen (1 und 4 in diesem Beispiel).

PS: Wir möchten die Variation nicht deaktivieren, daher könnte das Variationsbild angezeigt werden, wenn Sie diese Option auswählen. Daher ist das Deaktivieren der Variation oder das Entfernen des Preises und … keine Lösung für uns.

Ausblenden der Schaltflache „In den Einkaufswagen in Woocommerce Produktvariationen fur einen
LoicTheAztec

Hier ist der Weg zu machen Schaltfläche zum Warenkorb hinzufügen inaktiv auf Produktvariationen welches Produktattribut “pa_size” mit einer “XL” Wert:

add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {

    ## ---- Your settings ---- ##

    $taxonomy  = 'pa_size';
    $term_name="XL";

    ## ---- The active code ---- ##

    $found = false;

    // Loop through all product attributes in the variation
    foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
        $attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
        $term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
        // Searching for attribute 'pa_size' with value 'XL'
        if($attribute_taxonomy == $taxonomy && $term->name == $term_name ){
            $found = true;
            break;
        }
    }

    if( $found )
        $purchasable = false;

    return $purchasable;
}

Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder Designs) eingefügt. Getestet und funktioniert.

  • Danke dafür. Ich kann das anscheinend nicht mit meinen benutzerdefinierten Attributen oder Attributen nur mit Text zum Laufen bringen. Es funktioniert perfekt mit Attributen, die nur Zahlen enthalten.

    – Stanley Tan

    21. Juni 2019 um 3:34 Uhr

Verwenden Sie Ihren Slug-Text für term_name

add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {

    ## ---- Your settings ---- ##

    $taxonomy  = 'pa_size';
    $term_name="XL";

    ## ---- The active code ---- ##

    $found = false;

    // Loop through all product attributes in the variation
    foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
        $attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
        $term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
        // Searching for attribute 'pa_size' with value 'XL'
        if($attribute_taxonomy == $taxonomy && $term->slug == $term_name ){
            $found = true;
            break;
        }
    }

    if( $found )
        $purchasable = false;

    return $purchasable;
}

917570cookie-checkAusblenden der Schaltfläche „In den Einkaufswagen“ in Woocommerce-Produktvariationen für einen bestimmten Attributwert

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

Privacy policy