Ad

Adding Checkboxes To Variation Products Admin Meta Woocommerce

Respectfully had to ask this question again due to it being closed and linked to a question that had little reference to my problem and was more about field creation.

How do I add custom fields (multiple checkboxes) to a WooCommerce variable product in product admin as checkboxes that adds meta only if checked and saves as admin only?

Please understand adding the fields is complete but the question relates to me needing the checkbox to only add meta when checked and how to hide the meta not the creation of the fields that part is done.

This display code functions well inside my variation

add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
    
    function variation_settings_fields( $loop, $variation_data, $variation ) {
        woocommerce_wp_text_input(
          array(
              'id'            => "bin_location{$loop}",
              'name'          => "bin_location[{$loop}]",
              'value'         => get_post_meta( $variation->ID, 'bin_location', true ),
              'label'         => __( 'Bin Location', 'woocommerce' ),
              'desc_tip'      => true,
              'description'   => __( 'Some description.', 'woocommerce' ),
              'wrapper_class' => 'form-row form-row-full',
          )
        );
        woocommerce_wp_checkbox(
          array(
            'id'            => "licence_mcps{$loop}",
            'name'          => "licence_mcps[{$loop}]",
            'value'         => get_post_meta( $variation->ID, 'licence_mcps', true ),
            'label'         => __( 'MCPS', 'woocommerce' ),
            'desc_tip'    => true,
            'description' => __( "MCPS Product", "woocommerce" )
          )
        );
        woocommerce_wp_checkbox(
          array(
            'id'            => "licence_ppl{$loop}",
            'name'          => "licence_ppl[{$loop}]",
            'value'         => get_post_meta( $variation->ID, 'licence_ppl', true ),
            'label'         => __( 'PPL', 'woocommerce' ),
            'desc_tip'    => true,
            'description' => __( "PPL Product", "woocommerce" )
          )
        );
      }

This code functions but would prefer the checkbox only to add meta if checked on yes, is it possible to only add meta if the checkbox is checked

    add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
    function save_variation_settings_fields( $variation_id, $loop ) {
    $bin_loc = $_POST['bin_location'][ $loop ];
    $mcps_prod = $_POST['licence_mcps'][ $loop ];
    $ppl_prod = $_POST['licence_ppl'][ $loop ];
   
    if( isset( $bin_loc ) ) 
        update_post_meta( $variation_id, 'bin_location', esc_attr( $bin_loc )); 
  
    $mcps_checkbox = isset( $mcps_prod ) ? 'yes' : ' ';
     update_post_meta( $variation_id, 'licence_mcps', $mcps_checkbox );

    $ppl_checkbox  = isset( $ppl_prod ) ? 'yes' : ' ';
     update_post_meta( $variation_id, 'licence_ppl', $ppl_checkbox );
     
}

And this does not hide the meta how do i hide this apart from in order admin

add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if ( $licence_mcps = $values['data']->get_meta('licence_mcps') ) {
        $item->update_meta_data( 'licence_mcps', $licence_mcps ); 
    }
    if ( $bin_location = $values['data']->get_meta('bin_location') ) {
      $item->update_meta_data( 'bin_location', $bin_location ); 
    }
    if ( $licence_ppl = $values['data']->get_meta('licence_ppl') ) {
      $item->update_meta_data( 'licence_ppl', $licence_ppl ); 
    }
}
Ad

Answer

Correcting the checkboxes This has been resolved by adding if-else to the checkboxes

add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $loop ) {
    $bin_loc = $_POST['bin_location'][ $loop ];
    $mcps_prod = $_POST['licence_mcps'][ $loop ];
    $ppl_prod = $_POST['licence_ppl'][ $loop ];
   
    if( isset( $bin_loc ) ) 
        update_post_meta( $variation_id, 'bin_location', esc_attr( $bin_loc )); 

    If( isset($mcps_prod)){
      update_post_meta( $variation_id, 'licence_mcps', esc_attr( $mcps_prod )); 
      }else{
        update_post_meta( $variation_id, 'licence_mcps', false );
      }
    If( isset($ppl_prod)){
      update_post_meta( $variation_id, 'licence_ppl', esc_attr( $ppl_prod )); 
      }else{
        update_post_meta( $variation_id, 'licence_ppl', false );
      }
     
}

And hiding the metadata has been resolved by adding an underscore to the meta key

add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if ( $licence_mcps = $values['data']->get_meta('licence_mcps') ) {
        $item->update_meta_data( '_licence_mcps', $licence_mcps ); 
    }
    if ( $bin_location = $values['data']->get_meta('bin_location') ) {
      $item->update_meta_data( '_bin_location', $bin_location ); 
    }
    if ( $licence_ppl = $values['data']->get_meta('licence_ppl') ) {
      $item->update_meta_data( '_licence_ppl', $licence_ppl ); 
    }
}
Ad
source: stackoverflow.com
Ad