title: Add custom fields as cart item meta and order item meta in WooCommerce
body: <p>This is a plugin on how to add add cart item meta &amp; order item meta for my WooCommerce order. Initially my code below worked well for input type=text. It returns the label for value and the inputed value. </p>
  
  <p>On conversion to <code>type=checkbox</code> the code returns <code>label</code> and <code>value="on"</code> for those that are checked. </p>
  
  <p>I would like to return the only <strong>value names of checked values</strong> (ignore the values unchecked). </p>
  
  <p>A refactor to help include more checkboxes options would be helpful to reduce written code.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php
      global $woocommerce, $product, $post;
  
          add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
  
          function add_fields_before_add_to_cart( ) {
              ?&gt;
  
              &lt;div class="simple-selects"&gt;
                  &lt;div class="col-md-6"&gt;
                      &lt;h3&gt;Main meals&lt;/h3&gt;
                      &lt;p&gt;&lt;input type="checkbox" name="mm_chicken_cutlet_bento" id="mm_chicken_cutlet_bento"&gt;&lt;?php _e( "Chicken Cutlet Bento", "aoim"); ?&gt;&lt;/p&gt;
                      &lt;p&gt;&lt;input type="checkbox" name="mm_roasted_pork_rib_bento" id="mm_roasted_pork_rib_bento"&gt;&lt;?php _e( "Roasted Pork Rib Bento", "aoim"); ?&gt;&lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;        
  
              &lt;?php
          }
  
          /**
           * Add data to cart item
           */
          add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
          function add_cart_item_data( $cart_item_meta, $product_id ) {
  
              if ( isset( $_POST ['mm_chicken_cutlet_bento'] ) &amp;&amp; isset( $_POST ['mm_roasted_pork_rib_bento'] ) ) {
                  $custom_data  = array() ;
                  $custom_data [ 'mm_chicken_cutlet_bento' ]    = isset( $_POST ['mm_chicken_cutlet_bento'] ) ?  sanitize_text_field ( $_POST ['mm_chicken_cutlet_bento'] ) : "" ;
                  $custom_data [ 'mm_roasted_pork_rib_bento' ] = isset( $_POST ['mm_roasted_pork_rib_bento'] ) ? sanitize_text_field ( $_POST ['mm_roasted_pork_rib_bento'] ): "" ;
                  $cart_item_meta ['custom_data']     = $custom_data ;
              }
  
              return $cart_item_meta;
          }
  
          /**
           * Display custom data on cart and checkout page.
           */
          add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
          function get_item_data ( $other_data, $cart_item ) {
              if ( isset( $cart_item [ 'custom_data' ] ) ) {
                  $custom_data  = $cart_item [ 'custom_data' ];
  
                  $other_data[] = array( 'name' =&gt; 'Chicken Cutlet Bento', 'display'  =&gt; $custom_data['mm_chicken_cutlet_bento'] );
                  $other_data[] = array( 'name' =&gt; 'Roasted Pork Rib Bento', 'display'  =&gt; $custom_data['mm_roasted_pork_rib_bento'] );
              }
  
              return $other_data;
          }
  
          /**
           * Add order item meta.
           */
          add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
          function add_order_item_meta ( $item_id, $values ) {
              if ( isset( $values [ 'custom_data' ] ) ) {
                  $custom_data  = $values [ 'custom_data' ];
                  wc_add_order_item_meta( $item_id, 'Chicken Cutlet Bento', $custom_data['mm_chicken_cutlet_bento'] );
                  wc_add_order_item_meta( $item_id, 'Roasted Pork Rib Bento', $custom_data['mm_roasted_pork_rib_bento'] );
              }
          }
  
  ?&gt;
  </code></pre>