将复选框添加到变体产品管理元 woocommerce

Adding checkboxes to variation products admin meta woocommerce

不得不恭敬地再次问这个问题,因为它已被关闭并链接到一个与我的问题无关的问题,更多的是关于字段创建的问题。

如何将自定义字段(多个复选框)添加到产品管理中的 WooCommerce 变量产品作为复选框,仅在选中时添加元数据并仅保存为管理员?

请理解添加字段已完成,但问题涉及我需要复选框仅在选中时添加元数据以及如何隐藏元数据而不是字段的创建部分已完成。

这个显示代码在我的变体中运行良好

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" )
          )
        );
      }

此代码有效,但希望复选框仅在选中时添加元数据,是否可以仅在选中复选框时添加元数据

    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 );
     
}

这并没有隐藏元数据我如何隐藏它除了为了管理

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 ); 
    }
}

更正复选框 这已通过将 if-else 添加到复选框

来解决
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 );
      }
     
}

并且通过向元键添加下划线解决了隐藏元数据的问题

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 ); 
    }
}