• Resolved asaad202

    (@asaad202)


    Dears,

    We create a custom check box at checkout level so we create a new snippet as per the below:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields ) {
    	    $fields['billing']['billing_checkbox'] = array(
            'type'      => 'checkbox',
    		'required'=>false,
            'label'     => __('Please Click here if you have a child under the age of 3 and would like to receive a sampling box', 'woocommerce'),
            'class'     => array('form-row-wide'),
            'clear'     => true
    		
    			
        );
    
        return $fields;
    }
    add_action('woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta');
    function my_custom_checkout_field_update_order_meta($order_id) {
      	if ( ! empty( $_POST['billing_checkbox'] ) )
           update_post_meta( $order_id, 'billing_checkbox',  $_POST['billing_checkbox']  );
    	
    	
    }
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
    	
     $billing_checkbox = get_post_meta( $order->get_id(), 'billing_checkbox', true );
        if( $billing_checkbox == 1 )
           echo '<p><strong>Child: </strong> <span>you have a child under the age of 3 and would like to receive a sampling box</span></p>';
    	
    
    }

    We are trying to add a free product once the above checkbox is clicked, so I added the below snippet with the free product id but does not work:

    add_action('woocommerce_after_checkout_validation','place_test_request11');
    function place_test_request11() {
    
    	 $billing_checkbox = 1;
    $free_product_id = 121674 ;
    	global $woocommerce;
    	if (  WC()->cart->billing_checkbox==$billing_checkbox) {
    $product_cart_id = WC()->cart->generate_cart_id( 121674 );
    if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){
    	$woocommerce->cart->add_to_cart( 121674 ); 
    }
          
    }
    }

    Please can you help us in order to call the custom field and create the free product automatically after checked the checkbox?

    Best Regards,

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @asaad202,

    You did everything right just failed in one part, you have to check if the check is checked then through ajax you have to add the product then update the cart.

    Currently, you just add the product nothing else and call php function but would create any effect.

    Thank You

    Thread Starter asaad202

    (@asaad202)

    Hello @zworthkey12vaibhav,

    I added $billing_checkbox = 1; isn’t enough to check if the check is checked?

    Please can you clarify more concerning “then through ajax you have to add the product then update the cart.”

    Your support is appreciated.

    Thank you

    Thread Starter asaad202

    (@asaad202)

    @zworthkey12vaibhav

    even we tried the below, but seems that the checkbox always appeared as checked:

    add_action('woocommerce_after_checkout_validation','place_test_request2');
    function place_test_request2($fields) {
            $billing_checkbox2 =$fields['billing']['billing_checkbox']['checked'] = true;
    
            if($billing_checkbox2){
    		$free_product_id = 121674 ;
    		global $woocommerce;
    
    			$product_cart_id = WC()->cart->generate_cart_id( 121674 );
                   if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){
    	           $woocommerce->cart->add_to_cart( 121674 ); 
    
    	}
    
    }
    }

    Regards,

    Hi @asaad202,

    You getting it all wrong. When a page is loaded it becomes static. Some of the hooks check the input and perform ajax accordingly. But the hooks you used will not.

    Concerning AJAX, it is a jQuery function that helps you to listen to the input or anything perform some action Or run a PHP function.

    What I meant in my previous reply was, If you want to add a free product in the cart when a checkbox is checked then you have to add jQuery Script to that particular id of Checkbox and perform the function through ajax. It will work without refreshing the page.

    If you are not familiar with ajax then you can use this link
    https://www.sitepoint.com/how-to-use-ajax-in-wordpress-a-real-world-example/

    Thank You

    Thread Starter asaad202

    (@asaad202)

    Hello @zworthkey12vaibhav,

    I tried the below and working fine:

    add_action('woocommerce_after_checkout_validation','place_test_request2');
    function place_test_request2($fields) {
    		$free_product_id = 121674 ;
    		global $woocommerce;
     if ($billing_checkbox2=$fields['billing_checkbox'] == 1)  {
    			$product_cart_id = WC()->cart->generate_cart_id( 121674 );
                   if( ! WC()->cart->find_product_in_cart( $product_cart_id ) ){
    	           $woocommerce->cart->add_to_cart( 121674 ); 
    
    	}
    
    }
    }

    Do you recommend to proceed with it or trying Ajax?

    Regards,

    @asaad202 Above function will add the product after checkout. This means the user will not be able to see the product adding to the cart. If you wish that you can proceed with the above function.

    If you want to show adding product to the user you can use ajax.

    Thanks and Regards

    Hi @asaad202,

    It looks like using an extension such as Product Add-Ons or Free Gifts will be an easier option.

    If you want to try it, we have a 30-day refund policy. If the product doesn’t work the way you need or you think another product would work better, we are more than happy to provide a full refund.

    You can read more about our refund policy on our website here: https://woocommerce.com/refund-policy/

    Hi @asaad202

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. If you have further questions, please feel free to open a new topic.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Add a free product after check a checkbox at checkout level’ is closed to new replies.