Skip to content

Instantly share code, notes, and snippets.

@seb86
Last active July 20, 2016 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seb86/d7a16fb67a99e99230221762045f7d90 to your computer and use it in GitHub Desktop.
Save seb86/d7a16fb67a99e99230221762045f7d90 to your computer and use it in GitHub Desktop.
Gets the shipping cost for the product from the assigned shipping class
/**
* Returns the shipping cost from the shipping class assigned to the product.
*
* @param object $product
* @return string
*/
function get_shipping_cost( $product ) {
$shipping_method = apply_filters( 'woocommerce_default_single_product_shipping_method', 'flat_rate' );
$shipping = new WC_Shipping();
$load_shipping_methods = $shipping->load_shipping_methods();
$get_shipping_method = $load_shipping_methods[$shipping_method];
// If the shipping method does not have any settings then return.
if ( ! array_key_exists( 'settings', $get_shipping_method ) ) {
return;
}
$shipping_settings = $get_shipping_method->settings;
$enabled = $shipping_settings['enabled']; // Is this shipping enabled?
if ( 'yes' === $enabled ) {
$product_shipping_class = $product->get_shipping_class(); // The shipping class assigned to the product.
$shipping_cost = $shipping_settings['class_cost_' . $product_shipping_class];
if ( is_numeric( $shipping_cost ) ) {
$shipping_cost = wc_price( $shipping_cost );
}
return $shipping_cost;
} // END if shipping enabled.
} // END get_shipping_cost()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment