Get label name from product WooCommerce shipping class Id

347 views Asked by At

I want to display product shipping class label(character) instead its id(number). I mean, as seen in the image below, I wnat to chage 0 to Free or flat-rage etc. How or where do I have to edit under the relevant code below? Can I be helped by anybody professional? enter image description here

The relevant code is

if ($sale_carousel_items_content) {

      $carousel_items .= '<ul class="products" style="background:yellow; border:none; padding-left:50px;">';
    enter code here
          foreach ($sale_carousel_items_content as $item) {
        if ( isset( $item['product_id'] ) ) {
          $product = new WC_Product( $item['product_id'] );
          if ( $product->is_on_sale() ) {
            $countdown_container_id = uniqid('countdown',false);
            $target = explode("-", $item['target_date']);
            $carousel_items .= '<li class="row"><div class="img-wrapper col-xs-12 col-md-6">';
            $carousel_items .= '<a href="'.esc_url($product->get_permalink()).'" class="link-to-product">'.$product->get_image( 'shop_catalog' ).'</a>';
            $carousel_items .= '<h6>'.$product->get_shipping_class_id().'</h6></div>'; 
            $carousel_items .= '<div class="counter-wrapper col-xs-12 col-md-6"><h6>'.$product->get_name().'</h6>'; 
            // Short Description
            $carousel_items .= '<div class="woocommerce-product-details__short-description"><h6>'.$product->get_short_description().'</h6>';
            // Sale value in percents
            $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
            $carousel_items .= '<span class="sale-value" style="background:orange;"><font style="font-size:15px;">특판가         </font>-'.$percentage.'% </span>';
            $carousel_items .= '<div><h5></h5>';
            $carousel_items .= '<div class="price-wrapper"><h5>남은수량: '.$product->get_stock_quantity().'개</h5>';

            //$carousel_items .= '<div class="countdown-wrapper">';
            if ( $item['pre_countdown_text'] && $item['pre_countdown_text'] != '' ) {
              $carousel_items .= '<p>'.esc_attr($item['pre_countdown_text']).'</p>';
            }
            //$carousel_items .= '<div id="'.$countdown_container_id.'"></div></div>';
            //$carousel_items .= '<div class="price-wrapper">'.do_shortcode('[add_to_cart id="'.$item['product_id'].'"]').'</div>';
            if ( $target && $target!='' ) {
                        $carousel_items .='
                        <script type="text/javascript">
                            (function($) {
                                $(document).ready(function() {

                                    var container = $("#'.$countdown_container_id.'");
                                    var newDate = new Date('.$target[0].', '.$target[1].'-1, '.$target[2].');
                                    container.countdown({
                                        until: newDate,
                                    });

                                });
                            })(jQuery);
                        </script>';
                    }
                    $carousel_items .= '</div></li>';
            }
        }
    }
    $carousel_items .= '</ul>';
}
1

There are 1 answers

5
LoicTheAztec On BEST ANSWER

Final update

Use the following to get the term name (label name) from a product shipping class Id:

$term_id   = (int) $product->get_shipping_class_id(); // Get the shipping class Id
$term_name = ''; // Initializing

if( $term_id > 0 ) {
    $term = get_term( $term_id, 'product_shipping_class' );
    if( is_a( $term, 'WP_Term') ) {
        $term_name = $term->name;
    }
} 

// testing output
echo '<h6>'. $term_name .'</h6>';

So in your code replace the line

$carousel_items .= '<h6>'.$product->get_shipping_class_id().'</h6></div>';

by the following code block:

$shipping_class_id   = (int) $product->get_shipping_class_id(); // Get shipping class Id
$shipping_class_name = ''; // Initializing
if ( $shipping_class_id > 0 ) {
    $shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' ); // Get the WP_Term Object
    if ( is_a( $shipping_class_term, 'WP_Term') ) {
        $shipping_class_name = $shipping_class_term->name; // The label name (term name)
    }
}
$carousel_items .= '<h6>'.$shipping_class_name.'</h6></div>'; // Add to output

Tested and works.