Search code examples
wordpresswoocommercehook-woocommercewoocommerce-rest-api

In WooCommerce, Is it possible to display specific country only if cart total is over 100?


I have tried to do it using a filter hook called "woocommerce_countries". I tried getting the cart total in this hook but it's not working. Is anyone have any idea on this OR suggest any hook?


Solution

  • Please try with this code. WooCommerce 3.6.2, they have removed the ability to check the cart data. But cart session is accessible right now so we use this function WC()->session->cart

    add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
    function products_disable_country( $countries ) {
        $cartdata = WC()->session->cart;
        if (is_array($cartdata) && !empty($cartdata)) {
            foreach ($cartdata as $itemdata) {
                $sum += $itemdata['line_total'];
            }
            if ($sum >= 100) {
                unset($countries["CA"]);
            }
        }
        return $countries;
    }