Zeigen Sie verwandte Produkte für einen bestimmten Produktattributwert auf einzelnen Produktseiten an

Lesezeit: 2 Minuten

Zeigen Sie verwandte Produkte fur einen bestimmten Produktattributwert auf einzelnen
Subhonkulov

In WooCommerce habe ich eine eingestellt COLLECTION_ID Produktattribut für meine Produkte.

Ich möchte alle verwandten Produkte dazu anzeigen COLLECTION_ID Produktattributwert auf einzelnen Produktseiten. Irgendeine Hilfe?

Sie müssen Ihre eigenen Einstellungen im Abschnitt “Einstellungen” innerhalb der Funktion hinzufügen. Für den definierten Produktattributnamen verwendet diese Funktion den ersten entsprechenden Ausdruckssatz für das aktuelle Produkt (da ein Attribut viele Begriffe in einem Produkt haben kann) und zeigt alle ähnlichen Produkte direkt vor „Upsell-Produkte“ und „Verwandte Produkte“.

Der Code

add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );
function custom_output_product_collection(){

    ## --- YOUR SETTINGS --- ##

    $attribute = "Color"; // <== HERE define your attribute name
    $limit     = "3";     // <== Number of products to be displayed
    $cols      = "3";     // <== Number of columns
    $orderby   = "rand";  // <== Order by argument (random order here)

    ## --- THE CODE --- ##

    global $post, $wpdb;

    // Formatting the attribute
    $attribute = sanitize_title( $attribute );
    $taxonomy  = 'pa_' . $attribute;

    // Get the WP_Term object for the current product and the defined product attribute
    $terms = wp_get_post_terms( $post->ID, $taxonomy );
    $term = reset($terms);

    // Get all product IDs that have  the same product attribute value (except current product ID)
    $product_ids = $wpdb->get_col( "SELECT DISTINCT tr.object_id
        FROM {$wpdb->prefix}term_relationships as tr
        JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
        JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}'" );

    // Convert array values to a coma separated string
    $ids = implode( ',', $product_ids );

    ## --- THE OUTPUT --- ##

    echo '<section class="'.$attribute.' '.$attribute.'-'.$term->slug.' products">
        <h2>'.__( "Collection", "woocommerce" ).': '.$term->name.'</h2>';

    echo do_shortcode("[products ids="$ids" columns="$cols" limit="$limit" orderby='$orderby']");

    echo '</section>';
}

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

Geben Sie hier die Bildbeschreibung ein

  • Dies ist ein großartiges Code-Snippet, Loic. Wäre es möglich, den Code so zu ändern, dass nur Produkte der gleichen Farbe erhältlich sind? innerhalb derselben Produktkategorie werden geholt?

    – BarrieO

    16. März 2021 um 15:52 Uhr

1004640cookie-checkZeigen Sie verwandte Produkte für einen bestimmten Produktattributwert auf einzelnen Produktseiten an

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

Privacy policy