您好,我有此功能,可在我的帐户->订单列表中添加取消按钮
add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'custom_valid_order_statuses_for_cancel',1, 2 );
function custom_valid_order_statuses_for_cancel( $statuses, $order ){
// Set HERE the order statuses where you want the cancel button to appear
return array_merge( $statuses, array('processing', 'shipped'));
}
我想在他按下询问按钮时添加一条JavaScript确认消息(确定要取消订单吗?)
我如何才能实现将javascript放入函数中却没有运气的情况
取消按钮的HTML代码
<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-actions" data-title="Actions">
<a href="http://localhost/zumra/wordpress/my-account/view-order/1028/" class="woocommerce-button button view">View</a><a
href="http://localhost/zumra/wordpress/cart/?cancel_order=true&order=wc_order_CvYEzh8G4OKJ8&order_id=1028&redirect=http%3A%2F%2Flocalhost%2Fzumra%2Fwordpress%2Fmy-account%2F&_wpnonce=938ffd4571" class="woocommerce-button button cancel">Cancel</a><a href="/zumra/wordpress/my-account/orders/2/?bewpi_action=view&post=1028&nonce=17f4df17bd" class="woocommerce-button button invoice">Invoice</a> </td>
javascript代码:
( function($) {
$("#s").autocomplete({
source: product_lib.products_array
});
$('.woocommerce-button button cancel').click(function(e){
e.preventDefault();
if (confirm('are you sure?')) {
$('#idOfTheForm').submit();
}
});
document.getElementsByClassName('woocommerce-button button cancel').onclick = function() {
preventDefault();
alert("button was clicked");
};
window.onload = function() {
if (window.location.href.indexOf("comment") > -1) {
window.location.hash = "#tab-description";
location.reload();
}
}
})(jQuery);
答案 0 :(得分:1)
第一个想法(无代码) 您必须将其放入打印取消按钮的网站上加载的任何.js文件中。如果您不知道要加载哪些.js文件:右键单击=>检查=>网络=>重新加载页面=>按“ js”过滤
如果没有可以编辑的文件,您仍然可以将此js代码放在JS-HERE标签中
jQuery('#idOfCancelButton').click(function(e){
if (!confirm('are you sure?') {
e.preventDefault();
}
}
新想法
代替
preventDefault();
alert("button was clicked");
做
if (!confirm('Are you sure?')) {
preventDefault();
}
答案 1 :(得分:0)
您添加的选择器错误 试试这个与我一起工作的代码段:
function confirm_order_cancel(){
?>
<script type="text/javascript">
(function($){
$('.woocommerce-button.button.cancel').click( function(){
var txt;
var r = confirm("Are you sure you want to cancel your order ?!");
if (r == true) {
txt = "You pressed OK!";
} else {
return false;
}
$(this).innerHTML = txt;
});
})(jQuery);
</script>
<?php
}
add_action('wp_footer', 'confirm_order_cancel');