2
votes

I am trying to display the shipping class name and the shipping flat rate for the product on WooCommerce single product page. I have used the code below:

$shipping_class_id = $product->get_shipping_class_id();
$shipping_class= $product->get_shipping_class();
$fee = 0;

if ($shipping_class_id) {
   $flat_rates = get_option("woocommerce_flat_rates");
   $fee = $flat_rates[$shipping_class]['cost'];
}

$flat_rate_settings = get_option("woocommerce_flat_rate_settings");
echo 'Shipping cost: ' . ($flat_rate_settings['cost_per_order'] + $fee);

I can get the shipping class id and also the shipping class slug with this code but not the label. I can not also get the cost of that particular shipping class which is a flat rate of 5 Euro as defined in shipping zones section.

Looking forward to your replies.

1

1 Answers

4
votes

1) Get the shipping Class Label name (from a product):

The shipping class is registered as a term, so you can get the product shipping Class label easily using:

$shipping_class_id   = $product->get_shipping_class_id();
$shipping_class_term = get_term($shipping_class_id, 'product_shipping_class');

if( ! is_wp_error($shipping_class_term) && is_a($shipping_class_term, 'WP_Term') ) {
    $shipping_class_name  = $shipping_class_term->name;
}

2) Shipping methods:

As shipping methods are based on customer shipping location, If you have multiple shipping zones, you can't really get for a product a specific shipping method as you would like.

Also the shipping cost is calculated from cart packages in multiple possibilities depending on all your different shipping settings.

Now if you have only one shipping zone and you want to target a specific shipping method and get the shipping method title and it's approximate cost (like in settings), based on the product tax class, try the following:

// HERE set your targeted shipping Zone type (method ID)
$targeted_shipping_method_id = 'flat_rate';

// The product shipping class ID
$product_class_id = $product->get_shipping_class_id();

$zone_ids = array_keys( array('') + WC_Shipping_Zones::get_zones() );

// Loop through Zone IDs
foreach ( $zone_ids as $zone_id ) {
    // Get the shipping Zone object
    $shipping_zone = new WC_Shipping_Zone($zone_id);
    // Get all shipping method values for the shipping zone
    $shipping_methods = $shipping_zone->get_shipping_methods( true, 'values' );

    // Loop through Zone IDs
    foreach ( $shipping_methods as $instance_id => $shipping_method ) {
        // Shipping method rate ID
        $rate_id = $shipping_method->get_rate_id();

        // Shipping method ID
        $method_id = explode( ':', $rate_id);
        $method_id = reset($method_id);

        // Targeting a specific shipping method ID
        if( $method_id === $targeted_shipping_method_id ) {
            // Get Shipping method title (label)
            $title = $shipping_method->get_title();
            $title = empty($title) ? $shipping_method->get_method_title() : $title;

            // Get shipping method settings data
            $data = $shipping_method->instance_settings;

            ## COST:

            // For a defined shipping class
            if( isset($product_class_id) && ! empty($product_class_id) 
            && isset($data['class_cost_'.$product_class_id]) ) {
                $cost = $data['class_cost_'.$product_class_id];
            }
            // For no defined shipping class when "no class cost" is defined
            elseif( isset($product_class_id) && empty($product_class_id) 
            && isset($data['no_class_cost']) && $data['no_class_cost'] > 0 ) {
                $cost = $data['no_class_cost'];
            } 
            // When there is no defined shipping class and when "no class cost" is defined
            else {
                $cost = $data['cost'];
            }

            // Testing output
            echo '<p><strong>'.$title.'</strong>: '.$cost.'</p>';
        }
    }
}

Now if you want to access shipping method data