Snippet Category WooCommerce

Change Placeholder Image

add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src($src) {
    $upload_dir = wp_upload_dir();
    $uploads = untrailingslashit($upload_dir['baseurl']);
    $src = $uploads . '/2012/07/placeholder.png';
    return $src;
}

Hide Shipping Rates When Free Shipping is Available

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
function hide_shipping_when_free_is_available( $rates, $package ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return !empty( $free ) ? $free : $rates;
}

Customize “My Account” Tabs

add_filter ( 'woocommerce_account_menu_items', 'misha_custom_my_account_menu_items' );
function misha_custom_my_account_menu_items( $items ) {
    $items = array(
        'dashboard'       => __( 'Dashboard', 'woocommerce' ),
        'orders'          => __( 'Orders', 'woocommerce' ),
        // add more items or remove by unset()
    );
    return $items;
}

Automatically Log in New Users After Registration

add_action('woocommerce_registration_redirect', 'auto_login_new_user');
function auto_login_new_user($redirect_url) {
    $auto_login = true; // change to false to disable auto login
    if ($auto_login) {
        $customer_login = wc_get_customer_login_from_session();
        if ($customer_login) {
            wp_set_current_user($customer_login);
            wp_set_auth_cookie($customer_login);
        }
    }
    return $redirect_url;
}

Remove Billing Fields if Cart Contains Only Virtual Products

add_filter('woocommerce_checkout_fields', 'remove_billing_on_virtual');
function remove_billing_on_virtual($fields) {
    $only_virtual = true;

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        if (!($cart_item['data']->is_virtual())) $only_virtual = false;
    }

    if ($only_virtual) {
        unset($fields['billing']);
    }

    return $fields;
}

Change Number of Products Per Row

add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
    function loop_columns() {
        return 3; // 3 products per row
    }
}