Add checkbox input to product
-
Hello and thanks for the plugin
Our project is very small and we have no money to buy the Pro version, so we are trying to get what we need by studying and changing little things. We found this post on how to add custom fields to the product, but it doesn’t work with input checkbox. Our code
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group custom-fields">
<!-- field format: yes/no, checkbox, values=Si|No. name: destacat_a_linici -->
<input type="checkbox" id="pods-form-ui-pods-meta-destacat_a_linici" name="pods-form-ui-pods-meta-destacat_a_linici" class="pods-form-ui-field pods-form-ui-field-type-datetime pods-form-ui-field pods-form-ui-field-type-time pods-form-ui-field-name-pods-meta-destacat pods-validate pods-validate-required" value="">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
//field format: yes/no, checkbox, values=Si|No. name: destacat_a_linici
if ( ! empty( $postdata['pods-form-ui-pods-meta-destacat_a_linici'] ) ) {
update_post_meta( $product_id, 'pods-form-ui-pods-meta-destacat_a_linici', $postdata['pods-form-ui-pods-meta-destacat_a_linici'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
?>
<div class="dokan-form-group custom-fields">
<!-- field format: yes/no, checkbox, values=Si|No. name: destacat_a_linici -->
<?php $new_field = get_post_meta( $post_id, 'pods-form-ui-pods-meta-destacat_a_linici', true ); ?>
<input type="hidden" name="pods-form-ui-pods-meta-destacat_a_linici" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="pods-form-ui-pods-meta-destacat_a_linici" class="form-label">Destacada a l'inici</label>
<?php dokan_post_input_box( $post_id, 'pods-form-ui-pods-meta-destacat_a_linici', array( 'placeholder' => __( 'Text per destacada', 'dokan-lite' ), 'value' => $new_field ) ); ?>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
//field format: yes/no, checkbox, values=Si|No. name: destacat_a_linici
$new_field = get_post_meta( $product->get_id(), 'pods-form-ui-pods-meta-destacat_a_linici', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Destacada a l\'inici:', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}Thanks for your help and have a nice day
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- You must be logged in to reply to this topic.