Save custom product fields to cart/checkout/order

I found a solution and I’ll break it down here:

Step 1 – Add custom fields to product

add_action( 'woocommerce_after_variations_table', 'woo_show_some_text', 20 );
if(!function_exists('woo_show_some_text')){
    function woo_show_some_text() {
        echo '
        <div class="customSelection">
        <input type="hidden" name="combination" value="">
        <input type="hidden" name="totalCombination" value="">
        <div class="combination"></div>
        <label><input onkeypress="return onlyNumberKey(event)" value="0" class="numeric combinationFormula" type="number" placeholder="0" min="0" step="1" name="standard"> Standard</label>
        <label><input onkeypress="return onlyNumberKey(event)" value="0" class="numeric combinationFormula" type="number" placeholder="0" min="0" step="1" name="ministandard"> Mini Standard</label>
        <label><input onkeypress="return onlyNumberKey(event)" value="0" class="numeric combinationFormula" type="number" placeholder="0" min="0" step="1" name="offset"> Offset</label>
        <label><input onkeypress="return onlyNumberKey(event)" value="0" class="numeric combinationFormula" type="number" placeholder="0" min="0" step="1" name="wand"> Wand</label>
        </div>
        ';
    }
}

Step 2 – save custom fields to cart

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',10,3);
function wdm_add_item_data($cart_item_data, $product_id, $variation_id){
    if(isset($_REQUEST['standard'])){
        $cart_item_data['standard'] = sanitize_text_field($_REQUEST['standard']);
    }
	if(isset($_REQUEST['ministandard'])){
        $cart_item_data['ministandard'] = sanitize_text_field($_REQUEST['ministandard']);
    }
	if(isset($_REQUEST['offset'])){
        $cart_item_data['offset'] = sanitize_text_field($_REQUEST['offset']);
    }
	if(isset($_REQUEST['wand'])){
        $cart_item_data['wand'] = sanitize_text_field($_REQUEST['wand']);
    }
    return $cart_item_data;
}

Step 3– display custom fields on cart page

add_filter('woocommerce_get_item_data','wdm_add_item_meta',10,2);
function wdm_add_item_meta($item_data, $cart_item){
    if(array_key_exists('standard', $cart_item)){
        $custom_details = $cart_item['standard'];
        $item_data[] = array(
            'key'   => 'Standard',
            'value' => $custom_details
        );
    }
    if(array_key_exists('ministandard', $cart_item)){
        $custom_details = $cart_item['ministandard'];
        $item_data[] = array(
            'key'   => 'Mini Standard',
            'value' => $custom_details
        );
    }
    if(array_key_exists('offset', $cart_item)){
        $custom_details = $cart_item['offset'];
        $item_data[] = array(
            'key'   => 'Offset',
            'value' => $custom_details
        );
    }
    if(array_key_exists('wand', $cart_item)){
        $custom_details = $cart_item['wand'];
        $item_data[] = array(
            'key'   => 'Wand',
            'value' => $custom_details
        );
    }
    return $item_data;
}

Step 4 – display custom fields on order details

add_action( 'woocommerce_checkout_create_order_line_item', 'wdm_add_custom_order_line_item_meta',10,4 );
function wdm_add_custom_order_line_item_meta($item, $cart_item_key, $values, $order){
    if(array_key_exists('standard', $values)){
        $item->add_meta_data('Standard',$values['standard']);
    }
    if(array_key_exists('ministandard', $values)){
        $item->add_meta_data('Mini Standard',$values['ministandard']);
    }
    if(array_key_exists('offset', $values)){
        $item->add_meta_data('Offset',$values['offset']);
    }
    if(array_key_exists('wand', $values)){
        $item->add_meta_data('Wand',$values['wand']);
    }
}

I wish WooCommerce can make this easier 🙂 Solution was found on this website

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer