【发布时间】:2020-05-01 15:04:39
【问题描述】:
在 WooCommerce 中的订单跟踪中,跟踪结果在提交跟踪表单时显示为一个全新的页面。但是,我想看到的是 AJAX 中的结果。
这是 woocommerce/templates/order/form-tracking.php 中的表单:
<form action="<?php echo esc_url( get_permalink( $post->ID ) ); ?>" method="post" class="woocommerce-form woocommerce-form-track-order track_order">
<p><?php esc_html_e( 'To track your order, please enter your Order ID and the email you used during checkout. You can find these in your receipt and confirmation email.', 'woocommerce' ); ?></p>
<p class="form-row form-row-first"><label for="orderid"><?php esc_html_e( 'Order ID', 'woocommerce' ); ?></label> <input class="input-text" type="text" name="orderid" id="orderid" value="<?php echo isset( $_REQUEST['orderid'] ) ? esc_attr( wp_unslash( $_REQUEST['orderid'] ) ) : ''; ?>" placeholder="<?php esc_attr_e( 'Found in your order confirmation email.', 'woocommerce' ); ?>" /></p><?php // @codingStandardsIgnoreLine ?>
<p class="form-row form-row-last"><label for="order_email"><?php esc_html_e( 'Billing email', 'woocommerce' ); ?></label> <input class="input-text" type="text" name="order_email" id="order_email" value="<?php echo isset( $_REQUEST['order_email'] ) ? esc_attr( wp_unslash( $_REQUEST['order_email'] ) ) : ''; ?>" placeholder="<?php esc_attr_e( 'Email you used during checkout.', 'woocommerce' ); ?>" /></p><?php // @codingStandardsIgnoreLine ?>
<div class="clear"></div>
<p class="form-row"><button type="submit" class="button" name="track" value="<?php esc_attr_e( 'Track', 'woocommerce' ); ?>"><?php esc_html_e( 'Track', 'woocommerce' ); ?></button></p>
<?php wp_nonce_field( 'woocommerce-order_tracking', 'woocommerce-order-tracking-nonce' ); ?>
</form>
我尝试将其添加到同一个文件中,紧跟在结束表单标记之后和脚本标记中:
var formData = new FormData();
formData.append('from_ajax', '1');// append this to your form.
$('.track_order').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: formData, // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
$('.track-results').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
if(isset($_POST['from_ajax']){
if($_POST['from_ajax'] == 1){
// do not load header or footer , just load required view to display.
}
}
我从大约 5 年前提出的另一个问题中找到了这个 jQuery,但是 jQuery 似乎没有做出任何改变,订单跟踪结果仍然显示为全新的页面。我不知道我还应该添加什么或者是否有任何问题。我应该在另一个文件中添加 jQuery 吗?我应该在 jQuery 中添加更多内容还是进行更多修改?
谢谢大家,我是个菜鸟!
【问题讨论】:
标签: php jquery ajax wordpress woocommerce