/** * Shortcode: WooCommerce product table with add-to-cart buttons. * * @see https://wpcodebook.com/snippets/create-woocommerce-product-table-with-add-to-cart-buttons/ * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query */ add_shortcode( 'wpcodebook_product_table', function () { $rows = ''; foreach ( wc_get_products( array( 'limit' => -1 ) ) as $product ) { $rows .= sprintf( '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>', $product->get_image( 'thumbnail' ), $product->get_name(), $product->get_price_html(), sprintf( '<a class="button" href="%s">%s</a>', $product->add_to_cart_url(), $product->add_to_cart_text() ) ); } return '<table>' . $rows . '</table>'; } );
Shortcode usage:
[wpcodebook_product_table]
Result: