Wählen Sie Woocommerce-bezogene Produkte mithilfe einer benutzerdefinierten Taxonomie mit 3-Ebenen-Hierarchie aus
P粉037215587
P粉037215587 2024-04-06 20:05:30
0
1
729

Ich habe einen Woocommerce-Shop mit der benutzerdefinierten Kategorie „Sport“. Die Klassifizierung hat drei Ebenen – Eltern, Kind, Kind – zum Beispiel: Indoor-Sport > Arena-Sport > Wenn der Benutzer das Basketball-Element anzeigt, möchte ich, dass die zugehörigen Produkte zuerst andere Basketball-Elemente anzeigen und dann auf das Arena-Sport-Element zurückgreifen, wenn nicht genügend Basketball-Elemente vorhanden sind. Daher wird zuerst die unterste Ebene überprüft – Sub-Child, dann Child, dann Parent.

Außerdem verwende ich RankMath und kann den Klassifizierungsbegriff als „primären“ Begriff festlegen. Im obigen Beispiel ist der Hauptbegriff also Basketball. Hauptbegriffe sind fast immer Unter-Unterbegriffe, können aber auch Unterbegriffe sein.

Ich habe die Antworten der anderen beiden Fragen kombiniert und der Code funktioniert durch die Auswahl relevanter Produkte aus der Kategorie Sport. Aber es betrachtet nur Unterebenen, nicht Unter-Unterebenen. Im obigen Beispiel werden also die Elemente in Arena Sports ausgewählt, ohne Basketball zu priorisieren.

Welche Änderungen sollte ich vornehmen, um sicherzustellen, dass verwandte Produkte den „Haupt“-Taxonomiebegriff sehen und dann zuerst nach Produkten mit diesem Begriff und dann (falls erforderlich) auf der nächstniedrigeren Ebene der Begriffshierarchie suchen?

Vielen Dank für jede Hilfe oder jeden Rat, den Sie geben können.

Ich habe mich bei der Ausarbeitung des Codes auf diese beiden Artikel bezogen:

WooCommerce-bezogene Produkte nach Kinderkategorie als Ersatz für die Rangfolge der Mathe-Hauptkategorie

Verwenden Sie benutzerdefinierte Taxonomien, um relevante Produkte in Woocommerce auszuwählen

Dies ist der Code, den ich derzeit verwende:

add_filter( 'woocommerce_related_products', 'related_products_from_rankmath_primary_esporte_taxonomy', 10, 3 );
    function related_products_from_rankmath_primary_esporte_taxonomy( $related_posts, $product_id, $args ) {
        $taxonomy     = 'sport'; 
        $term_ids     = wp_get_post_terms( $product_id, $taxonomy, array( 'fields' => 'ids' ) ); 
        $term_slugs   = array(); 
  
 if( count($term_ids) == 1 ) {
        // Get children categories
        $children_ids = get_term_children( reset($category_ids), $taxonomy );
        // Loop through children terms Ids
        foreach ( $children_ids as $tem_id ) {
            $term_slugs[] = get_term_by( 'id', $tem_id, $taxonomy )->slug; // get the slug from each term Id
        }
    } 
    elseif( count( $term_ids ) > 1 ) {
        // Get the primary taxonomy/term as saved by Rank Math SEO
        $primary_tax_id = get_post_meta( $product_id, 'rank_math_primary_taxonomy', true );
        $term_slugs[]   = get_term_by( 'id', $primary_tax_id, $taxonomy )->slug; // get the slug from the term Id
    }
        
        
        if ( count( $term_ids ) > 0 ) {
            foreach ( $term_ids as $term_id ) {
                $term_slugs[] = get_term_by( 'id', $term_id, $taxonomy )->slug; 
    
                // Gets the IDs of child terms
                $children_ids = get_term_children( $term_id, $taxonomy );
    
                foreach ( $children_ids as $child_id ) {
                    $term_slugs[] = get_term_by( 'id', $child_id, $taxonomy )->slug; // Gets the slug of each child term
                }
            }
   
   
            $related_posts = wc_get_products( array(
                'status'        => 'publish',
                'tax_query'     => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field'    => 'slug',
                        'terms'    => $term_slugs,
                    ),
                ),
                'return'        => 'ids',
                'exclude'       => array( $product_id ),
                'visibility'    => 'catalog',
                'limit'         => -1,
            ) );
        }

    
    
        return $related_posts;
    }

P粉037215587
P粉037215587

Antworte allen(1)
P粉029327711

尝试以下操作(已评论):

// Utility function: Get related product Ids with a custom tax query
function get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id ) {
    return wc_get_products( array(
        'limit'         => -1,
        'status'        => 'publish',
        'exclude'       => array( $product_id ),
        'visibility'    => 'catalog',
        'tax_query'     => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'slug',
                'terms'    => $term_slugs,
            ),
        ),
        'return'        => 'ids',
    ) );
}

add_filter( 'woocommerce_related_products', 'related_products_from_rank_math_primary_category', 10, 3 );
function related_products_from_rank_math_primary_category( $related_posts, $product_id, $args  ) {
    // Get product categories set for the product
    $category_ids = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids') ); 
    $term_slugs   = array(); // Initializing   

    // 1). Only one product category => Fallback (Query products from "Sport" taxonomy)
    if( count($category_ids) == 1 ) {
        $taxonomy      = 'sport';
        // Get "Sport" the term set in the product
        $sport_ids     = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'ids') ); 
        $term          = get_term_by( 'id', reset($sport_ids), $taxonomy );
        $term_slugs[]  = $term ->slug;
        // Get related products from the "Sport" term
        $related_posts = get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );

        // IF there is not enough related products: Add the CHILDREN terms
        if ( $related_posts  0 ) {
                foreach ( $children_ids as $tem_id ) {
                    $term_slugs[] = get_term_by( 'id', $tem_id, $taxonomy )->slug; // get the slug from each term Id
                }
                // Get related products from the "Sport" terms
                $related_posts = get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );
            }
            // IF there is not enough related products: Add the PARENT term
            if ( $related_posts parent, $taxonomy );
                $term_slugs[] = $parent ->slug;
                // Get related products from the "Sport" terms
                return get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );
            } else {
                return $related_posts;
            }
        } else {
            return $related_posts;
        }
    } 
    // 2). More than one product categories => Rank Math SEO
    elseif( count( $category_ids ) > 1 ) {
        // Get the primary category/term as saved by Rank Math SEO
        $primary_cat_id = get_post_meta( $product_id, 'rank_math_primary_product_cat', true );
        $taxonomy       = 'product_cat';
        $term_slugs[]   = get_term_by( 'id', $primary_cat_id, $taxonomy )->slug; // get the slug from the term Id
        // Get related products from the category terms via Rank Math SEO
        return get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );
    }
    return $related_posts;
}

它应该可以工作。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!