skip to Main Content

We have woocommerce site that have all virtual and downloadable products. We have multiple tickets linked to each product. We did some research and came to know that woocommerce treating each item in cart as a separate reservation. We want to do single reservation for all items in cart.

For example,
We want to visit a museum on certain date 05/06/2021 and that have Adult, Kids and Senior citizen tickets linked to it. Suppose User have selected 5 Adult, 2 Kids and 1 senior citizen. As per our research we came to know that woocommerce will do 3 reservation

  1. 5 Adult
  2. 2 Kids
  3. 1 Senior citizen.

We want to keep only single reservation for 5 Adult, 2 Kids and 1 Senior Citizen.

Is there any way to do such thing in woocommerce ?

2

Answers


  1. There are probably plugins out there, but the simplest way to do this would to pretty much build your own function to handle this.

    So for example, instead of having the default add to cart button, you could either add your own or write some JavaScript that fires an AJAX request. That request would then add the relevant items to the cart programatically. On completion you could take the user to the cart page. So something like this, note that this is a very rough not tested example, there is a lot of info out there on how to do this

    Fired on add to cart

        addToCart: function(e) {
            e.preventDefault();
            var item_id = $(e.target).data('something');
    
            $.ajax({
                url: ajaxurl,
                type: "POST",
                data: {
                    'action': 'add_to_cart',
                    'item_id': item_id
                },
                success:function(data) {
                    // send to cart
                }
            });
        }
    

    In your functions.php file (or better in a separate functions-ajax.php file)

    <?php
        function customAddToCart(){
            global $woocommerce;
            // you should  do some checking against the values sent over along with a nonce
    
            // then do something with the item id you sent over and add products to the cart as required, so for example:
    
            if($_POST['item_id'] == "98765") {
                $woocommerce->cart->add_to_cart(10,5); // 5 adult tickets 
                $woocommerce->cart->add_to_cart(4,2); // 2 child tickets 
                $woocommerce->cart->add_to_cart(11); // 1 senior tickets 
            }
    
            // obviously needs more logic and that
    
            echo "1";
            die();
        }
        add_action('wp_ajax_add_to_cart', 'customAddToCart');
        add_action('wp_ajax_nopriv_add_to_cart', 'customAddToCart');    
    
    Login or Signup to reply.
  2. Yes, you don’t have to think of adults kids and Senior citezens as individual item but you need to think of it as a meta for order.

    Suppose you have a product A which is a visit to a museum now when someone goes to product page then there should be 3 number field for adults, kids and senior citizens. Whenever one has to book the product he/she has to make sure that he atleast makes sure that there is atleast 1 value for any of the three fields.

    These fields would be responsible for updating price of the product and once the product is added the values of adults, child and senior citizen will be saved inside the order.

    After googling I found this plugin – Advanced Product Fields

    1. 9 different form elements to choose from – [ Use this to create form ]
    2. Dynamic product price: Change the price depending on field value(s)

    I don’t think this would save order meta ( count of adult, child and senior citizen) in order. Use this answer to update meta using code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search