Utilizzare correttamente is_product_category()

20 giu 2018, 14:16:47
Visualizzazioni: 19.8K
Voti: 1

Ho due categorie di prodotti:

  1. MARKETING DIGITALE MULTILINGUE (ID 75)
  2. CANALI DI VENDITA INTERNAZIONALI (ID 107)

Voglio eseguire del codice tramite una condizione if solo per queste due categorie.

Ho provato a utilizzare questo codice ma non ha funzionato

if( is_product_category(107 || 75) ) { 

$term = get_queried_object();

$parent = $term->parent;

if (!empty($parent)) {
    $child_of = $parent;
} else {
    $child_of = $term->term_id;
}

$terms = get_terms( array(
    'taxonomy'      => 'product_cat',
    'child_of'      => $child_of,
) );

if ($terms) {
    foreach ( $terms as $category ) {

        $category_id = $category->term_id;
        $category_slug = $category->slug;
        $category_name = $category->name;
        $category_desc = $category->description;

        echo '<div class="'.$category_slug.'">';

        echo '<h2>'.$category_name.'</h2>';

        if ($category_desc) {
            echo '<p>'.$category_desc.'</p>';
        }

        $products_args = array(
            'post_type'     => 'product', // tipo di post prodotto
            'tax_query'     => array( 
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $category_id, // ID della categoria
                ),
            ),
        );

        $products = new WP_Query( $products_args );

        if ( $products->have_posts() ) { // inizia solo se abbiamo dei prodotti

            // INIZIA il loop normale di woocommerce

            woocommerce_product_loop_start();

            if ( wc_get_loop_prop( 'total' ) ) {

                while ( $products->have_posts() ) : $products->the_post();

                    /**
                     * Hook: woocommerce_shop_loop.
                     *
                     * @hooked WC_Structured_Data::generate_product_data() - 10
                     */
                    do_action( 'woocommerce_shop_loop' );

                    wc_get_template_part( 'content', 'product' );

                endwhile; // fine del loop

            }

            woocommerce_product_loop_end();

            // FINE del loop normale di woocommerce

            // Ripristina i dati originali del post, potrebbe non essere necessario qui (in un plugin potrebbe essere necessario)
            wp_reset_postdata();

        }
0
Tutte le risposte alla domanda 3
0

is_product_category() deve essere utilizzato solo nelle pagine archivio delle categorie di WooCommerce, quindi assicurati prima di essere su un archivio di categoria.

invece del numero della categoria usa il nome dello slug della categoria is_product_category('nome-slug-categoria')

non è necessario eseguire una condizione OR(||), usa semplicemente is_product_category('slug-categoria1','slug-categoria2') per ottenere lo stesso risultato

20 giu 2018 14:46:31
0

Prova:

if( is_product_category( 'category1-slug' ) || is_product_category( 'category2-slug' ) ) {
   //...
}
20 giu 2018 14:42:31
0

Per alcune categorie, dovresti utilizzare gli slug in un array quando usi la funzione is_product_category(). Di seguito un esempio. Testato e funziona.

if( is_product_category( array('category1-slug', 'category2-slug' )) )
10 mar 2020 17:10:14