Skip to content

Instantly share code, notes, and snippets.

@Lenny4
Created November 14, 2024 15:29
Show Gist options
  • Save Lenny4/448cc3894b940d3877f02754a9893cdf to your computer and use it in GitHub Desktop.
Save Lenny4/448cc3894b940d3877f02754a9893cdf to your computer and use it in GitHub Desktop.
Change event to document.body for dynamically added elements
<?php
/**
* Plugin Name: ExamplePlugin
* Version: 1.0.0
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
add_action(
'admin_print_footer_scripts-woocommerce_page_wc-orders',
function () {
?>
<script>
jQuery(function ($) {
$(document).ready(function () {
var $title = $('h1.wp-heading-inline');
var $orderData = $('#woocommerce-order-data').find('.inside');
var $buttonNoModif = $('<button class="page-title-action">Run Test before pull request, then try to edit Billing adress</button>')
.on('click', function () {
$orderData.empty();
$orderData.append('<div class="loading-content"><span class="spinner is-active"></span></div>');
$.ajax({
url: woocommerce_admin_meta_boxes.ajax_url,
data: {
order_id: woocommerce_admin_meta_boxes.post_id,
action: 'test_51185',
},
type: 'POST',
success: function (response) {
$orderData.empty();
$orderData.append(response);
}
});
});
var $buttonWithModif = $('<button class="page-title-action">Run Test with pull request, then try to edit Billing adress</button>')
.on('click', function () {
$orderData.empty();
$orderData.append('<div class="loading-content"><span class="spinner is-active"></span></div>');
$.ajax({
url: woocommerce_admin_meta_boxes.ajax_url,
data: {
order_id: woocommerce_admin_meta_boxes.post_id,
action: 'test_51185',
},
type: 'POST',
success: function (response) {
$orderData.empty();
$orderData.append(response);
if ('undefined' !== typeof window.wcOrderMetaBoxes) {
window.wcOrderMetaBoxes.wc_meta_boxes_order.init();
}
}
});
});
$title.after($buttonWithModif);
$title.after($buttonNoModif);
});
});
</script>
<?php
},
999
);
add_action(
'wp_ajax_test_51185',
function () {
$order_id = absint($_POST['order_id']);
$order = wc_get_order($order_id);
WC_Meta_Box_Order_Data::output($order);
wp_die();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment