Skip to content

Instantly share code, notes, and snippets.

@WooForce
Last active March 31, 2016 12:01
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 WooForce/96d428b1519d5a54efdb to your computer and use it in GitHub Desktop.
Save WooForce/96d428b1519d5a54efdb to your computer and use it in GitHub Desktop.
WooCommerce: To show Shipping Methods when Shipping Class exist
add_filter( 'woocommerce_package_rates', 'show_shippingpro_method_when_shipping_class_product_is_in_cart', 10, 2 );
function show_shippingpro_method_when_shipping_class_product_is_in_cart( $available_shipping_methods, $package ) {
$shipping_class_ids = array(
16);
$shipping_services_to_show = array(
'wf_woocommerce_shipping_pro:Group1Express',
'wf_woocommerce_shipping_pro:group2Standard',
);
$shipping_services_to_show = array_map('strtolower', $shipping_services_to_show);
$shipping_class_exists = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
if( in_array( $values[ 'data' ]->get_shipping_class_id(), $shipping_class_ids ) ) {
$shipping_class_exists = true;
break;
}
}
if(!$shipping_class_exists ) {
foreach ( $shipping_services_to_show as &$value ) {
unset( $available_shipping_methods[$value] );
}
}
return $available_shipping_methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment