Allow User To Add Custom Product Attribute Value (Dokan Multivendor for WooCommerce)

I use the Dokan Multivendor Plugin (with the auction module) for WooCommerce. I wish to allow the user to add a custom attribute value to an already-defined product attribute. More specifically, I wish them to be able to manually type the reference number and year of the auction product that they are uploading. I have tried to bypass the attribute dropdown menu using a PHP code snippet. I also tried the code below to add a custom field, however, when saving the product the values are not saved to the product attributes. add_action( 'wp_footer', 'custom_reference_number_year_fields', 10 ); function custom_reference_number_year_fields() { if ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' && isset( $_GET['product_id'] ) ) { ?> <script type="text/javascript"> jQuery(document).ready(function($) { // Add custom input fields for Reference Number and Year dynamically function addCustomReferenceYearFields() { // Reference Number field var referenceField = ` <div class="dokan-product-attribute-item dokan-clearfix"> <div class="content-half-part"> <label class="form-label" for="custom_reference_number">Reference Number</label> <input type="text" id="custom_reference_number" name="custom_reference_number" class="dokan-form-control" placeholder="Enter Reference Number"> </div> </div> `; // Year field var yearField = ` <div class="dokan-product-attribute-item dokan-clearfix"> <div class="content-half-part"> <label class="form-label" for="custom_year">Year</label> <input type="text" id="custom_year" name="custom_year" class="dokan-form-control" placeholder="Enter Year"> </div> </div> `; // Add custom fields before the predefined attribute section $('.dokan-attribute-variation-options').prepend(referenceField + yearField); } // Add the custom fields on page load addCustomReferenceYearFields(); // Save the custom Reference Number and Year values when form is submitted $('form#dokan-product-edit-form').on('submit', function(e) { var referenceNumber = $('#custom_reference_number').val(); var year = $('#custom_year').val(); if(referenceNumber) { $('<input>').attr({ type: 'hidden', name: 'custom_reference_number_value', value: referenceNumber }).appendTo('form#dokan-product-edit-form'); } if(year) { $('<input>').attr({ type: 'hidden', name: 'custom_year_value', value: year }).appendTo('form#dokan-product-edit-form'); } }); }); </script> <?php } } // Save the custom fields as product attributes add_action('dokan_process_product_meta', 'save_custom_reference_year_fields', 20, 2); function save_custom_reference_year_fields($product_id, $dokan_product) { if ( isset( $_POST['custom_reference_number_value'] ) ) { // Add the Reference Number as a new attribute term $reference_number = sanitize_text_field($_POST['custom_reference_number_value']); wp_set_object_terms( $product_id, $reference_number, 'pa_reference-number', true ); } if ( isset( $_POST['custom_year_value'] ) ) { // Add the Year as a new attribute term $year = sanitize_text_field($_POST['custom_year_value']); wp_set_object_terms( $product_id, $year, 'pa_year', true ); } } // Ensure custom Reference Number and Year are loaded on product edit add_action( 'dokan_product_edit_after_inventory_section', 'load_custom_reference_year_fields', 10, 2 ); function load_custom_reference_year_fields($post, $post_id) { $reference_number = wp_get_object_terms( $post_id, 'pa_reference-number', array( 'fields' => 'names' ) ); $year = wp_get_object_terms( $post_id, 'pa_year', array( 'fields' => 'names' ) ); if ( ! empty( $reference_number ) ) { ?> <script type="text/javascript"> jQuery(document).ready(function($) { $('#custom_reference_number').val('<?php echo esc_js( $reference_number[0] ); ?>'); }); </script> <?php } if ( ! empty( $year ) ) { ?> <script type="text/javascript"> jQuery(document).ready(function($) { $('#custom_year').val('<?php echo esc_js( $year[0] ); ?>'); }); </script> <?php } } As you can see in the end of the code, I'd also like these values to be visible when editing an existing product. Since they are not saved to the product, this doesn't work either. Thank you for an awesome forum and your help! Kind regards, Martin

Comment (0)

You’ll be in good company