当我从中删除某个项目时点击“撤消”链接后,如何重定向回到/ cart?
我不担心购物车是否空了。我必须已经实现template_redirect
,所以当用户从购物车“移除”某个商品时,它会重定向回购物车而不是主页。用:
add_action( 'template_redirect', function( ) {
$_var = ( $_GET[ 'removed_item' ] ) ?? null;
//var_dump($_var);
if( $_var == '1' ){
wp_safe_redirect( '/cart', 301 );
die();
}
} );
我可以挂钩同样的动作吗?单击“撤消”后,我看不到查询字符串...
修改
我可以看到行动的位置。它在插件中包含/ 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_dump
wp_get_referer()
时,我会回到网站的主页,这不是我想要的。我不想修改这个文件,因为这不是一个好主意......那么,我怎样才能修改动作呢?该函数本身是:public static function update_cart_action()
答案 0 :(得分:0)
所以......我最终不得不做一些伏都教来让它发挥作用。
这是钩子......
// 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事件