WooCommerce管理员添加费用AJAX:如何定位新订单请求URL?

时间:2019-12-06 18:47:22

标签: php jquery ajax wordpress woocommerce

在我的网站上,我添加了custom fields and AJAX to add fee to an order in the admin(WooCommerce>订单>编辑)。编辑现有订单时有效,但创建新订单时无效。

这是我的代码,该代码在管理命令页面中添加了一个链接以触发AJAX,该链接是通过具有int的操作woocommerce_admin_order_data_after_billing_address添加的:

$order

用于添加新订单的页面URL类似于$nonce = wp_create_nonce('add_custom_fee_nonce'); $link = admin_url('admin_ajax.php?action=add_custom_fee&post_id=' . $order->id . '&nonce=' . $nonce ); echo '<a class="add-custom-fee" data-nonce="' . $nonce . '" data-post_id="' . $order->id . '" href="' . $link . '">Add fee?</a>'; ,与请求URL不匹配。

这是我使用wp-admin/post-new.php?post_type=shop_order函数排队的jQuery脚本:

$.ajax

然后用我的PHP创建费用对象并将其添加到订单中:

jQuery(document).ready(function ($) {

  $('.add-custom-fee').click( function(e) {
    e.preventDefault();

    var add_fee_toggle = $("input[name='add_fee_toggle']:checked").val();
    var add_fee_name = $("input[name='add_fee_name']").val();
    var add_fee_percentage = $("input[name='add_fee_percentage']").val();

    // Remove cookie and do not apply fee
    if (add_fee_toggle === '0') {

      // Code to clear custom field values

    } else {

      order_id = $(this).attr('data-post_id');
      nonce = $(this).attr('data-nonce');

      // Push fee values to AJAX function
      $.ajax({
        type: 'post',
        dataType: 'json',
        url: myAjax.ajaxurl,
        data: {
          action: 'create_fee_object',
          order_id: order_id,
          nonce: nonce,
          add_fee_toggle: add_fee_toggle,
          add_fee_name: add_fee_name,
          add_fee_percentage: add_fee_percentage
        },
        success: function(response) {
          if ( response.type == 'success' ) {
            $("input[name='add_fee_toggle'][value='0']").prop('checked', true);
            // Trigger the order to recalculate on success
            $('button.calculate-action').trigger('click');
          } else {
            alert('An error has occurred while adding your fee.');
            $('button.calculate-action').trigger('click');
          }
        }
      });
    }
  });
});

我可以在管理员创建的新订单中添加费用吗?

对此有任何见识将不胜感激。谢谢!

0 个答案:

没有答案