How to set value/term of a Woocommerce attribute text field in admin panel?

I am assuming the ID of that text filed, is url-field here is the JS code

$("#url-field").keyup(function(event) {
     var url_val=$("#url-field").val();
      $.ajax({
            type:"POST",
            url: ajaxurl,
            data: {
                action:'use_someprefix_update_url',
                search_string:url_val,
                namefiled : namehere,
                postid : postidhereYouCangetItIHope  
            },
            success:function(data){
               alert("Success, you can remove me :-) ");
            }
        });
});

Here comes PHP, put this in functions.php

function use_someprefix_update_url(){
    $defaults = array();
    $defaults[ $_POST['namefiled'] ] = array (
                    'name' => $_POST['namefiled'],
                    'value' => $_POST['search_string'],
                );
     update_post_meta( $_POST['postid'] , '_product_attributes', $defaults );
}

Also this Hook also :

add_action('wp_ajax_use_someprefix_update_url', 'use_someprefix_update_url');

P.S : You have to do validation stuff.

Leave a Comment