Quote from arty on September 6, 2022, 12:50
Hi Mr Pablo,
I understand what you're trying to show. however i didn't make the choise to make it "work" like that, rather it's a woocommerce standard way of working.
The custom woocommerce_wp_checkbox() field is automatically saved in the wp_postsmeta table as "yes" or "no".
I have pasted bellow the whole plugin that is populating these fields and you can clearly see that i do not manipulate them in any way.
In order to make it work with 1 or 0, i have to write a lot of code as firstly i will to manipulate the fields on the save_product_custom_fields function so they will be written as 1 or 0(which is something very easy).
The difficult(not really difficult but it would need a lot of time) part would be to edit the code that these fields change the product behavior which is in 10+ different places and plugins.
Is it possible to make the checkboxes work in the standard woocommerce way in a future update?
if yes, how much time(approximatelly) will be needed?
thank you in advance for your time,
Menelaos
<?php
add_action(
'woocommerce_product_options_general_product_data',
'general_product_custom_fields'
);
// Add a custom field to general product data.
function general_product_custom_fields() {
global $post;
$checkbox_value = get_post_meta( $post->ID, 'xml_exempt', true );
if( empty( $checkbox_value ) ){
$checkbox_value = '';
}
woocommerce_wp_checkbox(
array(
'id' => 'xml_exempt',
'label' => __('Αφαίρεση από τα XML', 'woocommerce' ),
'description' => __( 'Επιλέξτε αν θέλετε να εξαιρέσετε το συγκεκριμένο προϊόν από τα XML', 'woocommerce' ),
'value' => $checkbox_value,
)
);
$mono_paralavi_value = get_post_meta( $post->ID, 'mono_paralavi', true );
if( empty( $mono_paralavi_value ) ){
$mono_paralavi_value = '';
}
woocommerce_wp_checkbox(
array(
'id' => 'mono_paralavi',
'label' => __('Μόνο Παραλαβή', 'woocommerce' ),
'description' => __( 'Επιλέξτε αν θέλετε να ενεργοποιήσετε παραλαβή μόνο από το κατάστημα', 'woocommerce' ),
'value' => $mono_paralavi_value,
)
);
}
add_action(
'woocommerce_process_product_meta',
'save_product_custom_fields'
);
// Save the custom field in the DB.
function save_product_custom_fields($id) {
update_post_meta(
$id,
'mono_paralavi',
$_POST['mono_paralavi']
);
update_post_meta(
$id,
'xml_exempt',
$_POST['xml_exempt']
);
}
?>
Hi Mr Pablo,
I understand what you're trying to show. however i didn't make the choise to make it"work" like that, rather it's a woocommerce standard way of working.
The custom woocommerce_wp_checkbox() field is automatically saved in the wp_postsmeta table as"yes" or"no".
I have pasted bellow the whole plugin that is populating these fields and you can clearly see that i do not manipulate them in any way.
In order to make it work with 1 or 0, i have to write a lot of code as firstly i will to manipulate the fields on the save_product_custom_fields function so they will be written as 1 or 0(which is something very easy).
The difficult(not really difficult but it would need a lot of time) part would be to edit the code that these fields change the product behavior which is in 10+ different places and plugins.
Is it possible to make the checkboxes work in the standard woocommerce way in a future update?
if yes, how much time(approximatelly) will be needed?
thank you in advance for your time,
Menelaos
<?php
add_action(
'woocommerce_product_options_general_product_data',
'general_product_custom_fields'
);
// Add a custom field to general product data.
function general_product_custom_fields() {
global $post;
$checkbox_value = get_post_meta( $post->ID, 'xml_exempt', true );
if( empty( $checkbox_value ) ){
$checkbox_value = '';
}
woocommerce_wp_checkbox(
array(
'id' => 'xml_exempt',
'label' => __('Αφαίρεση από τα XML', 'woocommerce' ),
'description' => __( 'Επιλέξτε αν θέλετε να εξαιρέσετε το συγκεκριμένο προϊόν από τα XML', 'woocommerce' ),
'value' => $checkbox_value,
)
);
$mono_paralavi_value = get_post_meta( $post->ID, 'mono_paralavi', true );
if( empty( $mono_paralavi_value ) ){
$mono_paralavi_value = '';
}
woocommerce_wp_checkbox(
array(
'id' => 'mono_paralavi',
'label' => __('Μόνο Παραλαβή', 'woocommerce' ),
'description' => __( 'Επιλέξτε αν θέλετε να ενεργοποιήσετε παραλαβή μόνο από το κατάστημα', 'woocommerce' ),
'value' => $mono_paralavi_value,
)
);
}
add_action(
'woocommerce_process_product_meta',
'save_product_custom_fields'
);
// Save the custom field in the DB.
function save_product_custom_fields($id) {
update_post_meta(
$id,
'mono_paralavi',
$_POST['mono_paralavi']
);
update_post_meta(
$id,
'xml_exempt',
$_POST['xml_exempt']
);
}
?>