i'm developing a Woocommerce Booking site for an Hotel, i'm stuck to find the way to have the url 'add to cart" for Woocommerce Booking in a custom loop. could someone please give me a little help whether is with WP_Query
or wc_get_products
. Any help would be appreciate. thx
Asked
Active
Viewed 1,484 times
1

LoicTheAztec
- 229,944
- 23
- 356
- 399

Emmann
- 63
- 9
1 Answers
1
To get bookable products from WooCommerce Bookings plugin:
1). Using a WC_Product_Query
(see documentation here):
$products = wc_get_products( array(
'status' => 'publish',
'type' => 'booking',
'limit' => -1,
) );
// Loop through an array of the WC_Product objects
foreach ( $products as $product ) {
// Output linked product name (with add to cart url)
echo '<p><a href="' . $product->add_to_cart_url() . '">' . $product->get_name() . '</a></p>'; // The product name
}
2). Using a WP_Query (see documentation here):
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => array( 'product' ),
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_type',
'terms' => array( 'booking' ),
'field' => 'slug',
)),
) );
if ( $query->have_posts() ) :
// Loop through an array of WP_Post objects
while ( $query->have_posts() ) : $query->the_post();
// Get the WC_Product Object (optional)
$product = wc_get_product();
// Output linked product name (with add to cart URL)
echo '<p><a href="' . $product->add_to_cart_url() . '">' . get_the_title() . '</a></p>'; // The product name
endwhile;
wp_reset_postdata();
else :
// No post found
echo '<p>' . __("No products found", "woocommerce") . '</p>';
endif;
Both ways works…
Note for add to cart Url on bookable products:
On Bookable products, you mostly can't get the add to cart URL as it involves some choices only possible in single product pages… So when usingWC_Product
add_to_cart_url()
method your get the link to the single product page.

LoicTheAztec
- 229,944
- 23
- 356
- 399
-
Thx for answering my question @LoicTheAztec, your answers are interesting but i need the "add to cart url button" like so: ` 'publish', 'type' => 'booking', 'limit' => -1, 'category' => 'el-dorado-royale' )); foreach ( $products as $product ) { ?> add_to_cart_text(); ?> ` – Emmann Jul 08 '20 at 18:20
-
@Emmann You can't have an add to cart URL on most of bookable products (as they involves some choices only possible in single product pages)… – LoicTheAztec Jul 08 '20 at 18:23
-
copy that @LoicTheAztec – Emmann Jul 08 '20 at 18:27