web-dev-qa-db-fra.com

Filtrez la catégorie dans la page de la boutique WooCommerce pour afficher la sous-catégorie correspondante

Je travaille avec le filtrage par catégorie de produit sans succès. Je tente d'extraire la sous-catégorie des produits à afficher entre les produits title et price sur tous les produits vus dans la page du magasin.

J'ai essayé quelques solutions et je me bats vraiment. Vous trouverez ci-dessous le lien vers la page de la boutique où je tente d’afficher chaque sous-catégorie de la catégorie suivante: Designer -> Designer Name.

J'ai besoin de mettre la sous-catégorie Designer Name entre Title et Price ici: http://glamboutique.wpengine.com/shop/

J'ai fait des progrès. Actuellement, la catégorie parente est affichée, mais comme je l’ai dit, j’ai simplement besoin que la sous-catégorie de Designer (catégorie parente) apparaisse entre Title et Price. Ci-dessous le code:

function wc_shop_product_sub_category() {
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    if ( $product_cats && ! is_wp_error ( $product_cats ) ) {
        $single_cat = array_shift( $product_cats );
        ?>
        <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
        <?php
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_shop_product_sub_category' );

Mettre à jour

Ainsi, après avoir passé en revue, j'ai trouvé la solution avec la réponse de Fayaz comme point de départ:

function wc_shop_product_sub_category() {
    global $post;
    // grabs a specific category based on the slug
    $brands_id = get_term_by('slug', 'designers', 'product_cat');

    // gets subcats and loops through them to display
    $terms = get_the_terms($post->ID, 'product_cat');
    foreach ($terms as $term) {
        if($term->parent === $brands_id->term_id) { ?>
            <h6 class="product-sub-cats"><?php echo $term->name; ?></h6>
            <?php  break;
         }
    }
}
// this will place the designers between title & price
add_action( 'woocommerce_shop_loop_item_title', 'wc_shop_product_sub_category' );

Actuellement, cette solution ne place pas le Designer Name entre les Title et Price, en cherchant comment remédier à cela.

1
Erik

Selon votre description, je suppose que chaque produit a une catégorie nommée Designers et designer-name sous-catégorie dans la catégorie Designers.

Dans ce cas, le code suivant fonctionnera pour vous (lisez les commentaires dans le code pour une explication):

function wc_shop_product_sub_category() {
    // get the category object for "designers" category
    $designers_category = get_term_by( 'name', 'designers', 'product_cat' );
    if( $designers_category ) {
        // this CODE returns all the sub-categories of "designers" category
        // that belongs to the current product in the loop
        $designers = wp_get_post_terms( get_the_ID(), 'product_cat', array( 'child_of' => $designers_category->term_id ) );
        if ( $designers && ! is_wp_error( $designers ) ) {
            // remember, there may be multiple sub categories for "Designers" category. So if you want to
            // show multiple designers, you'll need to loop through the $designers array to show them all
            $designer = array_shift( $designers );
            ?>
            <h2 itemprop="name" class="product_category_title"><span><?php echo $designer->name; ?></span></h2>
            <?php
        }
    }
}
// this will place the designers between title & price
add_action( 'woocommerce_shop_loop_item_title', 'wc_shop_product_sub_category' );
// this will place the designers after price
// add_action( 'woocommerce_after_shop_loop_item_title', 'wc_shop_product_sub_category' );
0
Fayaz