2015-06-09 67 views
0

我的客戶希望結賬「精簡」以跳過購物車頁面並一次直接結帳一個產品。我得到了這個覆蓋,但如果客戶決定不確認結賬,我還需要自動清空購物車。確定是否在woocommerce的商店頁面上

爲此,我想檢查我是否位於購物車或結帳之外的任何其他頁面上,並在此處執行此操作,但是我嘗試了所有命令(is_shop(),is_front_page(),is_page('Shop')) is_product(),is_home())總是返回false,所以我不知道該怎麼做。這是我試圖做到這一點(在我的themses functions.php):

function reset_cart_front() { 
    global $woocommerce; 
    echo "Attempting to empty<br>"; 

    if (is_shop()) { 
     echo "is right page<br>"; 
     $woocommerce->cart->empty_cart(); 
    } else { 
     echo "is not right<br>"; 
    } 

} 
add_action('init', 'reset_cart_front'); 

什麼給?

+1

由於查詢變量尚未設置,'init'對於執行任何條件邏輯測試(例如'is_shop()')爲時過早。 – helgatheviking

回答

0

沒關係,我知道了!

function reset_cart_shop_loop() { 
    global $woocommerce; 
    $woocommerce->cart->empty_cart(); 
} 
add_action('woocommerce_before_shop_loop', 'reset_cart_shop_loop'); 
0

糾正我,如果我錯了......但我認爲你應該檢查當前is_checkout()頁,並清空購物車,如果它不是:

function reset_cart_front() { 
    global $woocommerce; 

    if (!is_checkout()) { 
     $woocommerce->cart->empty_cart(); 
    } 

} 
add_action('template_redirect', 'reset_cart_front'); 

我也覺得'init'太儘早掛鉤(並建議嘗試'template_redirect')。

相關問題