Durch Kommas getrennte Produktattributbegriffe, wenn mehrere Werte verfügbar sind

Jan 13 2021

In WooCommerce baue ich derzeit eine Funktion, die einige Produktattribute auf der Shop-Seite wiedergibt. Ich möchte diese durch Kommas trennen, wenn mehrere Attributwerte verfügbar sind, aber ich weiß nicht wie.

Mein Code:

add_action('woocommerce_after_shop_loop_item_title', 'TitleVariations', 10);
function TitleVariations()
{
global $product; $colormonth = $product->get_attribute('color-month'); $finish = $product->get_attribute('finish'); $design = $product->get_attribute('design'); echo '<span class="variation-display">'; echo __($colormonth, 'woocommerce');
echo __($finish, 'woocommerce'); echo __($crossdesign, 'woocommerce');
echo '</span>';
}

Antworten

2 LoicTheAztec Jan 13 2021 at 22:50

Die WC_Product-Methode get_attribute()gibt eine durch Kommas getrennte Zeichenfolge von Werten an, wenn mehr als ein Wert vorhanden ist. Sie müssen auch überprüfen, ob für jedes Attribut ein Begriff in der Liste enthalten ist.

Um den Namen der Produktattributbezeichnung zu erhalten, können Sie die wc_attribute_label()Produktattributfunktion verwenden.

1). Wenn Sie jedes Produktattribut mit dem Beschriftungsnamen und dem Wert (den Begriffen) (jedem anderen Attribut in einer Zeile) erhalten möchten, verwenden Sie stattdessen Folgendes.

Dieser Code behandelt auch benutzerdefinierte Produktattribute :

add_action('woocommerce_after_shop_loop_item_title', 'display_loop_product_attributtes', 10);
function display_loop_product_attributtes()
{
    global $product; // Here define your product attribute names (slugs) $attribute_names = array('color-month', 'finish', 'design'); 
    $attributes = array(); // Initializing // Loop Through product attributes array foreach( $attribute_names as $attribute_name ) { if( taxonomy_exists( 'pa_' . $attribute_name )  ) {
            $attribute = 'pa_' . $attribute_name; // Custom taxonomy
        } else {
            $attribute = $attribute_name; // Custom attribute (not a taxonomy)
        }

        $values_str = $product->get_attribute($attribute); if ( $values_str ) {
            $attributes[] = '<strong>' . wc_attribute_label($attribute) . ':</strong> ' . $values_str; } } // Output product attribute label / values pairs (one by line) if( ! empty( $attributes ) ) { 
        echo '<span class="variation-display">' . implode( '<br>', $attributes ) . '</span>';
    }
}

2). Wenn Sie jedoch alle Begriffe Ihrer Produktattribute als durch Kommas getrennte Zeichenfolge erhalten möchten, lautet Ihr Code wie unter Anzeigen bestimmter Produktattribute unter dem Produkttitel auf den Woocommerce-Archivseiten .

Also für deinen Code:

add_action('woocommerce_after_shop_loop_item_title', 'display_loop_product_attributtes', 10);
function display_loop_product_attributtes()
{
    global $product;
        
    $color_month = $product->get_attribute('color-month');
    $finish = $product->get_attribute('finish');
    $design = $product->get_attribute('design');

    $attributes = array(); // Initializing if ( $color_month ) {
        $attributes[] = $color_month;
    }
    if (  $finish ) { $attributes[] = $finish; } if ( $design ) {
        $attributes[] = $design;
    }

    // Output product attribute values
    if( ! empty( $attributes ) ) { echo '<span class="variation-display">' . implode( ', ', $attributes ) . '</span>';
    }
}

Der Code befindet sich in der Datei functions.php des aktiven untergeordneten Themas (oder des aktiven Themas). Es sollte funktionieren.

3 u_mulder Jan 13 2021 at 22:14

Sammeln Sie Ihre Werte für das Array und dann für implodedieses Array:

$values = [ __($colormonth, 'woocommerce'),
    __($finish, 'woocommerce'), __($crossdesign, 'woocommerce'),
];

// if some values returned by `__()` are empty strings, 
// you can filter your array so as to remove them
$values = array_filter($values);

echo '<span class="variation-display">';
echo implode(', ', $values);
echo '</span>';