Add a free product after check a checkbox at checkout level
-
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)
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.