Php 取消撤消删除项目重定向
当我从/cart中删除项目时,单击“撤消”链接后,如何重定向回/cart 我不关心车是空的还是空的。我已经实现了一个Php 取消撤消删除项目重定向,php,wordpress,woocommerce,wordpress-theming,Php,Wordpress,Woocommerce,Wordpress Theming,当我从/cart中删除项目时,单击“撤消”链接后,如何重定向回/cart 我不关心车是空的还是空的。我已经实现了一个模板\u重定向,因此当用户从购物车中“删除”一个项目时,它会重定向回购物车而不是主页。与: add_action( 'template_redirect', function( ) { $_var = ( $_GET[ 'removed_item' ] ) ?? null; //var_dump($_var); if( $_v
模板\u重定向
,因此当用户从购物车中“删除”一个项目时,它会重定向回购物车而不是主页。与:
add_action( 'template_redirect', function( ) {
$_var = ( $_GET[ 'removed_item' ] ) ?? null;
//var_dump($_var);
if( $_var == '1' ){
wp_safe_redirect( '/cart', 301 );
die();
}
} );
我能做同样的动作吗?单击“撤消”按钮后,我没有看到查询字符串
编辑
我可以看到行动是在哪里进行的。它在插件includes/class-ws-form-handler.php中,就在第536行附近。。。包含:
// Undo Cart Item
$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
WC()->cart->restore_cart_item( $cart_item_key );
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
编辑2:
将以下内容添加到上面的重定向中不会起任何作用
// undo the remove, redirect back to cart
if ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
// Undo Cart Item
$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
WC()->cart->restore_cart_item( $cart_item_key );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
因此,当我
var\u dump
wp\u get\u referer()时,我会返回该站点的主页,这不是我要找的。我不想修改这个文件,因为这不是一个伟大的想法。。。那么,我如何修改操作呢?函数本身是:公共静态函数update\u cart\u action()。。。最后我不得不做一些巫术来让它起作用
这是钩子
// undo the remove, redirect back to cart
// check if the cart is updated... if it is, proceed
$_cu = apply_filters( 'woocommerce_update_cart_action_cart_updated', true );
if( $_cu ) {
global $woocommerce;
if ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
// item
$cart_item_key = sanitize_text_field( $_GET['undo_item'] );
// Get the cart
$woocommerce->cart->get_cart();
// restore the item
$woocommerce->cart->restore_cart_item( $cart_item_key );
// now do the redirect
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
它被应用于init事件got It。可能不漂亮,但它很管用