簡體   English   中英

添加項目后,WooCommerce重定向到購物車

[英]WooCommerce Redirect to Cart After Item Added

我添加了代碼,一次只允許購物車中有1件商品。 但是,當用戶將任何項目添加到他們的購物車時,它不會將其定向到“購物車”頁面。

這是我正在使用的代碼:

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
wc_add_notice( 'WARNING MESSAGE - You can only have 1 Item in your Cart. Previous Items have been removed.', 'error' );

return $cart_item_data;
}

因此,我的目標是保留此功能和錯誤消息,但將用戶帶到購物車。 我確定此代碼中有某些內容阻止用戶前往購物車並停留在產品頁面上。

在此先感謝您的幫助!

如果添加錯誤,則WooCommerce將不會重定向。 參見來源 您可以嘗試將通知類型設置為“ notice”。

添加商品時清除購物車:

add_filter( 'woocommerce_add_to_cart_validation', 'so_41002991_empty_cart' );
function so_41002991_empty_cart( $valid ){
    WC()->cart->empty_cart();
    wc_add_notice( __( 'WARNING MESSAGE - You can only have 1 Item in your Cart. Previous Items have been removed.', 'your-plugin' ), 'notice' );
    return $valid;
}

有一個WooCommerce設置將啟用重定向到購物車。 但是,您也可以選擇使用一個過濾器:

add_filter( 'woocommerce_add_to_cart_redirect', 'so_41002991_redirect_to_cart' );
function so_41002991_redirect_to_cart( $url ){
    return wc_get_cart_url();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM