Search
Close this search box.
Search
Close this search box.

WooCommerce Hooks

If you use a hook to add or manipulate code, you can add your custom code to your theme functions.php file.

Here is a full list of hooks and filters used in WooCommerce, as well a the files in which they are called.

Removing breadcrums

remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);

Removing product sorting options

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

Removing result count text

The texts that says (Showing x – x of x results).

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );

Removing related products

remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

Removing single product meta (SKU, categories and tags)

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);

How to redirect to shop page after add to cart action

add_filter('add_to_cart_redirect', 'redirect_to_shop');

function redirect_to_shop() {
    $checkout_url = get_permalink(woocommerce_get_page_id('shop'));
    return $checkout_url;
}

Hide other shipping options when Free Shipping is available

If the code below does not work, try adding an item to your cart and retest. It may be a cache problem.

add_filter('woocommerce_package_rates', 'unset_shipping_when_free_is_available', 10, 2);

function unset_shipping_when_free_is_available($rates, $package) {

    // Only unset rates if free_shipping is available
    if (isset($rates['free_shipping'])) {

        $free_shipping = $rates['free_shipping'];
        $rates = array();
        $rates['free_shipping'] = $free_shipping;
    }

    return $rates;
}

Reorder cart items alphabetically

add_action('woocommerce_cart_loaded_from_session', reorder_cart, 100);

function reorder_cart() {
    global $woocommerce;
    $products_in_cart = array();
    foreach ($woocommerce->cart->cart_contents as $key => $item) {
        $products_in_cart[$key] = $item['data']->get_title();
    }
    natsort($products_in_cart);
    $cart_contents = array();
    foreach ($products_in_cart as $cart_key => $product_title) {
        $cart_contents[$cart_key] = $woocommerce->cart->cart_contents[$cart_key];
    }
    $woocommerce->cart->cart_contents = $cart_contents;
}

Change sale with percentage

add_filter('woocommerce_sale_flash', 'sale_price_use_percentage');

function sale_price_use_percentage() {
    global $product;
    $final_price = $product->price;
    if ($final_price != $product->regular_price) {
        $percentage = 100 - round(($final_price / $product->regular_price) * 100);
        echo '<span class="onsale">-' . $percentage . '%</span>';
    }
}

Learning Resources