Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Shipping doesn’t get calculated until cart/checkout. If you want to show a quote for shipping on a product page, add the the short/long description.

    Thread Starter Chris

    (@comradeseidl)

    Hi Mike,

    I understand that shipping normally isn’t calculated until checkout. However, for my current project I need to show flat rate shipping on the product page.

    I’m not sure what you mean by, “add the the short/long description.”

    In lieu of using built-in functionality, I wrote this function to obtain an individual product’s shipping cost for the Flat Rate shipping method (does not work with fees, defaults to 1 quantity):

    function wc_get_flat_rate_shipping_cost( $product_object ) {
    
    	$flat_rate_settings	= maybe_unserialize( get_option( 'woocommerce_flat_rate_settings' ) );
    
    	$base_rate 			= isset( $flat_rate_settings['cost'] ) ? $flat_rate_settings['cost'] : 0;
    
    	$key				= 'class_cost_' . $product_object->get_shipping_class();
    	$class_rate			= isset( $flat_rate_settings[$key] ) ? $flat_rate_settings[$key] : 0;
    	$class_rate			= str_replace( '[qty]', 1, $class_rate );
    	$class_rate			= str_replace( '[cost]', $product_object->price, $class_rate );
    	$class_rate			= (int)$class_rate;
    
    	$total = ( isset( $base_rate ) && isset( $class_rate ) ) ? $base_rate + $class_rate : 0;
    
    	return $total;
    
    }

    Unless there is a function that I should be using from the WC_Shipping or WC_Shipping_Flat_Rate classes, I will mark this as resolved.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    There is no dedicated function in core for this, so your custom solution should be fine.

    If you know your shipping method, let’s say flat_rate, you can use this:
    $cost = WC_Shipping::instance()->load_shipping_methods()['flat_rate']->settings['class_cost_' . $product->get_shipping_class()];
    If anyone knows a better way to do it, I’ll be happy to know.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to Show Shipping Cost on Product Page’ is closed to new replies.