G
G
Genri_Rus2019-12-16 19:37:18
WordPress
Genri_Rus, 2019-12-16 19:37:18

How to save 2 checkboxes in admin in woocommerce?

Example

add_action( 'woocommerce_product_data_panels', 'custom_added_tabs_panel' );
function custom_added_tabs_panel() {
  ?>
  <div id="status_panel_product_data" class="panel woocommerce_options_panel">
    <div class="options_group">
      <?php

      woocommerce_wp_checkbox(array(
        'id'            => 'status-one',
        'wrapper_class' => 'show_if_simple',
        'label'         => 'Статус 1'
      ));
      ?>
    </div>
    <div class="options_group">
      <?php

      woocommerce_wp_checkbox(array(
        'id'            => 'status-two',
        'wrapper_class' => 'show_if_simple',
        'label'         => 'Статус 2'
      ));
      ?>
    </div>
  </div>
  <?php
}

I can't figure out how to save these 2 metafields?
Tried like this, but nothing works:
add_action( 'woocommerce_process_product_meta', 'custom_fields_save', 10 );

function custom_fields_save( $post_id ) {
  $product = wc_get_product( $post_id );
  
  
  $checkbox_field = (isset( $_POST['status-one'] ) || isset($_POST['status-two'] )) ? 'yes' : 'no';
  $product->update_meta_data( 'status-one, status-two', $checkbox_field );
  
  $product->save();
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question