skip to Main Content

I’m creating a travel agency website and the client needs two different prices for each product. One for children and other for adults.

For example, the product price for adults is $100 and for children it’s $90. So in the single product page I want two input fields: “Quantity of adults” and “Quantity of children”, and I want it to calculate the price based on the quantity the user inputs.

So for example if the user types “2 adults” and “1 child”, the cart total should be $290

Can anyone give me a hand on how to do it?

I know how to create the custom field for the children price in the product admin and the input fields in the single product page, but I don’t know how to make the calculation and show it in the cart.

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    I thought about using product variations but I want the user to be able to add both quantities into cart at once, without having to add first adults and after children.

    I'm quite new to programming so I definitely don't know the best practices to achieve what I want, but here's what I tried so far was:

    Created an additional field to receive the children discount in the product admin page. So the price for adults will be for example $100, and children get $10 discount.

    /* create field in product advanced */
    function create_child_discount_field() {
        $args = array(
        'id' => 'child_discount',
        'label' => __( 'Discount for children', 'cfwc' ),
        'class' => 'custom-field',
        );
        woocommerce_wp_text_input( $args );
    }
    add_action( 'woocommerce_product_options_advanced', 'create_child_discount_field' );
    
    /* save field */
    function save_child_discount_field( $post_id ) {
        $product = wc_get_product( $post_id );
        $title = isset( $_POST['child_discount'] ) ? $_POST['child_discount'] : '';
        $product->update_meta_data( 'child_discount', sanitize_text_field( $title ) );
        $product->save();
    }
    add_action( 'woocommerce_process_product_meta', 'save_child_discount_field' );
    

    Created input fields in the product page like so:

    add_action( 'woocommerce_before_add_to_cart_button', 'custom_qty_fields', 5 );
    
    function custom_qty_fields(){
            echo '<div class="custom-field">
                    <p>Quantity of adults:</p>
                    <input type="text" name="qty_adults" value="" title="Qty Adults">
                 </div>
                 <div class="custom-field">
                    <p>Quantity of children:</p>
                    <input type="text" name="qty_children" value="" title="Qty Children">
                 </div>';
    }
    

    Now as I don't know how to send these quantity fields and calculate the value, here's what I did:

    I have hidden the original Woocommerce quantity field using css (opacity: 0) and used jquery to calculate the qty of children + qty of adults, and then set the result in the hidden woocommerce quantity field. So let's say for example the user inputs 5 adults and 2 children, jquery will calculate 5 + 2 and set 7 to the woocommerce standard quantity field, so the cart will have a total quantity of 7.

    Then what I want to do is, calculate the quantity of children times the children discount field set in the product, and set the value as a discount using the cart calculate fees action:

    add_action('woocommerce_cart_calculate_fees' , 'add_discount_for_children');
    
    function add_discount_for_children( WC_Cart $cart ){
    
        $discount = ??? //no clue on how to do that :(
    
        $cart->add_fee( 'Discount for children', -$discount);
    }
    

    I really don't know the best practices to achieve this and I'm probably doing it in a very bad way, but I hope you guys can understand better what I want and point me in the right way.

    Thanks again!


  2. I think you can use product variations for the same result.
    You can define 2 variations, Adult and Child, each one with his specific prices.
    Look, this was made in test site.

    https://i.stack.imgur.com/446c5.png Define variations.

    https://i.stack.imgur.com/6Se8h.png Child

    https://i.stack.imgur.com/hr2QE.png Adult

    It would display as such.

    enter image description here

    If this doesn’t work for you, please provide more info about your problem and Wc Setup. Such as code and errors you found. That will make easier for us to help you.

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