简体   繁体   中英

Woocommerce Product/Category loop Problem

I have the latest Woocommerce installed with the latest Wordpress. I have installed Grid/List Toggle. my problem starts here. as it changes all the categories and products into listviews

I wanted Categories in Grid View

and products in listView.

So I Search and wound out that categories and products use the same start and end loop on template/loop/loop-start.php ">

can change it there but then it changes everything to that class.

now my solution was the following add conditional tags to this page

but this is a bit problematic. see code

<?php
/**
 * Product Loop Start
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/loop-start.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce/Templates
 * @version     3.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<?php 
if ( is_product_category() ) {

  if ( is_shop() ) {
    echo '<ul class="product-category1">';
  } elseif ( is_product_category( array( 'cd', 'album' ) )) {
    echo '<ul class="product-category2">';
  } else {
    echo '<ul class="products columns-' .esc_attr( wc_get_loop_prop( 'columns' ) );.' ">';
  }

} ?>

<!--<ul class="products columns-<?php //echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>" id="HELP">-->

now the above code works sort of the if_shop statement does not work, but the is_product_category works like a charm and the else statement also do not work

can someone point me in the correct direction for this, have been looking for an answer for weeks now and nothing comes close or is too old to use as Woocommerce changed a lot of their code.

ok after a lot of days and now one helped, I got it to work. Please remember to copy the woocommerce/template/loop/loop-start.php and loop-end.php to your theme/woocommerce/loop/loop-start.php and loop-end.php

loop/loop-start.php

/*This is the main cat on homepage or shop page*/
if (is_shop()|| is_front_page()) {
/*this can be any thing you want it to be <ul><div>*/
echo '<div class="yourclass">';
} 
/*this should be your sub categories 1 level down*/
else if ( is_product_category(array('cat1', 'cat2','cat3', 'cat4', 'cat5'))){
/*this can be any thing you want it to be <ul><div>*/
echo '<div class="yourclass">';
}
/*this should be your sub categories 2 level down*/
 else if ( is_product_category(array('subcat1','subcat2'))){
echo '<ul class="products" id="NORMAL">';
}

then in the loop-end.php

/*This is the main cat on homepage or shop page*/
    if (is_shop()|| is_front_page()) {
    /*this can be any thing you want it to be <ul><div>*/
    echo '</div>';
    } 
    /*this should be your sub categories 1 level down*/
    else if ( is_product_category(array('cat1', 'cat2','cat3', 'cat4', 'cat5'))){
    /*this can be any thing you want it to be <ul><div>*/
    echo '</div>';
    }
    /*this should be your sub categories 2 level down*/
     else if ( is_product_category(array('subcat1','subcat2'))){
    echo '</ul>';
    }

There should be a better way to do this but for me this all I could do. the hooks that I could find on the web are old once and do not work on Woocommerce any more.

hope this helps someone!

PS: ALWAYS MAKE BACKUPS OF YOUR FILES YOU EDIT, SO IF YOU MAKE MISTAKES YOU CAN JUIST UPLOAD THE OLD ONE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM