• Resolved 2candela2

    (@2candela2)


    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)
  • Plugin Support Yeasin Arafat

    (@yeasinarafathridoy)

    Hi 2candela2,

    Could you please test the code provided in this Gist URL and let me know if it meets your needs?

    Thanks!

    Thread Starter 2candela2

    (@2candela2)

    Thanks a lot Yeasin Arafat ! it works.
    I go to try a select and a repeatable field. I leave this open for not to make so many issues.

    Have a good day!

    Thread Starter 2candela2

    (@2candela2)

    Yeasin Arafat I was able to build a select, but I don’t know how to populate the select with the values from the database. I use it to add the taxes, since we have products with different kinds. I need it for _tax_class.

    add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );

    function new_product_field(){ ?>

    <div class="dokan-form-group custom-fields">


    <!-- group -->
    <div class="dokan-form-group dokan-clearfix custom-tax">
    <span class="woocommerce-help-tip" tabindex="0" aria-label="Definiu si tot el producte està subjecte a impostos"></span>
    <!-- field format: select name: _tax_status -->
    <div class="content-half-part _tax_status">
    <label class="form-label" for="_tax_status">Estat de l'impost</label>
    <div class="dokan-input-group">
    <select style="" id="_tax_status" name="_tax_status" class="select short">
    <option value="taxable" >Subjecte a impostos</option>
    <option value="none" selected="selected">Cap</option>
    </select>
    </div>
    </div>
    <!-- field format: select name: _tax_class -->
    <div class="content-half-part _tax_clas">
    <label class="form-label" for="_tax_class">Classe d'impost</label>
    <div class="dokan-input-group">
    <select style="" id="_tax_class" name="_tax_class" class="select short">
    <option value="" selected="selected">Estàndard</option>
    <option value="" >Bàsic</option>
    <option value="" >Directa</option>
    </select>
    </div>
    </div>
    </div>

    </div>

    <?php
    }

    Thanks a lot and have a good day ??

    Thread Starter 2candela2

    (@2candela2)

    Also I need it for 3 taxonomies. Thanks

    Thread Starter 2candela2

    (@2candela2)

    I got it.
    Thanks for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.