Home > Back-end >  custom order meta with ajax
custom order meta with ajax

Time:08-19

I'm trying to add value to the order meta when the user clicks on the button on the thankyou page.

First I create a custom meta on order and then I want to fill this meta with "status", but don't know why meta field don't get value with click. Console have no errors.

 <?php

    add_action('woocommerce_thankyou', 'add_thank_you_meta');
    function add_thank_you_meta($order_id)
    {
    ?>
        <script type="text/javascript">
            jQuery(function($) {

                $('#addmeta').on('click', function() {

                    var ajax_url = '<?php echo admin_url("admin-ajax.php"); ?>';
                    var ajax_nonce = '<?php echo wp_create_nonce("custom_nonce"); ?>';
                    var form_data = [];

                    form_data.push({
                        "name": "nonce",
                        "value": ajax_nonce
                    }, {
                        "name": "action",
                        "value": "order_meta"
                    }, {
                        "name": "order_id",
                        "value": '<?php echo $order_id; ?>',
                    });

                    $.ajax({
                        type: 'POST',
                        url: ajax_url,   
                        data: form_data,
                        success: function(textStatus) {
                            console.log('uspesno');
                        },
                        error: function(MLHttpRequest, textStatus, errorThrown) {
                            alert('neuspesno');
                        }
                    });
                });


            });
        </script>
    <?php
    }



    add_action('woocommerce_checkout_create_order', 'add_custom_share_meta', 20, 1);
    function add_custom_share_meta($order)
    {
        $order->update_meta_data('delil_povezavo', '');
    }


    add_action('wp_ajax_order_meta', 'add_order_meta');
    add_action('wp_ajax_nopriv_order_meta', 'add_order_meta');


    function add_order_meta()
    {
        if (isset($_POST['order_id']) && $_POST['order_id'] > 0) {
            $order = wc_get_order($_POST['order_id']);
            $order->update_meta_data('delil_povezavo', 'yes');
            die();
        }
    }

Thanks for help

CodePudding user response:

fix and correct code is:

<?php

add_action('woocommerce_thankyou', 'add_thank_you_meta');
function add_thank_you_meta($order_id)
{
?>
    <script type="text/javascript">
        jQuery(function($) {

            $('#addmeta').on('click', function() {

                var ajax_url = '<?php echo admin_url("admin-ajax.php"); ?>';
                var ajax_nonce = '<?php echo wp_create_nonce("custom_nonce"); ?>';
                var form_data = [];

                form_data.push({
                    "name": "nonce",
                    "value": ajax_nonce
                }, {
                    "name": "action",
                    "value": "order_meta"
                }, {
                    "name": "order_id",
                    "value": '<?php echo $order_id; ?>',
                });

                $.ajax({
                    type: 'POST',
                    url: ajax_url,
                    data: form_data,
                    success: function(textStatus) {
                        console.log('uspesno');
                    },
                    error: function(MLHttpRequest, textStatus, errorThrown) {
                        alert('neuspesno');
                    }
                });
            });


        });
    </script>
<?php
}


add_action('wp_ajax_order_meta', 'add_order_meta');
add_action('wp_ajax_nopriv_order_meta', 'add_order_meta');


function add_order_meta()
   {
    if (isset($_POST['order_id']) && $_POST['order_id'] > 0) {
     update_post_meta($_POST['order_id'], '_vivo_delil_povezavo', 'yes');
     die();
   }
 }

We must update post meta not meta data.

  • Related