How to add shipping class in product page

This topic has 7 replies, 2 voices, and was last updated 1 years, 7 months ago ago by Rose Tyler

  • Avatar: lew-shop
    lew-shop
    Participant
    September 15, 2022 at 13:50

    how i can add shipping class to product page before buttom “shop now”?
    I try use this code but no working.

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

    $shipping_class = $product->get_shipping_class();

    switch ( $shipping_class ) {
    case ‘free-shipping’:
    echo ‘

    Free shipping on this item

    ‘;
    break;
    case ‘flat-rate’:
    echo ‘

    Flat rate shpping $10

    ‘;
    break;
    }
    }
    add_action( ‘woocommerce_before_single_product’, ‘wpse_295878_shipping_banner’, 20 );

    6 Answers
    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    September 15, 2022 at 14:19

    Hello,

    Your code needs to be improved
    Use next code snippet instead of your one

    function etheme_child_product_shipping_banner() {
    global $product;
    
    $shipping_class = $product->get_shipping_class();
    
    switch ( $shipping_class ) {
        case 'free-shipping':
            echo 'Free shipping on this item';
        break;
        case 'flat-rate':
            echo 'Flat rate shpping $10';
        break;
    }
    }
    add_action('woocommerce_before_single_product', 'etheme_child_product_shipping_banner', 20 );

    Result: https://prnt.sc/rLBkW2B8iuG4

    Regards

    Avatar: lew-shop
    lew-shop
    Participant
    September 16, 2022 at 13:08

    And what name should I enter in: case ‘free-shipping’

    When I enter my name it doesn’t work. e.g:

    function etheme_child_product_shipping_banner() {
    global $product;

    $shipping_class = $product->get_shipping_class();
    
    switch ( $shipping_class ) {
        case 'free-shipping':
            echo 'Free shipping on this item';
        break;
        case 'flat-rate':
            echo 'Flat rate shpping $10';
        break;
    }
    }
    add_action('woocommerce_before_single_product', 'etheme_child_product_shipping_banner', 20 );

    Or maybe it can be done with a static block or custom html code in single product builder?
    https://prnt.sc/5HHAnUsAUqxI

    Avatar: lew-shop
    lew-shop
    Participant
    September 16, 2022 at 13:09

    my code

    function etheme_child_product_shipping_banner() {
    global $product;
    
    $shipping_class = $product->get_shipping_class();
    
    switch ( $shipping_class ) {
        case 'duze-inpost':
            echo 'Kurier';
        break;
    	case 'drobnica':
            echo 'paczkomat';
        break;
    }
    }
    add_action('woocommerce_before_single_product', 'etheme_child_product_shipping_banner', 20 );
    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    September 16, 2022 at 13:57

    Hello,

    As we see in the product (1st link from private) you have successfully showed the text for https://prnt.sc/PE1RbLr1cVj4 using our code snippet.

    All possible values you may use are here (they are created from your side) -> https://prnt.sc/tUMDPWLRmnRY or you may create new shipping classes and use new ones too.
    Example:

    function etheme_child_product_shipping_banner() {
    global $product;
    
    $shipping_class = $product->get_shipping_class();
    
    switch ( $shipping_class ) {
    case 'duze-inpost':
    echo 'Kurier';
    break;
    case 'paleta-niestandardowa-dhl':
    echo 'paleta-niestandardowa-dhl text';
    break;
    case 'lekker-niest-bez-pobrania':
    echo 'lekker-niest-bez-pobrania custom text';
    break;
    }
    }
    add_action('woocommerce_before_single_product', 'etheme_child_product_shipping_banner', 20 );

    P.S. Foreach item here -> https://prnt.sc/tUMDPWLRmnRY you may add own special

    case 'your-slug':
            echo 'your custom text for this shipping class';
        break;

    We hope, we provided you maximum detalized info about how to achieve your results.

    Regards

    Please contact administrator
    for this information.
    Avatar: lew-shop
    lew-shop
    Participant
    September 16, 2022 at 14:00

    Or maybe it can be done with a static block or custom html code in single product builder?
    https://prnt.sc/5HHAnUsAUqxI

    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    September 16, 2022 at 14:29

    No, please use the method that was described in my previous reply.

    Regards

  • Viewing 7 results - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.