I am trying to add my product to cart.
I have created product type bookable_product. So when I executed below code
function add_bookable_product_to_cart()
{
if (! is_admin()) {
$product_id = 38; // Your bookable product ID
$product_cart_id = WC()->cart->generate_cart_id($product_id);
if (! WC()->cart->find_product_in_cart($product_cart_id)) {
// Add the bookable product to the cart
$added = WC()->cart->add_to_cart($product_id);
if (!$added) {
// Handle failure to add, maybe log or display an error
error_log('Could not add product to cart: ' . $product_id);
}
}
}
}
add_action("wp", "add_bookable_product_to_cart", 10);
As per this doc
I have used WooCommerce for only product management purpose all fields I have created my self. So in default product I have not added any data of WC.