diff --git a/src/Package.php b/src/Package.php index 7fbc16c2..187c2229 100644 --- a/src/Package.php +++ b/src/Package.php @@ -451,6 +451,14 @@ protected static function generate_key() { return md5( serialize( $key ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize } + public static function is_shipping_debug_mode() { + return apply_filters( 'woocommerce_gzd_shipments_is_shipping_debug_mode', 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ); + } + + public static function is_constant_defined( $constant ) { + return class_exists( 'Automattic\Jetpack\Constants' ) ? \Automattic\Jetpack\Constants::is_defined( $constant ) : defined( $constant ); + } + public static function log( $message, $type = 'info', $source = '' ) { $enable_logging = defined( 'WP_DEBUG' ) && WP_DEBUG ? true : false; diff --git a/src/Packaging.php b/src/Packaging.php index 10227452..8c506c1d 100644 --- a/src/Packaging.php +++ b/src/Packaging.php @@ -279,6 +279,12 @@ public function has_inner_dimensions() { return ! empty( $this->get_inner_width( 'edit' ) ) || ! empty( $this->get_inner_length( 'edit' ) ) || ! empty( $this->get_inner_height( 'edit' ) ); } + public function has_shipping_class_restrictions() { + $classes = $this->get_available_shipping_classes( 'edit' ); + + return ! empty( $classes ) ? true : false; + } + /** * Returns the available shipping provider names. * @@ -315,7 +321,9 @@ public function supports_shipping_class( $shipping_class ) { $classes = $this->get_available_shipping_classes( 'edit' ); $supports = false; - if ( empty( $classes ) || in_array( $shipping_class, $classes, true ) ) { + if ( empty( $shipping_class ) && empty( $classes ) ) { + $supports = true; + } elseif ( empty( $classes ) || in_array( $shipping_class, $classes, true ) ) { $supports = true; } diff --git a/src/Packing/Item.php b/src/Packing/Item.php index 3a395ef0..d1d65ffd 100644 --- a/src/Packing/Item.php +++ b/src/Packing/Item.php @@ -60,7 +60,7 @@ public function canBePacked( $box, $already_packed_items, int $proposed_x, int $ if ( $product = $this->get_product() ) { $shipping_class = $product->get_shipping_class_id(); - if ( ! empty( $shipping_class ) ) { + if ( $box->get_packaging()->has_shipping_class_restrictions() ) { if ( ! $box->get_packaging()->supports_shipping_class( $shipping_class ) ) { $fits = false; } diff --git a/src/ShippingMethod/ShippingMethod.php b/src/ShippingMethod/ShippingMethod.php index 269b4625..7a0f49ff 100644 --- a/src/ShippingMethod/ShippingMethod.php +++ b/src/ShippingMethod/ShippingMethod.php @@ -541,7 +541,8 @@ public function calculate_shipping( $package = array() ) { if ( isset( $package['items_to_pack'], $package['package_data'] ) ) { $cart_data = (array) $package['package_data']; - $boxes = \DVDoug\BoxPacker\BoxList::fromArray( $this->get_available_packaging_boxes( $cart_data ) ); + $available_boxes = $this->get_available_packaging_boxes( $cart_data ); + $boxes = \DVDoug\BoxPacker\BoxList::fromArray( $available_boxes ); $cost_calculation_mode = $this->get_multiple_shipments_cost_calculation_mode(); $multiple_rules_calculation_mode = $this->get_multiple_rules_cost_calculation_mode(); @@ -712,23 +713,80 @@ public function calculate_shipping( $package = array() ) { $packaging_ids = array_unique( array_merge( $packaging_ids, array( $packaging->get_id() ) ) ); } } - } - if ( ! empty( $applied_rules ) ) { - $this->add_rate( - array( - 'cost' => $total_cost, - 'label' => $this->get_rate_label( $total_cost ), - 'package' => $package, - 'meta_data' => array( - '_packed_items' => $total_packed_items, - '_packed_item_map' => $total_packed_item_map, - '_packaging_ids' => $packaging_ids, - '_rule_ids' => $rule_ids, - '_packages' => $applied_rules, - ), - ) - ); + if ( ! empty( $applied_rules ) ) { + if ( Package::is_shipping_debug_mode() ) { + $notices = array(); + $available_box_list = array(); + + foreach ( $available_boxes as $box ) { + $available_box_list[] = $box->get_packaging()->get_title(); + } + + $notices[] = sprintf( _x( '### Debug information for %1$s:', 'shipments', 'woocommerce-germanized-shipments' ), $this->get_title() ); + $notices[] = sprintf( _x( 'Available packaging options: %1$s', 'shipments', 'woocommerce-germanized-shipments' ), implode( ', ', $available_box_list ) ); + + $package_count = 0; + + foreach ( $applied_rules as $applied_rule ) { + if ( $packaging = wc_gzd_get_packaging( $applied_rule['packaging_id'] ) ) { + $package_count++; + + $notices[] = sprintf( _x( '## Package %1$d/%2$d: %3$s: ', 'shipments', 'woocommerce-germanized-shipments' ), $package_count, count( $applied_rules ), $packaging->get_title() ); + $rule_match_count = 0; + + foreach ( $applied_rule['rules'] as $rule ) { + if ( $the_rule = $this->get_shipping_rule_by_id( $rule, $applied_rule['packaging_id'] ) ) { + $rule_match_count++; + + $notices[] = sprintf( _x( 'Rule %1$d: %2$s', 'shipments', 'woocommerce-germanized-shipments' ), $rule, wc_price( $the_rule['costs'] ) ); + } + } + + foreach ( $applied_rule['items'] as $item_product_key => $quantity ) { + $product_ids = explode( '_', $item_product_key ); + $product_title = $product_ids[0]; + + if ( $product = wc_get_product( $product_ids[1] ) ) { + $product_title = $product->get_title(); + } + + $product_desc = ! empty( $product_ids[0] ) ? sprintf( _x( '%1$s (Parent: %2$s)', 'shipments', 'woocommerce-germanized-shipments' ), $product_title, $product_ids[0] ) : $product_title; + $notices[] = sprintf( _x( '%1$s x %2$s', 'shipments', 'woocommerce-germanized-shipments' ), $quantity, $product_desc ); + } + } + } + + $notices[] = sprintf( _x( '## Total: %1$s (%2$s, %3$s)', 'shipments', 'woocommerce-germanized-shipments' ), wc_price( $total_cost ), $cost_calculation_mode, $multiple_rules_calculation_mode ); + + if ( ! Package::is_constant_defined( 'WOOCOMMERCE_CHECKOUT' ) && ! Package::is_constant_defined( 'WC_DOING_AJAX' ) ) { + $the_notice = ''; + + foreach ( $notices as $notice ) { + $the_notice .= $notice . '
'; + } + + if ( ! wc_has_notice( $the_notice ) ) { + wc_add_notice( $the_notice ); + } + } + } + + $this->add_rate( + array( + 'cost' => $total_cost, + 'label' => $this->get_rate_label( $total_cost ), + 'package' => $package, + 'meta_data' => array( + '_packed_items' => $total_packed_items, + '_packed_item_map' => $total_packed_item_map, + '_packaging_ids' => $packaging_ids, + '_rule_ids' => $rule_ids, + '_packages' => $applied_rules, + ), + ) + ); + } } } @@ -1097,7 +1155,7 @@ protected function generate_shipping_rules_html( $option_name, $option ) { - +