Last active
July 20, 2016 17:22
-
-
Save seb86/d7a16fb67a99e99230221762045f7d90 to your computer and use it in GitHub Desktop.
Gets the shipping cost for the product from the assigned shipping class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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