Skip to content

Commit

Permalink
Improve parent/child hierarchy when adding items. Improve assembled b…
Browse files Browse the repository at this point in the history
…undle parent/child hierarchy.
  • Loading branch information
dennisnissle committed Oct 29, 2024
1 parent 56c7a9e commit 56453f3
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 46 deletions.
2 changes: 0 additions & 2 deletions assets/js/static/admin-shipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,6 @@ window.shipments.admin = window.shipments.admin || {};
};

this.onAddItemSuccess = function( data ) {
this.getShipmentContent().find( '.shipment-item-list' ).append( data.new_item );

this.refreshDom();
this.unblockItems();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<tbody id="wc-gzd-return-shipment-items" data-row="">
<?php
foreach ( $order_shipment->get_available_items_for_return() as $item_id => $item_data ) :
foreach ( $order_shipment->get_selectable_items_for_return() as $item_id => $item_data ) :
?>
<tr>
<td><?php echo esc_attr( $item_data['name'] ); ?></td>
Expand Down
16 changes: 9 additions & 7 deletions src/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -1287,14 +1287,14 @@ public static function add_shipment_item_load() {
$items = array();

if ( 'return' === $shipment->get_type() ) {
$items = $order_shipment->get_available_items_for_return(
$items = $order_shipment->get_selectable_items_for_return(
array(
'shipment_id' => $shipment->get_id(),
'disable_duplicates' => true,
)
);
} else {
$items = $order_shipment->get_available_items_for_shipment(
$items = $order_shipment->get_selectable_items_for_shipment(
array(
'shipment_id' => $shipment_id,
'disable_duplicates' => true,
Expand Down Expand Up @@ -1322,9 +1322,8 @@ public static function add_shipment_item_submit() {
);

$response = array(
'success' => true,
'message' => '',
'new_item' => '',
'success' => true,
'message' => '',
);

$shipment_id = absint( $_POST['reference_id'] );
Expand Down Expand Up @@ -1367,10 +1366,13 @@ public static function add_shipment_item_submit() {
}

ob_start();
include Package::get_path() . '/includes/admin/views/html-order-shipment-item.php';
$response['new_item'] = ob_get_clean();
foreach ( $shipment->get_items() as $item ) {
include Package::get_path() . '/includes/admin/views/html-order-shipment-item.php';
}
$html = ob_get_clean();

$response['fragments'] = array(
'#shipment-' . $shipment->get_id() . ' .shipment-item-list:first' => '<div class="shipment-item-list">' . $html . '</div>',
'#shipment-' . $shipment->get_id() . ' .item-count:first' => self::get_item_count_html( $shipment, $order_shipment ),
);

Expand Down
114 changes: 87 additions & 27 deletions src/Compatibility/Bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Vendidero\Germanized\Shipments\Compatibility;

use Vendidero\Germanized\Shipments\Interfaces\Compatibility;
use Vendidero\Germanized\Shipments\Order;
use Vendidero\Germanized\Shipments\Product;
use Vendidero\Germanized\Shipments\Shipment;
use Vendidero\Germanized\Shipments\ShipmentItem;

defined( 'ABSPATH' ) || exit;

Expand All @@ -26,10 +28,79 @@ function () {

add_filter( 'woocommerce_gzd_shipments_order_item_product', array( __CLASS__, 'get_product_from_item' ), 10, 2 );
add_filter( 'woocommerce_gzd_shipments_cart_item', array( __CLASS__, 'adjust_cart_item' ), 10, 2 );
add_action( 'woocommerce_gzd_shipment_items_synced', array( __CLASS__, 'apply_bundle_hierarchy' ), 10, 3 );
add_filter( 'woocommerce_gzd_shipment_order_selectable_items_for_shipment', array( __CLASS__, 'filter_bundle_children' ), 10, 3 );
add_action( 'woocommerce_gzd_shipment_added_item', array( __CLASS__, 'on_added_shipment_item' ), 10, 2 );
add_filter( 'woocommerce_gzd_shipment_order_item_quantity_left_for_shipping', array( __CLASS__, 'maybe_remove_children' ), 10, 2 );
}

/**
* @param ShipmentItem $item
* @param Shipment $shipment
*
* @return void
*/
public static function on_added_shipment_item( $item, $shipment ) {
if ( $order_item = $item->get_order_item() ) {
if ( $shipment_order = $shipment->get_order_shipment() ) {
$order = $shipment_order->get_order();

if ( self::order_item_is_assembled_bundle( $order_item, $shipment_order ) ) {
$available = $shipment_order->get_available_items_for_shipment(
array(
'shipment_id' => $shipment->get_id(),
'disable_duplicates' => true,
)
);

$children_to_add = array();

foreach ( $available as $item_id => $item_data ) {
if ( ! $child_order_item = $order->get_item( $item_id ) ) {
continue;
}

if ( wc_pb_is_bundled_order_item( $child_order_item ) ) {
$container_id = wc_pb_get_bundled_order_item_container( $child_order_item, $order, true );

if ( $container_id === $order_item->get_id() ) {
$children_to_add[ $item_id ] = $item_data['max_quantity'];

$props = array(
'quantity' => $item_data['max_quantity'],
'parent' => $item,
);

if ( $child_item = wc_gzd_create_shipment_item( $shipment, $child_order_item, $props ) ) {
$shipment->add_item( $child_item );
}
}
}
}
}
}
}
}

/**
* @param $order_item
* @param $order
*
* @return void
*/
protected static function order_item_is_assembled_bundle( $order_item, $shipment_order ) {
$is_assembled = false;

if ( wc_pb_is_bundle_container_order_item( $order_item ) ) {
if ( $product = $shipment_order->get_order_item_product( $order_item ) ) {
if ( ! $product->is_virtual() && apply_filters( 'woocommerce_gzd_shipments_force_bundle_item_container', true, $order_item, $shipment_order ) ) {
$is_assembled = true;
}
}
}

return $is_assembled;
}

/**
* @param integer $quantity_left
* @param \WC_Order_Item $order_item
Expand All @@ -47,41 +118,30 @@ public static function maybe_remove_children( $quantity_left, $order_item ) {
}

/**
* Add parent bundle id to the child bundles.
*
* @param Shipment $shipment
* @param \WC_Order_Item[] $items
* @param array $args
* @param Order $shipment_order
*
* @return void
* @return \WC_Order_Item[]
*/
public static function apply_bundle_hierarchy( $shipment ) {
$map = array();
$parents = array();

foreach ( $shipment->get_items() as $item ) {
if ( $order_item = $item->get_order_item() ) {
$map[ $item->get_order_item_id() ] = $item->get_id();
public static function filter_bundle_children( $items, $args, $shipment_order ) {
$order = $shipment_order->get_order();

if ( wc_pb_is_bundled_order_item( $order_item ) ) {
$container_id = wc_pb_get_bundled_order_item_container( $order_item, false, true );
foreach ( $items as $order_item_id => $item ) {
if ( ! $order_item = $order->get_item( $order_item_id ) ) {
continue;
}

if ( ! isset( $parents[ $container_id ] ) ) {
$parents[ $container_id ] = array();
if ( wc_pb_is_bundled_order_item( $order_item ) ) {
if ( $container = wc_pb_get_bundled_order_item_container( $order_item, $order, false ) ) {
if ( self::order_item_is_assembled_bundle( $container, $shipment_order ) ) {
unset( $items[ $order_item_id ] );
}

$parents[ $container_id ][] = $item;
}
}
}

foreach ( $parents as $order_item_id => $shipment_items ) {
if ( array_key_exists( $order_item_id, $map ) ) {
$parent_id = $map[ $order_item_id ];

foreach ( $shipment_items as $shipment_item ) {
$shipment_item->set_item_parent_id( $parent_id );
}
}
}
return $items;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,13 @@ public function get_items_to_pack_left_for_shipping( $legacy_group_by_product_gr
return apply_filters( 'woocommerce_gzd_shipment_order_items_to_pack_left_for_shipping', $items_to_be_packed );
}

public function get_selectable_items_for_shipment( $args = array() ) {
return apply_filters( 'woocommerce_gzd_shipment_order_selectable_items_for_shipment', $this->get_available_items_for_shipment( $args ), $args, $this );
}

/**
* @param bool|Shipment $shipment
* @param array $args
*
* @return array
*/
public function get_available_items_for_shipment( $args = array() ) {
Expand Down Expand Up @@ -921,6 +926,10 @@ public function get_non_returnable_items() {
return $items;
}

public function get_selectable_items_for_return( $args = array() ) {
return apply_filters( 'woocommerce_gzd_shipment_order_selectable_items_for_return', $this->get_available_items_for_return( $args ), $args, $this );
}

/**
* @return array
*/
Expand Down Expand Up @@ -1072,7 +1081,6 @@ public function get_shippable_items() {
* @since 3.0.0
* @package Vendidero/Germanized/Shipments
*/

do_action( 'woocommerce_gzd_shipments_order_after_get_items', $this->get_order() );

return apply_filters( 'woocommerce_gzd_shipment_order_shippable_items', $items, $this->get_order(), $this );
Expand Down
3 changes: 1 addition & 2 deletions src/ReturnShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function sync_items( $args = array() ) {
)
);

$available_items = $order_shipment->get_available_items_for_return(
$available_items = $order_shipment->get_selectable_items_for_return(
array(
'shipment_id' => $this->get_id(),
'exclude_current_shipment' => true,
Expand Down Expand Up @@ -473,7 +473,6 @@ public function sync_items( $args = array() ) {
* @return bool
*/
public function needs_items( $available_items = false ) {

if ( ! $available_items && ( $order_shipment = $this->get_order_shipment() ) ) {
$available_items = array_keys( $order_shipment->get_available_items_for_return() );
}
Expand Down
4 changes: 4 additions & 0 deletions src/SecretBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ public static function supports_auto_insert() {
public static function maybe_insert_missing_key( $encryption_type = '' ) {
$updated = false;

if ( ! self::supports_storing_secrets() ) {
return $updated;
}

if ( ! self::has_valid_encryption_key( $encryption_type ) ) {
$constant = self::get_encryption_key_constant( $encryption_type );
$key_value = self::get_random_encryption_key();
Expand Down
2 changes: 2 additions & 0 deletions src/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,8 @@ public function add_item( $item ) {
$this->reset_content_data();
$this->calculate_totals();
$this->sync_packaging();

do_action( "{$this->get_general_hook_prefix()}added_item", $item, $this );
}

/**
Expand Down
44 changes: 40 additions & 4 deletions src/ShipmentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,13 @@ public function sync( $args = array() ) {
'manufacture_country' => $product ? $product->get_manufacture_country() : '',
'attributes' => $attributes,
'item_parent_id' => $this->get_item_parent_id() ? $this->get_item_parent_id() : 0,
'parent' => null,
)
);

if ( ! is_null( $args['parent'] ) && is_a( $args['parent'], 'Vendidero\Germanized\Shipments\ShipmentItem' ) ) {
$this->set_parent( $args['parent'] );
}
}

$this->set_props( $args );
Expand Down Expand Up @@ -580,6 +585,17 @@ public function set_item_parent_id( $value ) {
$this->set_prop( 'item_parent_id', absint( $value ) );
}

/**
* @param ShipmentItem $item
*
* @return void
*/
public function set_parent( $item ) {
$this->parent = $item;

$this->set_item_parent_id( $item->get_id() );
}

/**
* @param ShipmentItem $item
*
Expand All @@ -605,11 +621,11 @@ public function remove_child( $key ) {
* @return ShipmentItem|ShipmentReturnItem|null
*/
public function get_parent() {
if ( ! $this->get_item_parent_id() ) {
return null;
}

if ( is_null( $this->parent ) ) {
if ( ! $this->get_item_parent_id() ) {
return null;
}

if ( $this->get_shipment() ) {
if ( $item = $this->get_shipment()->get_item( $this->get_item_parent_id() ) ) {
$this->parent = $item;
Expand Down Expand Up @@ -763,4 +779,24 @@ public function set_attributes( $attributes ) {
public function is_readonly() {
return apply_filters( "{$this->get_general_hook_prefix()}is_readonly", $this->get_item_parent_id() > 0 ? true : false, $this );
}

public function save() {
if ( $parent = $this->get_parent() ) {
if ( $this->get_item_parent_id() !== $parent->get_id() ) {
$this->set_item_parent_id( $parent->get_id() );
}
}

return parent::save();
}

public function delete( $force_delete = false ) {
$has_deleted = parent::delete( $force_delete );

if ( true === $has_deleted && $this->has_children() ) {
foreach ( $this->get_children() as $child ) {
$child->delete( $force_delete );
}
}
}
}
2 changes: 1 addition & 1 deletion templates/myaccount/add-return-shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*/
do_action( 'woocommerce_gzd_add_return_shipment_details_before_shipment_table_items', $order );

foreach ( $shipment_order->get_available_items_for_return() as $order_item_id => $item_data ) {
foreach ( $shipment_order->get_selectable_items_for_return() as $order_item_id => $item_data ) {
wc_get_template(
'shipment/add-return-shipment-item.php',
array(
Expand Down
Loading

0 comments on commit 56453f3

Please sign in to comment.