Skip to content

Commit

Permalink
Added shipping debug mode output. Always respect shipping class restr…
Browse files Browse the repository at this point in the history
…ictions for packaging options in case set.
  • Loading branch information
dennisnissle committed Mar 25, 2024
1 parent aa83203 commit bd8b4d4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 9 additions & 1 deletion src/Packaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Packing/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
94 changes: 76 additions & 18 deletions src/ShippingMethod/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 . '<br/>';
}

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,
),
)
);
}
}
}

Expand Down Expand Up @@ -1097,7 +1155,7 @@ protected function generate_shipping_rules_html( $option_name, $option ) {
</div>
</td>
<td class="cb">
<input class="cb" name="<?php echo esc_attr( $field_key ); ?>[cb][{{ data.rule_id }}]" type="checkbox" value="{{ data.rule_id }}" data-attribute="cb" />
<input class="cb" name="<?php echo esc_attr( $field_key ); ?>[cb][{{ data.rule_id }}]" type="checkbox" value="{{ data.rule_id }}" data-attribute="cb" title="<?php echo esc_attr_x( 'Rule:', 'shipments', 'woocommerce-germanized-shipments' ); ?> {{ data.rule_id }}" />
</td>
<td class="packaging">
<select class="wc-enhanced-select shipping-packaging" name="<?php echo esc_attr( $field_key ); ?>[packaging][{{ data.rule_id }}]" data-attribute="packaging">
Expand Down

0 comments on commit bd8b4d4

Please sign in to comment.