Show Content On Single Product Based On Shipping Class – Shortcode

With the shortcode [shippingclass] you can display any content on your single product page, which you specify per shipping class within your functions.php. This Snippet includes a fallback which will hop in if the product does not match any (within your function.php file) specified shipping class by you.

I did use it for displaying a free shipping label below the price, if the shipping class is set to free shipping. If the shipping class is not free, the snippet throws out the usual „plus shipping“. This is an enormous powerful snippet, as it allows you to display any content for each shipping class you provide.

// Show Shipping Class Shortcode

function shipping_banner() {
    $product = wc_get_product();

    $shipping_class = $product->get_shipping_class();

    switch ( $shipping_class ) {
        case 'kostenloser-versand': // Shipping Class Slug
            echo 'Some Taxes <div class="free-shipping">Yay.. Free Shipping!</div>'; // Desired Content For This SC
            break;
        default: // Means Fallback
            echo 'Some Taxes <a href="http://example.com/shipping/">Plus Shipping</a>'; // Fallback Content
    }
}
add_shortcode( 'shippingclass', 'shipping_banner', 20 );

WHERE TO PLACE THIS CODE?

Straight to your functions.php in your active theme folder. But please – use a child theme. 😉

Pleasure?

Please share your thumbsup, suggestions, edits, and questions within my comment section down there. 

I love posting snippets for you, but with a bit of feedback, it gets even more fun. 😀

Sharing is caring:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert