Holen Sie sich Woocommerce-Variationsattribute

Lesezeit: 2 Minuten

Benutzer-Avatar
Shehab Eltawel

Ich versuche, Variationen eines variablen Produkts auf der benutzerdefinierten Produktseite zu erhalten. Ich habe zwei Attribute, eines für Größen als Auswahl und das andere für Farben als Farbfelder. Das Problem, dass ich das Attribut, das ich anzeigen muss, nicht anzeigen kann, und wenn ich den folgenden Code verwende, gibt er einen Textnamen von Größen oder Farben zurück, nicht das ausgewählte Dropdown-Menü für Größen oder die Farbmuster. Bitte um Hilfe?!

echo implode(', ', wc_get_product_terms( $product_id, 'pa_colors' )); 

Benutzer-Avatar
José Carlos Ramos Carmenates

Dies ist ein kurzer Code, um Ihre Frage zu lösen. Ich lasse Ihnen den gesamten Code, Sie können nur das verwenden, was Sie brauchen.

Die erste ist prüfen, ob get_product Funktion existiert, und überprüfen Sie den Produkttyp, um ein korrektes Produktobjekt mit der ID zu erstellen (in meinem Fall $idProduct).

Es funktioniert auf WooCommerce 3.x, ich teste es nicht auf WooCommerce < 3.x.

if( function_exists('get_product') ) {
        $product = get_product( $idProduct );
        if ( $product->is_type( 'variable' ) ) {

            $product = new WC_Product_Variable( $idProduct );

            $available_variations = $product->get_available_variations(); //get all child variations
            $variation_variations = $product- >get_variation_attributes(); // get all attributes by variations

            // taxonomy           => terms
            // pa_attribute-color => array('blue', 'red', green)
            // Use ex: get_taxonomy('pa_attribute-color')->labels; to get the Name and not the slug to attributes, it can be the taxonomy
            // Use ex: get_term_by('name', 'pa_attribute-color', 'pa_attribute-color); to get the Name/label 

            $result = array( $available_variations , $attributes);  // only to see the result you can use var_dump, error_log, etc.
            //...
            //... 
        }elseif ( $product->is_type( 'bundle' ) && class_exists( 'WC_Product_Bundle' ) ) {
            $product = new WC_Product_Bundle( $idProduct );
        }else{
            $product = new WC_Product( $idProduct );
        }
    }

Auch Sie versuchen mit:

$product->get_attribute( $key );
wc_attribute_label($key);

wo $key kann sein pa_farbe , pa_größeetc

Ich hoffe dir zu helfen.

1364870cookie-checkHolen Sie sich Woocommerce-Variationsattribute

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

Privacy policy