Created
November 27, 2015 12:36
-
-
Save kharissulistiyo/2ea80f8d4491d4c8fc41 to your computer and use it in GitHub Desktop.
WooCommerce: Display all products in one page in a simple way
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 | |
/** | |
* All products in one page | |
* | |
* Display all products in a table with their add to cart button. | |
* Usage : [all_products] to display all products. | |
* Usage : [all_products category="singles"] to display products under "singles" category (slug). | |
*/ | |
add_shortcode('all_products', 'prefix_show_all_products'); | |
if ( ! function_exists( 'prefix_show_all_products' ) ) : | |
function prefix_show_all_products($atts){ | |
$atts = shortcode_atts( array( | |
'category' => '' // Product category slug | |
), $atts ); | |
$args = array(); | |
$args['post_type'] = 'product'; | |
$args['post_status'] = 'publish'; | |
$args['posts_per_page'] = -1; | |
if( $atts['category'] ){ | |
$args['tax_query'] = array( | |
array( | |
'taxonomy' => 'product_cat', | |
'terms' => array_map( 'sanitize_title', explode( ',', $atts['category'] ) ), | |
'field' => 'slug', | |
'operator' => 'IN' | |
) | |
); | |
} | |
$products = new WP_Query( $args ); | |
ob_start(); | |
?> | |
<table> | |
<thead> | |
<tr> | |
<td><?php echo __('Product Name', 'textdomain'); ?></td> | |
<td><?php echo __('Action', 'textdomain'); ?></td> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
while ( $products->have_posts() ) : $products->the_post(); | |
?> | |
<tr> | |
<td> | |
<?php the_title(); ?> | |
</td> | |
<td> | |
<?php echo do_shortcode('[add_to_cart id="'.get_the_ID().'" show_price="false"]'); ?> | |
</td> | |
</tr> | |
<?php | |
endwhile; | |
?> | |
</tbody> | |
</table> | |
<?php | |
return ob_get_clean(); | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, how can I know if this code is still working ? Thank you.