Handy WooCommerce Code Snippets

Published on January 23, 2020 by Sebastian Moran in MainWP Blog under Tips & Tricks
Heads up: This page may include affiliate links. Read the full disclaimer.

WooCommerce is a very heavily used plugin to turn your WordPress child site into a full eCommerce store.

MainWP offers a very easy to use code snippets extension which makes it easy to run code snippets over all of the connect child sites.

A couple of common of handy code snippets if you needed to temp disable all products from being purchasable on your child site you can use this code snippet;

add_filter( 'woocommerce_is_purchasable', '__return_false');

You would then need to deactivate that code snippet from the child sites where it was running.

Another common code snippet fix would be for changing the read more button text which normally shows for products which are out of stock you can make that change using this code snippet;

add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
    if ( 'Read more' == $text ) {
        $text = __( 'More Info', 'woocommerce' );
    }

    return $text;
} );

Going along with the previous code snippet if you wanted to change the link that would show for out of stock products you can use this code snippet;

add_filter( 'woocommerce_product_add_to_cart_url', 'out_of_stock_read_more_url', 50, 2 );
function out_of_stock_read_more_url( $link, $product  ) {
    // Only for "Out of stock" products
    if( $product->get_stock_status() == 'outofstock' ){

        // Here below we change the link
        $link = home_url("/");
    }
    return $link;
}

Using the code snippets extension is an easy way to make code changes over all connect child sites.

Remember to join the MainWP community which has a whole category where new handy code snippets are being added every week.

Share

Manage Unlimited WordPress Sites from One Dashboard!

  • Privacy-first, Open Source, Self-hosted
  • Easy Client Management
  • 15+ & 30 + Premium Add-ons
  • Bulk Plugins & Themes Management
Get Pro Now

Categories

Recent Posts

Search MainWP.com

[searchwp_form id="1"]