使用下面的代码,客户可以在结帐时从购物车中删除商品,但如果删除所有商品 - 客户应该被重定向到商店页面。
我已经尝试了下面的代码,但它无法正常工作,客户仍然留在“
The checkout is not available when your cart is empty
”上,后面是一个按钮,可以将客户带到商店。
这个想法不仅仅是重定向,而且还使用
wc_add_notice
显示消息。
代码的第一部分是这样的:
add_filter('woocommerce_cart_item_name', 'product_thumbnail_and_remove_on_checkout', 20, 3 );
function product_thumbnail_and_remove_on_checkout( $product_name, $cart_item, $cart_item_key ) {
if ( is_checkout() && ! is_wc_endpoint_url() ) {
$cart = WC()->cart->get_cart();
foreach ($cart as $cart_key => $cart_value){
if ($cart_key == $cart_item_key){
$product_id = $cart_item['product_id'];
$_product = $cart_item['data'] ;
$remove_product = sprintf(
'<a class="remove" style="float:left;margin-right:5px;" href="%s" title="%s" data-product_id="%s" data-product_sku="%s" data-cart_item_key="%s">×</a>',
esc_url(wc_get_cart_remove_url( $cart_key)),
__( 'Remove From Order', 'woocommerce' ),
esc_attr($product_id),
esc_attr($_product->get_sku()),
esc_attr($cart_item_key)
);
}
}
$product = $cart_item['data'];
$thumbnail = $product->get_image(array(50, 50));
$image_html = '<div class="product-item-thumbnail">' . $thumbnail . '</div>';
$product_name_link = '<a class="pnoc" href="' . $product->get_permalink() . '">' . $product_name . '</a>';
$product_name = $remove_product . $image_html . $product_name_link;
}
return $product_name;
}
我正在尝试使工作的代码:
add_action( 'wp_enqueue_scripts', 'cart_js_on_checkout', 10 );
function cart_js_on_checkout() {
if ( is_checkout() ) {
wp_enqueue_script( 'wc-cart' );
}
}
add_action( 'template_redirect', 'redirect_empty_checkout_to_shop' );
function redirect_empty_checkout_to_shop() {
if ( is_checkout() && WC()->cart->is_empty() && ! is_wc_endpoint_url( 'order-pay' ) && ! is_wc_endpoint_url( 'order-received' ) ) {
wp_safe_redirect( wc_get_page_permalink( 'shop') );
wc_clear_notices();
wc_add_notice( __( 'The checkout is not available when your cart is empty. Happy shopping!', 'woocommerce' ), 'info' );
}
}
如果我理解正确的话: 您想在从购物车中删除最后一件商品后立即重定向用户吗?
现在,在我的 WordPress 和 Woo 副本上,当我从购物车中删除商品时,页面不会重新加载。仅部分页面使用 AJAX 更新(我认为您可以在 Woo 中更改此设置)。
因此,在删除购物车和您在页面上看到的内容之间可能没有调用“模板挂钩”。您可能仍然需要“模板挂钩”部分,以防用户在购物车为空时导航到购物车。这会将他们重定向出那里。
但是,如果它发生在购物车页面上的 AJAX 中,除了上面的内容之外,您还可以尝试此操作:
function redirect_cart_when_empty() {
echo '<script type="text/javascript">
;( function( $, window, document, undefined ) {
"use strict";
$( document ).ready( function() {
$(document.body).on("wc_cart_emptied", function(){
window.location.replace("'.wc_get_page_permalink( 'shop').'");
});
} );
} ) ( jQuery, window, document );
</script>';
}
add_action('woocommerce_before_cart_table', 'redirect_cart_when_empty');
我会尝试解释我们想要什么,所以如果它不起作用,你也许可以弄清楚。基本上我们可以在前端(客户端)使用 Woo 的 javascript (jQuery) 触发器来启动一些简单的 javascript 来执行重定向/重新加载。
关于这些钩子的文档不多,但我选择的钩子希望在购物车清空时被 Woo 解雇。一旦我们有了一些 Javascript,我们就可以知道购物车是空的,我们可以简单地将页面重定向到商店永久链接。
我只是将我的“标准”jQuery 包装器放在它周围...我不是 jQuery 专家,我只是涉猎了一点。
我不确定何时使用这个钩子,但是还有其他一些可以使用,例如
removed_from_cart
,cart_totals_refreshed
等。这不会那么干净,但你可以检查总数购物车并进行重定向等...也许有一个“空购物车按钮”会触发它?我不知道。
最后一点是我认为
window.location.replace()
更好,因为这将消除使用后退按钮返回空购物车并重定向回商店的情况。如果我们只是使用 window.location.href
的话,这只会为用户节省一些麻烦。
您也可以简单地重新加载页面并允许“模板挂钩”使用
window.location.reload();
进行重定向,然后您不需要将 PHP 与 javascript 混合在一起。
你就在附近。将最后一个函数替换为以下内容,当购物车在结帐(或购物车)清空时,该函数将重定向到商店页面,并在商店页面中显示通知:
add_action( 'template_redirect', 'redirect_empty_checkout_to_shop' );
function redirect_empty_checkout_to_shop() {
if ( ( ( is_checkout() && ! is_wc_endpoint_url() ) || is_cart() ) && WC()->cart->is_empty() ) {
// We add a WC session variable to display the notices in shop
WC()->session->set('display_redir_notice', '1' );
// The redirection
wp_safe_redirect( wc_get_page_permalink( 'shop') );
exit(); // exit silently
} elseif ( is_shop() && WC()->session->get('display_redir_notice') ) {
// Display the notice in shop page
wc_add_notice( __( 'The checkout is not available when your cart is empty. Happy shopping!', 'woocommerce' ), 'info' );
// Remove the WC session variable
WC()->session->__unset('display_redir_notice');
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。