Skip to content

Commit

Permalink
Do use default props when merging from label request to make sure add…
Browse files Browse the repository at this point in the history
…itional data, e.g. sender_address for return labels is passed correctly to the label. While comparing product dimensions with actual shipment dimensions, make sure to always compare the longest side as length. Use a fallback when auto packing shipments and no packaging is available to prevent errors.
  • Loading branch information
dennisnissle committed Nov 30, 2023
1 parent 4b158bd commit 73fba17
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 79 deletions.
3 changes: 3 additions & 0 deletions includes/wc-gzd-shipment-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ function wc_gzd_split_shipment_street( $street_str ) {
return $return;
}

/**
* @return ShippingProvider\Auto[]|ShippingProvider\Simple[]
*/
function wc_gzd_get_shipping_providers() {
return ShippingProvider\Helper::instance()->get_shipping_providers();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ public static function add_shipment() {
}

if ( is_wp_error( $shipments ) || empty( $shipments ) ) {
if ( is_wp_error( $shipments ) ) {
$response_error['message'] = $shipments->get_error_message();
}

wp_send_json( $response_error );
}

Expand Down
81 changes: 49 additions & 32 deletions src/Automation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Vendidero\Germanized\Shipments;

use Vendidero\Germanized\Shipments\Admin\MetaBox;
use Vendidero\Germanized\Shipments\Admin\Settings;
use Vendidero\Germanized\Shipments\Packing\Helper;
use WC_Order;

Expand Down Expand Up @@ -240,46 +240,63 @@ public static function create_shipments( $order ) {
$items = $order_shipment->get_items_to_pack_left_for_shipping();
$packed_boxes = Helper::pack( $items, $packaging_boxes, 'order' );

foreach ( $packed_boxes as $box ) {
$packaging = $box->getBox();
$items = $box->getItems();
$shipment_items = array();

foreach ( $items as $item ) {
$order_item = $item->getItem();

if ( ! isset( $shipment_items[ $order_item->get_id() ] ) ) {
$shipment_items[ $order_item->get_id() ] = 1;
} else {
$shipment_items[ $order_item->get_id() ]++;
}
}

$shipment = wc_gzd_create_shipment(
$order_shipment,
array(
'items' => $shipment_items,
'props' => array(
'packaging_id' => $packaging->get_id(),
'status' => $shipment_status,
),
)
);
if ( empty( $packaging_boxes ) && 0 === count( $packed_boxes ) ) {
$shipment = wc_gzd_create_shipment( $order_shipment, array( 'props' => array( 'status' => $shipment_status ) ) );

if ( ! is_wp_error( $shipment ) ) {
$order_shipment->add_shipment( $shipment );

$shipments_created[ $shipment->get_id() ] = $shipment;
} else {
foreach ( $shipments_created as $id => $shipment_created ) {
$shipment_created->delete( true );
$order_shipment->remove_shipment( $id );
}

foreach ( $shipment->get_error_messages() as $code => $message ) {
$errors->add( $code, $message );
}
}
} else {
if ( 0 === count( $packed_boxes ) ) {
$errors->add( 404, sprintf( _x( 'Seems like none of your <a href="%1$s">packaging options</a> is available for this order.', 'shipments', 'woocommerce-germanized-shipments' ), Settings::get_settings_url( 'packaging' ) ) );
} else {
foreach ( $packed_boxes as $box ) {
$packaging = $box->getBox();
$items = $box->getItems();
$shipment_items = array();

foreach ( $items as $item ) {
$order_item = $item->getItem();

if ( ! isset( $shipment_items[ $order_item->get_id() ] ) ) {
$shipment_items[ $order_item->get_id() ] = 1;
} else {
$shipment_items[ $order_item->get_id() ]++;
}
}

$shipment = wc_gzd_create_shipment(
$order_shipment,
array(
'items' => $shipment_items,
'props' => array(
'packaging_id' => $packaging->get_id(),
'status' => $shipment_status,
),
)
);

if ( ! is_wp_error( $shipment ) ) {
$order_shipment->add_shipment( $shipment );

$shipments_created[ $shipment->get_id() ] = $shipment;
} else {
foreach ( $shipments_created as $id => $shipment_created ) {
$shipment_created->delete( true );
$order_shipment->remove_shipment( $id );
}

foreach ( $shipment->get_error_messages() as $code => $message ) {
$errors->add( $code, $message );
}
}
}
}
}
} else {
$shipment = wc_gzd_create_shipment( $order_shipment, array( 'props' => array( 'status' => $shipment_status ) ) );
Expand Down
4 changes: 2 additions & 2 deletions src/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ private static function maybe_create_packaging() {
),
array(
'description' => _x( 'Letter C4', 'shipments', 'woocommerce-germanized-shipments' ),
'length' => 22.9,
'width' => 32.4,
'length' => 32.4,
'width' => 22.9,
'height' => 2,
'weight' => 0.01,
'max_content_weight' => 1,
Expand Down
62 changes: 57 additions & 5 deletions src/Labels/ReturnLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ protected function get_sender_address_prop( $prop, $context = 'view' ) {
return $value;
}

/**
* Returns the sender address first line.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_sender_address_1( $context = 'view' ) {
return $this->get_sender_address_prop( 'address_1', $context );
}

public function get_sender_address_2( $context = 'view' ) {
return $this->get_sender_address_prop( 'address_2', $context );
}
Expand All @@ -66,27 +76,69 @@ public function get_sender_address_addition() {
}

public function get_sender_street( $context = 'view' ) {
return $this->get_sender_address_prop( 'street', $context );
$sender_address = $this->get_sender_address();

if ( array_key_exists( 'street', $sender_address ) ) {
return $this->get_sender_address_prop( 'street', $context );
}

$split = wc_gzd_split_shipment_street( $this->{'get_sender_address_1'}() );

return $split['street'];
}

public function get_sender_street_number( $context = 'view' ) {
return $this->get_sender_address_prop( 'street_number', $context );
$sender_address = $this->get_sender_address();

if ( array_key_exists( 'street_number', $sender_address ) ) {
return $this->get_sender_address_prop( 'street_number', $context );
}

$split = wc_gzd_split_shipment_street( $this->{'get_sender_address_1'}() );

return $split['number'];
}

public function get_sender_street_addition( $context = 'view' ) {
return $this->get_sender_address_prop( 'street_addition', $context );
$sender_address = $this->get_sender_address();

if ( array_key_exists( 'street_addition', $sender_address ) ) {
return $this->get_sender_address_prop( 'street_addition', $context );
}

$split = wc_gzd_split_shipment_street( $this->{'get_sender_address_1'}() );

return $split['addition'];
}

public function get_sender_company( $context = 'view' ) {
return $this->get_sender_address_prop( 'company', $context );
}

public function get_sender_name( $context = 'view' ) {
return $this->get_sender_address_prop( 'name', $context );
$sender_address = $this->get_sender_address();

if ( array_key_exists( 'name', $sender_address ) ) {
return $this->get_sender_address_prop( 'name', $context );
}

return '';
}

public function get_sender_first_name( $context = 'view' ) {
return $this->get_sender_address_prop( 'first_name', $context );
}

public function get_sender_last_name( $context = 'view' ) {
return $this->get_sender_address_prop( 'last_name', $context );
}

public function get_sender_formatted_full_name() {
return sprintf( _x( '%1$s', 'shipments full name', 'woocommerce-germanized-shipments' ), $this->get_sender_name() ); // phpcs:ignore WordPress.WP.I18n.NoEmptyStrings
if ( empty( $this->get_sender_first_name() ) ) {
return $this->get_sender_name();
}

return sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce-germanized-shipments' ), $this->get_sender_first_name(), $this->get_sender_last_name() );
}

public function get_sender_postcode( $context = 'view' ) {
Expand Down
72 changes: 38 additions & 34 deletions src/ShippingProvider/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ protected function get_default_label_props( $shipment ) {
}
}

if ( 'return' === $shipment->get_type() ) {
$default['sender_address'] = $shipment->get_sender_address();
}

return $default;
}

Expand All @@ -737,21 +741,11 @@ public function create_label( $shipment, $props = false ) {
if ( false === $props ) {
$props = $this->get_default_label_props( $shipment );
} elseif ( is_array( $props ) ) {
$props = wp_parse_args(
$props,
array(
'shipping_provider' => $this->get_name(),
'weight' => wc_gzd_get_shipment_label_weight( $shipment ),
'net_weight' => wc_gzd_get_shipment_label_weight( $shipment, true ),
'shipment_id' => $shipment->get_id(),
'services' => array(),
'product_id' => $this->get_default_label_product( $shipment ),
'print_format' => $this->get_default_label_print_format( $shipment ),
)
);
$default_props = $this->get_default_label_props( $shipment );
$default_props['services'] = array();
$default_props['print_format'] = '';

$dimensions = wc_gzd_get_shipment_label_dimensions( $shipment );
$props = array_merge( $props, $dimensions );
$props = wp_parse_args( $props, $default_props );
}

$props = wp_parse_args(
Expand All @@ -769,26 +763,6 @@ public function create_label( $shipment, $props = false ) {
foreach ( $this->get_services() as $service ) {
$label_field_id = $service->get_label_field_id();

if ( ! $service->supports(
array(
'shipment' => $shipment,
'product' => $props['product_id'],
)
) ) {
$props['services'] = array_diff( $props['services'], array( $service->get_id() ) );

foreach ( $service->get_label_fields( $shipment ) as $setting ) {
if ( array_key_exists( $setting['id'], $props ) ) {
unset( $props[ $setting['id'] ] );
}
}

if ( array_key_exists( $label_field_id, $props ) ) {
unset( $props[ $label_field_id ] );
continue;
}
}

/**
* Should only be the case for manual requests, e.g. form submits.
*/
Expand All @@ -815,6 +789,36 @@ public function create_label( $shipment, $props = false ) {
}
}

/**
* Remove services + service meta in case the service is not available or not booked.
*/
if ( ! $service->supports(
array(
'shipment' => $shipment,
'product' => $props['product_id'],
)
) || ! in_array( $service->get_id(), $props['services'], true ) ) {
$props['services'] = array_diff( $props['services'], array( $service->get_id() ) );

foreach ( $service->get_label_fields( $shipment ) as $setting ) {
$setting_id = $setting['id'];

if ( strstr( $setting_id, '[' ) ) {
$setting_parts = explode( '[', $setting_id );
$setting_id = $setting_parts[0];
}

if ( array_key_exists( $setting_id, $props ) ) {
unset( $props[ $setting_id ] );
}
}

if ( array_key_exists( $label_field_id, $props ) ) {
unset( $props[ $label_field_id ] );
continue;
}
}

if ( in_array( $service->get_id(), $props['services'], true ) ) {
$valid = $service->validate_label_request( $props, $shipment );

Expand Down
Loading

0 comments on commit 73fba17

Please sign in to comment.