Skip to content

Commit

Permalink
Added preview shipment button to table. Use item_parent_id as "real" …
Browse files Browse the repository at this point in the history
…parent id, as the parent_id attribute already exists to link return items to their shipment's parent. By default, do not show child items within customer context (my account, emails).
  • Loading branch information
dennisnissle committed Jul 2, 2024
1 parent 1592b30 commit 932b012
Show file tree
Hide file tree
Showing 27 changed files with 653 additions and 96 deletions.
107 changes: 107 additions & 0 deletions assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,66 @@ a.woocommerce-gzd-input-toggle-trigger {
}
}

.wc-gzd-modal-preview-shipment {
.wc-backbone-modal-header {
display: flex;
justify-content: space-between;

.shipment-status {
margin-right: 54px;
}
}

h2 {
margin-top: 0;
}

.wc-gzd-preview-shipment-item-list {
margin-left: -1.5em;
width: calc(100% + 3em);
margin-top: 1.5em;
}

.wc-gzd-preview-shipment-items {
width: 100%;
margin: 0;
border-collapse: collapse;

th, td {
padding: 1em 1.5em;
border: 0;
border-bottom: 1px solid #eee;
margin: 0;
background: 0 0;
box-shadow: none;
text-align: right;
vertical-align: top;

&:first-child {
text-align: left;
}
}

tr {
th {
border-color: #ccc;
}

&:last-child {
td {
border-bottom: none;
}
}

&.shipment-item-is-child {
td:first-child {
padding-left: 2.5em;
}
}
}
}
}

.wc-gzd-admin-shipment-modal {
.wc-backbone-modal-content {
min-width: 600px;
Expand Down Expand Up @@ -1033,6 +1093,41 @@ a.button.wc-gzd-shipment-action-button {
td.column-title {
font-weight: bold;

.shipment-preview {
float: right;
width: 16px;
padding: 20px 4px 4px 4px;
height: 0;
overflow: hidden;
position: relative;
border: 2px solid transparent;
border-radius: 4px;

&:hover {
border: 2px solid var(--wp-admin-theme-color, #00a0d2);
}

&::before {
font-family: WooCommerce;
speak: never;
font-weight: 400;
font-variant: normal;
text-transform: none;
margin: 0;
text-indent: 0;
position: absolute;
left: 0;
width: 100%;
height: 100%;
text-align: center;
content: "\e010";
line-height: 16px;
font-size: 14px;
vertical-align: middle;
top: 4px;
}
}

.shipment-title-meta {
margin-top: 0;
font-weight: normal;
Expand Down Expand Up @@ -1108,6 +1203,12 @@ a.button.wc-gzd-shipment-action-button {
border-bottom: none !important;
}
}

&.shipment-item-is-child {
td.wc-gzd-shipment-item-column-name {
padding-left: .5em;
}
}
}

th, td {
Expand Down Expand Up @@ -1473,6 +1574,12 @@ a.button.wc-gzd-shipment-action-button {
padding: .5em;
border-bottom: 1px solid #dfdfdf;

&.shipment-item-is-child {
.shipment-item-name {
padding-left: 2.5em;
}
}

&:last-child {
border-bottom: none;
}
Expand Down
10 changes: 9 additions & 1 deletion assets/js/static/admin-shipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,15 @@ window.germanized.admin = window.germanized.admin || {};

if ( $item.length > 0 ) {
$item.slideUp( 150, function() {
$( this ).remove();
if ( $item.hasClass( 'shipment-item-is-parent' ) ) {
$children = $item.parents( '.shipment-item-list' ).find( '.shipment-item-parent-' + data['item_id'] );

$children.each( function( $child ) {
$( this ) .remove();
} );
}

$item.remove();
});
}

Expand Down
34 changes: 18 additions & 16 deletions includes/admin/views/html-order-shipment-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,38 @@
defined( 'ABSPATH' ) || exit;
?>

<div class="shipment-item" data-id="<?php echo esc_attr( $item->get_id() ); ?>">
<div class="shipment-item <?php echo esc_attr( $item->get_item_parent_id() > 0 ? 'shipment-item-is-child shipment-item-parent-' . $item->get_item_parent_id() : '' ); ?> <?php echo esc_attr( $item->has_children() ? 'shipment-item-is-parent' : '' ); ?>" data-id="<?php echo esc_attr( $item->get_id() ); ?>" id="shipment-item-<?php echo esc_attr( $item->get_id() ); ?>">
<div class="columns">
<?php foreach ( Admin::get_admin_shipment_item_columns( $shipment ) as $column_name => $column ) : ?>
<div class="column col-<?php echo esc_attr( $column['size'] ); ?> shipment-item-<?php echo esc_attr( $column_name ); ?>">
<?php if ( 'name' === $column_name ) : ?>
<?php echo wp_kses_post( $item->get_name() ); ?> <?php echo ( $item->get_sku() ? '<small>(' . esc_html( $item->get_sku() ) . ')</small>' : '' ); ?>
<?php elseif ( 'return_reason' === $column_name ) : ?>
<select class="item-return-reason-code" id="shipment-item-return-reason-code-<?php echo esc_attr( $item->get_id() ); ?>" name="shipment_item[<?php echo esc_attr( $shipment->get_id() ); ?>][return_reason_code][<?php echo esc_attr( $item->get_id() ); ?>]">
<select class="item-return-reason-code <?php echo ( $item->is_readonly() ? 'disabled' : '' ); ?>" id="shipment-item-return-reason-code-<?php echo esc_attr( $item->get_id() ); ?>" name="shipment_item[<?php echo esc_attr( $shipment->get_id() ); ?>][return_reason_code][<?php echo esc_attr( $item->get_id() ); ?>]" <?php echo ( $item->is_readonly() ? 'disabled' : '' ); ?>>
<option value=""><?php echo esc_html_x( 'None', 'shipments return reason', 'woocommerce-germanized-shipments' ); ?></option>
<?php foreach ( wc_gzd_get_return_shipment_reasons( $item->get_order_item() ) as $reason ) : ?>
<option value="<?php echo esc_attr( $reason->get_code() ); ?>" <?php selected( $reason->get_code(), $item->get_return_reason_code() ); ?>><?php echo esc_html( $reason->get_reason() ); ?></option>
<?php endforeach; ?>
</select>
<?php elseif ( 'quantity' === $column_name ) : ?>
<input type="number" size="6" step="1" name="shipment_item[<?php echo esc_attr( $shipment->get_id() ); ?>][quantity][<?php echo esc_attr( $item->get_id() ); ?>]" class="item-quantity" data-original-value="<?php echo esc_attr( $item->get_quantity() ); ?>" value="<?php echo esc_attr( $item->get_quantity() ); ?>" />
<input type="number" size="6" step="1" name="shipment_item[<?php echo esc_attr( $shipment->get_id() ); ?>][quantity][<?php echo esc_attr( $item->get_id() ); ?>]" class="item-quantity" <?php echo ( $item->is_readonly() ? 'readonly' : '' ); ?> data-original-value="<?php echo esc_attr( $item->get_quantity() ); ?>" value="<?php echo esc_attr( $item->get_quantity() ); ?>" />
<?php elseif ( 'action' === $column_name ) : ?>
<?php
echo wc_gzd_render_shipment_action_buttons( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
array(
'delete' => array(
'classes' => 'remove-shipment-item',
'action' => 'delete',
'name' => _x( 'Delete item', 'shipments', 'woocommerce-germanized-shipments' ),
'custom_attributes' => array(
'data-delete' => $item->get_id(),
<?php if ( ! $item->is_readonly() ) : ?>
<?php
echo wc_gzd_render_shipment_action_buttons( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
array(
'delete' => array(
'classes' => 'remove-shipment-item',
'action' => 'delete',
'name' => _x( 'Delete item', 'shipments', 'woocommerce-germanized-shipments' ),
'custom_attributes' => array(
'data-delete' => $item->get_id(),
),
),
),
)
);
?>
)
);
?>
<?php endif; ?>
<?php endif; ?>

<?php
Expand Down
117 changes: 117 additions & 0 deletions includes/admin/views/html-preview-shipment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Preview shipment HTML.
*/
defined( 'ABSPATH' ) || exit;

$order_shipment = $shipment->get_order_shipment();
$total_shipments = $order_shipment ? $order_shipment->get_shipment_count( $shipment->get_type() ) : 1;
$position_number = $order_shipment ? $order_shipment->get_shipment_position_number( $shipment ) : 1;
?>
<div class="wc-gzd-preview-shipment">
<p class="shipment-summary">
<?php echo sprintf( esc_html_x( '%1$s %2$d/%3$d to ', 'shipments', 'woocommerce-germanized-shipments' ), esc_html( wc_gzd_get_shipment_label_title( $shipment->get_type() ) ), esc_html( $position_number ), esc_html( $total_shipments ) ); ?>
<?php if ( ( $s_order = $shipment->get_order() ) && is_callable( array( $s_order, 'get_edit_order_url' ) ) ) : ?>
<a href="<?php echo esc_url( $s_order->get_edit_order_url() ); ?>"><?php echo sprintf( esc_html_x( '#%1$s', 'shipments', 'woocommerce-germanized-shipments' ), esc_html( $s_order->get_order_number() ) ); ?></a>
<?php else : ?>
<?php echo sprintf( esc_html_x( '#%1$s', 'shipments', 'woocommerce-germanized-shipments' ), esc_html( $shipment->get_order_number() ) ); ?>
<?php endif; ?>
</p>

<div class="wc-gzd-preview-shipment-data columns">
<div class="column col-6">
<?php if ( 'return' === $shipment->get_type() ) : ?>
<h2><?php echo esc_html_x( 'Return from', 'shipments', 'woocommerce-germanized-shipments' ); ?></h2>
<address><?php echo wp_kses_post( $shipment->get_formatted_sender_address() ); ?></address>
<?php else : ?>
<h2><?php echo esc_html_x( 'Shipping to', 'shipments', 'woocommerce-germanized-shipments' ); ?></h2>
<address><?php echo wp_kses_post( $shipment->get_formatted_address() ); ?></address>
<?php endif; ?>
<?php
$provider = $shipment->get_shipping_provider();
$tracking_id = $shipment->get_tracking_id();

if ( ! empty( $provider ) && ! empty( $tracking_id ) ) :
?>
<p>
<span class="shipment-shipping-provider"><?php echo sprintf( esc_html_x( 'via %s', 'shipments', 'woocommerce-germanized-shipments' ), wp_kses_post( wc_gzd_get_shipping_provider_title( $provider ) ) ); ?></span>

<?php if ( $shipment->has_tracking() && ( $tracking_url = $shipment->get_tracking_url() ) ) : ?>
<a class="shipment-tracking-id" target="_blank" href="<?php echo esc_url( $tracking_url ); ?>"><?php echo esc_html( $tracking_id ); ?></a>
<?php else : ?>
<span class="shipment-tracking-id"><?php echo esc_html( $tracking_id ); ?></span>
<?php endif; ?>
</p>
<?php endif; ?>

<?php do_action( 'woocommerce_gzd_shipments_preview_after_column_shipping_to', $shipment ); ?>
</div>
<div class="column col-6">
<h2><?php echo esc_html_x( 'Package', 'shipments', 'woocommerce-germanized-shipments' ); ?></h2>
<p>
<?php if ( $packaging = $shipment->get_packaging() ) : ?>
<?php echo wp_kses_post( $packaging->get_description() ); ?><br/>
<?php endif; ?>

<?php echo wp_kses_post( $shipment->get_formatted_dimensions() ); ?>, <?php echo wp_kses_post( wc_gzd_format_shipment_weight( $shipment->get_total_weight(), $shipment->get_weight_unit() ) ); ?>
</p>

<?php do_action( 'woocommerce_gzd_shipments_preview_after_column_package', $shipment ); ?>
</div>

<?php do_action( 'woocommerce_gzd_shipments_preview_after_columns', $shipment ); ?>
</div>

<?php do_action( 'woocommerce_gzd_shipments_preview_before_item_list', $shipment ); ?>

<div class="wc-gzd-preview-shipment-item-list">
<table class="wc-gzd-preview-shipment-items">
<?php
$preview_table_columns = apply_filters(
'woocommerce_gzd_shipments_preview_shipment_columns',
array(
'name' => _x( 'Position', 'shipments', 'woocommerce-germanized-shipments' ),
'quantity' => _x( 'Quantity', 'shipments', 'woocommerce-germanized-shipments' ),
'dimensions' => _x( 'Dimensions', 'shipments', 'woocommerce-germanized-shipments' ),
'weight' => _x( 'Weight', 'shipments', 'woocommerce-germanized-shipments' ),
),
$shipment->get_type()
);
?>
<thead>
<tr>
<?php foreach ( $preview_table_columns as $column_name => $column_title ) : ?>
<th class="wc-gzd-preview-shipment-item-column wc-gzd-preview-shipment-item-column-<?php echo esc_attr( $column_name ); ?>"><?php echo esc_html( $column_title ); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $shipment->get_items() as $item ) : ?>
<tr class="wc-gzd-shipment-item-preview wc-gzd-shipment-item-preview-<?php echo esc_attr( $item->get_id() ); ?> <?php echo esc_attr( $item->get_item_parent_id() > 0 ? 'shipment-item-is-child shipment-item-parent-' . $item->get_item_parent_id() : '' ); ?> <?php echo esc_attr( $item->has_children() ? 'shipment-item-is-parent' : '' ); ?>">
<?php foreach ( $preview_table_columns as $column_name => $column_title ) : ?>
<td class="wc-gzd-preview-shipment-item-column wc-gzd-preview-shipment-item-column-<?php echo esc_attr( $column_name ); ?>">
<?php if ( 'name' === $column_name ) : ?>
<?php if ( $product = $item->get_product() ) : ?>
<a href="<?php echo esc_url( get_edit_post_link( $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id() ) ); ?>"><?php echo wp_kses_post( $item->get_name() ); ?></a>
<?php else : ?>
<?php echo wp_kses_post( $item->get_name() ); ?>
<?php endif; ?>

<?php echo ( $item->get_sku() ? '<br/><small>' . esc_html_x( 'SKU:', 'shipments', 'woocommerce-germanized-shipments' ) . ' ' . esc_html( $item->get_sku() ) . '</small>' : '' ); ?>
<?php elseif ( 'quantity' === $column_name ) : ?>
<?php echo esc_html( $item->get_quantity() ); ?>x
<?php elseif ( 'dimensions' === $column_name ) : ?>
<?php echo wp_kses_post( wc_gzd_format_shipment_dimensions( $item->get_dimensions(), $shipment->get_dimension_unit() ) ); ?>
<?php elseif ( 'weight' === $column_name ) : ?>
<?php echo wp_kses_post( wc_gzd_format_shipment_weight( $item->get_weight(), $shipment->get_weight_unit() ) ); ?>
<?php endif; ?>

<?php do_action( "woocommerce_gzd_shipments_preview_shipment_column_{$column_name}", $item, $shipment ); ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function trigger( $order_id ) {
* @return string
*/
public function get_additional_content() {
if ( is_callable( 'parent::get_additional_content' ) ) {
if ( method_exists( get_parent_class( $this ), 'get_additional_content' ) ) {
return parent::get_additional_content();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function trigger( $shipment_id ) {
* @return string
*/
public function get_additional_content() {
if ( is_callable( 'parent::get_additional_content' ) ) {
if ( method_exists( get_parent_class( $this ), 'get_additional_content' ) ) {
return parent::get_additional_content();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function trigger( $shipment_id, $is_confirmation = false ) {
* @return string
*/
public function get_additional_content() {
if ( is_callable( 'parent::get_additional_content' ) ) {
if ( method_exists( get_parent_class( $this ), 'get_additional_content' ) ) {
return parent::get_additional_content();
}

Expand Down
2 changes: 1 addition & 1 deletion includes/emails/class-wc-gzd-email-customer-shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function trigger( $shipment_id ) {
* @return string
*/
public function get_additional_content() {
if ( is_callable( 'parent::get_additional_content' ) ) {
if ( method_exists( get_parent_class( $this ), 'get_additional_content' ) ) {
return parent::get_additional_content();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function trigger( $shipment_id ) {
* @return string
*/
public function get_additional_content() {
if ( is_callable( 'parent::get_additional_content' ) ) {
if ( method_exists( get_parent_class( $this ), 'get_additional_content' ) ) {
return parent::get_additional_content();
}

Expand Down
3 changes: 0 additions & 3 deletions includes/wc-gzd-shipment-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,7 @@ function wc_gzd_shipment_wp_error_has_errors( $error ) {
* @return ShipmentReturnItem|WP_Error
*/
function wc_gzd_create_return_shipment_item( $shipment, $shipment_item, $args = array() ) {

try {

if ( ! $shipment_item || ! is_a( $shipment_item, '\Vendidero\Germanized\Shipments\ShipmentItem' ) ) {
throw new Exception( _x( 'Invalid shipment item', 'shipments', 'woocommerce-germanized-shipments' ) );
}
Expand All @@ -593,7 +591,6 @@ function wc_gzd_create_return_shipment_item( $shipment, $shipment_item, $args =
$item->set_shipment( $shipment );
$item->sync( $args );
$item->save();

} catch ( Exception $e ) {
return new WP_Error( 'error', $e->getMessage() );
}
Expand Down
Loading

0 comments on commit 932b012

Please sign in to comment.