Custom form on checkout ONLY for specific products with variation SKU?
-
Hi everyone,
As some of my products are made-to-measure, I needed to ask customers to enter additional info during checkout if they have such items in their cart.
To be clear, I am selling stuff that are available in regular sizes S,M,L,XL… AND…. made-to-measure. So I needed a custom form to show up during checkout ONLY if these specific products with variation SKU are in cart.
Customizing some codes I found online, I was able to add such form, and I got this extra custom fields to appear in order emails. This part works like a charm.
Problem is : form appears for every order, regardless customer is buying a regular size or made-to-measure.
Here’s what I tried :
/* --------------------------------------------- */ // M2M ORDER FORM /* --------------------------------------------- */ /** * Add the field to the checkout **/ add_action( 'woocommerce_after_order_notes', 'm2m_custom_checkout_field' ); function m2m_custom_checkout_field( $checkout ) { //Check if M2M item in Cart $m2m_in_cart = true; if (strpos($sku, 'M2M') !== false) { echo 'true'; } //M2M item is in cart so show additional fields if ( $m2m_in_cart === true ) { echo '<div id="my_measurements"><h3>' . __( 'Measurements' ) . '</h3><br><br><p style="margin: 0 0 8px;">YOU ARE ABOUT TO PURCHASE MADE TO MEASURE PRODUCTS : PLEASE ENTER YOUR MEASUREMENTS BELOW</p> <p style="margin: 0 0 8px;">Not sure how to do this? <a href="https://www.torturegardenclothing.com/m2m_guidelines.pdf" target="_blank", >Download our made-to-measure measurement guidelines (PDF)</a></p>'; woocommerce_form_field( 'my-gender-select', array( 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => true, 'type' => 'select', 'options' => array( 'male' => __('Male', 'Male' ), 'female' => __('Female', 'Female' ) ), 'class' => array( 'my-gender-select form-row-wide' ), 'label' => __( 'Gender' ), ), $checkout->get_value( 'my-gender-select' ) ); woocommerce_form_field( 'inches-or-cm', array( 'placeholder' => _x('', 'placeholder', 'woocommerce'), 'required' => true, 'type' => 'select', 'options' => array( 'inches' => __('in', 'in' ), 'centimeters' => __('cm', 'cm' ) ), 'class' => array( 'my-measurement-unit-select form-row-wide' ), 'label' => __( 'Measurement Unit' ), ), $checkout->get_value( 'inches-or-cm' ) ); woocommerce_form_field( 'my-height', array( 'type' => 'text', 'class' => array( 'my-height form-row-wide' ), 'label' => __( 'Height' ), ), $checkout->get_value( 'my-height' ) ); woocommerce_form_field( 'my-over-bust-size', array( 'type' => 'text', 'class' => array( 'my-over-bust-size form-row-wide' ), 'label' => __( 'Over Bust' ), ), $checkout->get_value( 'my-over-bust-size' ) ); woocommerce_form_field( 'my-full-bust-size', array( 'type' => 'text', 'class' => array( 'my-full-bust-size form-row-wide' ), 'label' => __( 'Full Bust' ), ), $checkout->get_value( 'my-bust-size' ) ); woocommerce_form_field( 'my-under-bust-size', array( 'type' => 'text', 'class' => array( 'my-under-bust-size form-row-wide' ), 'label' => __( 'Under Bust' ), ), $checkout->get_value( 'my-under-bust-size' ) ); woocommerce_form_field( 'my-natural-waist-size', array( 'type' => 'text', 'class' => array( 'my-natural-waist-size form-row-wide' ), 'label' => __( 'Natural Waist' ), ), $checkout->get_value( 'my-natural-waist-size' ) ); woocommerce_form_field( 'my-upper-hip-size', array( 'type' => 'text', 'class' => array( 'my-upper-hip-size form-row-wide' ), 'label' => __( 'Upper Hip' ), ), $checkout->get_value( 'my-upper-hip-size' ) ); woocommerce_form_field( 'my-fullest-butt-hip-size', array( 'type' => 'text', 'class' => array( 'my-fullest-butt-hip-size form-row-wide' ), 'label' => __( 'Fullest Butt / Hip' ), ), $checkout->get_value( 'my-fullest-butt-hip-size' ) ); woocommerce_form_field( 'my-under—butt-crease', array( 'type' => 'text', 'class' => array( 'my-under—butt-crease form-row-wide' ), 'label' => __( 'Under-Butt Crease' ), ), $checkout->get_value( 'my-under—butt-crease' ) ); woocommerce_form_field( 'my-top—of-thigh-size', array( 'type' => 'text', 'class' => array( 'my-top—of-thigh-size form-row-wide' ), 'label' => __( 'Top of Thigh' ), ), $checkout->get_value( 'my-top—of-thigh-size' ) ); woocommerce_form_field( 'my-knee-size', array( 'type' => 'text', 'class' => array( 'my-knee-size form-row-wide' ), 'label' => __( 'Knee' ), ), $checkout->get_value( 'my-knee-size' ) ); woocommerce_form_field( 'my-calf-size', array( 'type' => 'text', 'class' => array( 'my-calf-size form-row-wide' ), 'label' => __( 'Calf' ), ), $checkout->get_value( 'my-calf-size' ) ); echo '</div>'; } } /** * Check if Conditional Product is In cart * * @param $product_id * * @return bool */ function m2m_in_cart( $product_id ) { //Check to see if user has m2m product in cart global $woocommerce; //flag no M2M product in cart $m2m_in_cart = false; foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if (strpos($sku, 'M2M') !== false) { //M2M item is in cart! $m2m_in_cart = true; } } return $m2m_in_cart; } /** * Update the order meta with field value **/ add_action( 'woocommerce_checkout_update_order_meta', 'm2m_custom_checkout_field_update_order_meta' ); function m2m_custom_checkout_field_update_order_meta( $order_id ) { //check if $_POST has our custom fields if ( $_POST['my-gender-select'] ) { //It does: update post meta for this order update_post_meta( $order_id, 'Gender', esc_attr( $_POST['my-gender-select'] ) ); } if ( $_POST['inches-or-cm'] ) { update_post_meta( $order_id, 'Measurement Unit', esc_attr( $_POST['inches-or-cm'] ) ); } if ( $_POST['my-height'] ) { //It does: update post meta for this order update_post_meta( $order_id, 'Height', esc_attr( $_POST['my-height'] ) ); } if ( $_POST['my-over-bust-size'] ) { update_post_meta( $order_id, 'Over Bust', esc_attr( $_POST['my-over-bust-size'] ) ); } if ( $_POST['my-full—bust-size'] ) { //It does: update post meta for this order update_post_meta( $order_id, 'Full Bust', esc_attr( $_POST['my-full—bust-size'] ) ); } if ( $_POST['my-under—bust-size'] ) { //It does: update post meta for this order update_post_meta( $order_id, 'Under Bust', esc_attr( $_POST['my-under—bust-size'] ) ); } if ( $_POST['my-natural-waist-size'] ) { update_post_meta( $order_id, 'Natural Waist', esc_attr( $_POST['my-natural-waist-size'] ) ); } if ( $_POST['my-upper-hip-size'] ) { //It does: update post meta for this order update_post_meta( $order_id, 'Upper Hip', esc_attr( $_POST['my-upper-hip-size'] ) ); } if ( $_POST['my-fullest-butt—hip-size'] ) { update_post_meta( $order_id, 'Fullest Butt / Hip', esc_attr( $_POST['my-fullest-butt—hip-size'] ) ); } if ( $_POST['my-under—butt-crease'] ) { update_post_meta( $order_id, 'Under-Butt Crease', esc_attr( $_POST['my-under—butt-crease'] ) ); } if ( $_POST['my-top—of-thigh-size'] ) { //It does: update post meta for this order update_post_meta( $order_id, 'Top of Thigh', esc_attr( $_POST['my-top—of-thigh-size'] ) ); } if ( $_POST['my-knee-size'] ) { update_post_meta( $order_id, 'Knee', esc_attr( $_POST['my-knee-size'] ) ); } if ( $_POST['my-calf-size'] ) { update_post_meta( $order_id, 'Calf', esc_attr( $_POST['my-calf-size'] ) ); } } /** * Add the field to order emails **/ add_filter( 'woocommerce_email_order_meta_keys', 'm2m_checkout_field_order_meta_keys' ); function m2m_checkout_field_order_meta_keys( $keys ) { //Check if M2M Items in Cart $m2m_in_cart = true; if (strpos($sku, 'M2M') !== false) { echo 'true'; } //Only if M2M Items in cart if ( $m2m_in_cart === true ) { $keys[] = 'Gender'; $keys[] = 'Measurement Unit'; $keys[] = 'Height'; $keys[] = 'Over Bust'; $keys[] = 'Full Bust'; $keys[] = 'Under Bust'; $keys[] = 'Natural Waist'; $keys[] = 'Upper Hip'; $keys[] = 'Fullest Butt / Hip'; $keys[] = 'Under-Butt Crease'; $keys[] = 'Top of Thigh'; $keys[] = 'Knee'; $keys[] = 'Calf'; } return $keys; } /* --------------------------------------------- */ // END OF M2M ORDER FORM /* --------------------------------------------- */
If one of you guys have any idea what I am doing wrong, that’d really help me ! I’ve been trying to fix for 2 days but can’t seem to find the proper way to make it work….
THANK YOU FOR UR HELP !
A.
- The topic ‘Custom form on checkout ONLY for specific products with variation SKU?’ is closed to new replies.