Charly Anderson | Handy WooCommerce Snippets

Handy WooCommerce Snippets


WooCommerce is a free Wordpress plugin that enables you to sell products directly via your website. Here are some handy snippets to improve functionality.

You can use the snippets I have provided below by adding these into your theme’s functions.php file. Before doing this, I would recommend backing up your website before making any changes.

Declare WooCommerce Support

If you are developing a bespoke theme and WooCommerce is supported, you should add this snippet to declare support. Without this, users will be prompted that the theme doesn’t support WooCommerce.

add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
    add_theme_support( 'woocommerce' );
}

Remove Cart Button From Shop Page

Do you want people to look at the product before adding it to the cart? If yes, use this snippet to disable the button on the shop page. Users will then need to click on the product before purchasing.

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10); 

Add Custom Pagination

Want to customise the pagination? Add this snippet and adjust to suit your requirements.

add_filter( 'woocommerce_pagination_args',  'custom_woo_pagination' );

function custom_woo_pagination( $args ) {
    $args['prev_text'] = 'Your code here';
    $args['next_text'] = 'Your code here';
    return $args;
}

Wrap Product Images In A Div

Sometimes, for styling purposes, you may wish to wrap your product images in a DIV. Use this snippet and change the markup to suit your requirements.

add_action( 'woocommerce_before_shop_loop_item_title', create_function('', 'echo "<div>";'), 5, 2);
add_action( 'woocommerce_before_shop_loop_item_title',create_function('', 'echo "</div>";'), 12, 2);

Change the redirect URL for the ‘Return to shop’ button

If you want to bypass the main shop page in your website, you can use this snippet to change the button URL to anything you want.


function wc_empty_cart_redirect_url() {
    return 'your-url-here';
}

Remove Tabs On Product Pages

Not all websites need these extra tabs – simply add this snippet to remove them from your site. You do not need to use them all so just delete the unset lines to suit your requirements.

add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );

function woo_remove_product_tabs( $tabs ) {
    unset( $tabs['description'] );
    unset( $tabs['reviews'] );
    unset( $tabs['additional_information'] );
    return $tabs;
}

Overide Crop Settings For Single Product Images

WooCommerce recently changed their crop settings. Originally you could hard crop the single product images but for some reason, they have disabled this feature. By add the snippet below, you can override this and set the crop sizes to suit your needs.

Remember to regenerate your thumbnails after implementing this snippet.

add_filter( 'woocommerce_get_image_size_single', 'my_set_product_img_size' );
add_filter( 'woocommerce_get_image_size_shop_single', 'my_set_product_img_size' );
add_filter( 'woocommerce_get_image_size_woocommerce_single', 'my_set_product_img_size' );
function my_set_product_img_size()
{
    $size = array(
        'width'  => 600,
        'height' => 600,
        'crop'   => 1,
    );
    return $size;
}

Enable New Image Features

In version 3, WooCommerce released some new image features for your products including zoom, lightbox and a slider. To enable these features, use the snippet below. You do not need to enable them all, you can delete the relevant ‘add_theme_support’ lines to suit your requirements.

Please note, you may need to implement some additional styling if these features do not display as expected.

add_action( 'after_setup_theme', 'yourtheme_setup' );
 
function yourtheme_setup() {
    add_theme_support( 'wc-product-gallery-zoom' );
    add_theme_support( 'wc-product-gallery-lightbox' );
    add_theme_support( 'wc-product-gallery-slider' );
}

Change Related Product Text

Changing this text couldn’t be easier. Pop in the snippet below and change the text to suit.

function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related Products' :
            $translated_text = __( 'Your new text choice here', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
Feb 25 2018 ,
Charly July 8th, 2018