Skip to content

Instantly share code, notes, and snippets.

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 barryhughes/0081fc4e62842693fee998dcc9f27cc5 to your computer and use it in GitHub Desktop.
Save barryhughes/0081fc4e62842693fee998dcc9f27cc5 to your computer and use it in GitHub Desktop.
A possible alternative to the initial solution in WooCommerce PR#37847 (relates to this comment: https://github.com/woocommerce/woocommerce/pull/37847#issuecomment-1542940612)
diff --git a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
index 4b140a814f..5719a8a12b 100644
--- a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
+++ b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
@@ -177,8 +177,16 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
// Also handles BW compatibility when slugs were used instead of ids.
$shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
$class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
-
- if ( '' === $class_cost_string ) {
+ $no_class_cost = 0 === strlen( $class_cost_string );
+
+ if ( $no_class_cost && 'class' === $this->type ) {
+ // If we are calculating per each individual shipping class, but one of the products belongs to a class
+ // for which a cost has not been defined, then we cannot supply a shipping rate.
+ $has_costs = false;
+ break;
+ } elseif ( $no_class_cost ) {
+ // Otherwise, if we do not have a class cost but we are intending to use the highest available rate,
+ // we should simply skip this product.
continue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment