Created
November 29, 2018 12:02
-
-
Save ARCHTKT/fa20e7cfb269b0c9a1fdfd4ec4a2e377 to your computer and use it in GitHub Desktop.
Show and separate subcategories and products in woocommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /* No copiar esta linea */ | |
/* | |
* Este snippet te muestra las categorías en la página de tienda | |
* y las subcategorías en las páginas de categorías separadas por títulos en castellano. | |
* | |
* Probado con Woocommerce 3.5.1 | |
* | |
*/ | |
add_filter( 'woocommerce_product_loop_start', 'archtkt_maybe_show_product_subcategories' ); | |
remove_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' ); | |
function archtkt_maybe_show_product_subcategories( $loop_html = '' ) { | |
$parent_id = is_product_category() ? get_queried_object_id() : 0; | |
$product_categories = woocommerce_get_product_subcategories( $parent_id ); | |
if ( $product_categories ) { | |
if ( $parent_id === 0 ){ | |
echo '<h2>Categorías</h2>'; /* Cambiar el nombre para las categorías en pg. de tienda. */ | |
} else { | |
echo '<h2>Subcategorías</h2>'; /* Cambiar el nombre para las subcategorías en pg. de categorías. */ | |
} | |
} | |
ob_start(); | |
foreach ( $product_categories as $category ) { | |
echo '<li class="product">'; | |
do_action( 'woocommerce_before_subcategory', $category ); | |
do_action( 'woocommerce_before_subcategory_title', $category ); | |
do_action( 'woocommerce_shop_loop_subcategory_title', $category ); | |
do_action( 'woocommerce_after_subcategory_title', $category ); | |
do_action( 'woocommerce_after_subcategory', $category ); | |
echo '</li>'; | |
} | |
if ( $product_categories ) { | |
echo '</ul><h2>Productos</h2>'; | |
echo '<ul class="products columns-' . esc_attr( wc_get_loop_prop( 'columns' ) ) . '">'; | |
} | |
$loop_html .= ob_get_clean(); | |
echo $loop_html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment