skip to Main Content

I would like to display a message on the Product Page depending on the shipping class of the product – if shipping class is "X" don’t display shortcode, else display it.
We have set only local pick up for heavy furniture and don’t want these items to display our regular message of 48-72 hours shipping time.

I found this post about the same topic, but I can’t figure out how the final code must be, since I’m not a developer: wooCommerce – get shipping class from the Product Page

Any help would be appreciate, thanks in advance!

The code from the other post:

$Set_shipping_class = 'furniture-oversized';

if ( $Set_shipping_class  === WC_Product::get_shipping_class()); {
  add_action( 'woocommerce_before_add_to_cart_form', ‘display_Shortcode’, 100 );
  function display_Shortcode() {
    echo do_shortcode('[phoeniixx-pincode-check]');
}              
        }

A second trying:

$Set_shipping_class = 'furniture-oversized';
$_product = wc_get_product();
$shipclass = $_product->get_shipping_class(); //this line kills the site

And the final solution:

$_product = wc_get_product();
$shipclass = $_product->get_shipping_class(); 

is the right way to go, but you need to pass the post id of the product in to wc_get_product(), so the first line should actually be:

$_product = wc_get_product( $post_id );

not forgetting to set the $post_id variable first.

I don’t know how to put all the code toghether.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you so much, you helped me a lot @LoicTheAztec


  2. Try the following instead (where the IF statement needs to be inside the function):

    add_action( 'woocommerce_before_add_to_cart_form', 'display_shortcode_before_add_to_cart_form', 100 );
    function display_shortcode_before_add_to_cart_form() {
        global $product;
    
        if ( 'furniture-oversized' === $product->get_shipping_class() ) {
            echo do_shortcode('[my_custom_shortcode]');
        }
    }    
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search