mercredi 6 octobre 2021

Wordpress | Woocommerce custom checkbox - Change the checked checkbox's default value from "yes" to "CHECKED"

I have a custom checkbox in my woocommerce products. When this checkbox is checked it adds the 'yes' value into the custom off_plan field. When its unchecked it works good and updates the value of my custom field to 'off'. My question is how may I also add the custom value for checked checkbox (for example 'checked').

Now it works like - checked = value 'yes', unchecked = value 'off'
It has to work like this - checked value 'Checked', unchecked = value 'off'

Code

add_action( 'save_post_product', 'bbloomer_save_related_checkbox_products' );
  
function bbloomer_save_related_checkbox_products( $product_id ) {
   global $pagenow, $typenow;
   if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset( $_POST['off_plan'] ) ) {
      update_post_meta( $product_id, 'off_plan', $_POST['off_plan'] ); //here is the problem - I need to return a custom value
    } else update_post_meta( $product_id, 'off_plan', 'off' );
}

I have tried this code bellow, but if I use this solution, it does not remember that checkbox was checked.

update_post_meta( $product_id, 'off_plan', 'Checked' )

Do you have any idea how may I have the custom value for this? Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire