skip to Main Content

I need to adding to cart woocommerce booking product, if i writing this code, i have false
Or where normal documentation for this plugin?

global $woocommerce;
$added = $woocommerce->cart->add_to_cart($product_id);

2

Answers


  1. As, per my understanding i think bookable products meant to be book only.
    For that what you need to do is use this piece of code to do this pragmatically.

    create_wc_booking( $product_id, $new_booking_data = array(), $status = 'confirmed', $exact = false )
    

    1 Product ID: The id of the bookable product which you are creating a new booking for.
    2.New Booking data: Array of booking data.
    3.Status: Status of the new booking. Valid statuses include: ‘unpaid’, ‘pending’, ‘confirmed’, ‘cancelled’, ‘complete’
    4.Exact: true or false – If false, the function will look for the next available slot after your start date, if the date you tried to book is unavailable.

    Login or Signup to reply.
  2. // add the below code in a in functions.php of your theme.
    add_filter( 'woocommerce_add_cart_item_data', array( $this, 'wdm_add_cities_to_cart' ), 11, 2 );
    
    // the function accepts two parameters, the cart data and the product id
    public function wdm_add_cities_to_cart( $cart_item_meta, $product_id ) {
        // let's consider that the user is logged in
        $user_id = get_current_user_id();
        if( 0 != $user_id)
        {
            // set the values as bookings meta
            $cart_item_meta['booking']['From'] = get_user_meta( $user_id, 'FROM', true );
            $cart_item_meta['booking']['To'] = get_user_meta( $user_id, 'TO', true );  
        }
        return $cart_item_meta;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search