Skip to content

Commit

Permalink
Item packing: Use canPack method instead of grouping.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Nov 22, 2023
1 parent 0ea6ae3 commit cab80ac
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 213 deletions.
75 changes: 36 additions & 39 deletions src/Automation.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ public static function create_shipments( $order ) {

if ( $order_shipment->needs_shipping() ) {
if ( $order_shipment->has_auto_packing() ) {
$items_to_pack = $order_shipment->get_items_to_pack_left_for_shipping();

if ( $method = $order_shipment->get_builtin_shipping_method() ) {
$packaging_boxes = $method->get_method()->get_available_packaging_boxes();
} else {
Expand All @@ -239,48 +237,47 @@ public static function create_shipments( $order ) {
$packaging_boxes = Helper::get_packaging_boxes( $available_packaging );
}

foreach ( $items_to_pack as $group => $items ) {
$packed_boxes = Helper::pack( $items, $packaging_boxes, '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 ( $packed_boxes as $box ) {
$packaging = $box->getBox();
$items = $box->getItems();
$shipment_items = array();
foreach ( $items as $item ) {
$order_item = $item->getItem();

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() ]++;
}
}

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

$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 );
}
foreach ( $shipment->get_error_messages() as $code => $message ) {
$errors->add( $code, $message );
}
}
}
Expand Down
33 changes: 21 additions & 12 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Vendidero\Germanized\Shipments;

use DVDoug\BoxPacker\ItemList;
use Exception;
use Vendidero\Germanized\Shipments\Packing\Helper;
use Vendidero\Germanized\Shipments\Packing\ItemList;
use Vendidero\Germanized\Shipments\Packing\OrderItem;
use Vendidero\Germanized\Shipments\ShippingMethod\MethodHelper;
use Vendidero\Germanized\Shipments\ShippingMethod\ProviderMethod;
Expand Down Expand Up @@ -542,29 +542,38 @@ public function get_item_quantity_left_for_returning( $order_item_id, $args = ar
/**
* @param false $legacy_group_by_product_group
*
* @return OrderItem[]
* @return ItemList|OrderItem[]
*/
public function get_items_to_pack_left_for_shipping( $legacy_group_by_product_group = null ) {
$items = $this->get_available_items_for_shipment();
$items_to_be_packed = array();
$items_to_be_packed = ! is_null( $legacy_group_by_product_group ) ? array() : new ItemList();

foreach ( $items as $order_item_id => $item ) {
if ( ! $order_item = $this->get_order()->get_item( $order_item_id ) ) {
continue;
}

$product_group = '';
$box_item = new Packing\OrderItem( $order_item );

if ( $product = $order_item->get_product() ) {
$product_group = Helper::get_product_packing_group( $product );
}
if ( ! is_null( $legacy_group_by_product_group ) ) {
$product_group = '';

if ( ! array_key_exists( $product_group, $items_to_be_packed ) ) {
$items_to_be_packed[ $product_group ] = new ItemList();
}
if ( $product = $order_item->get_product() ) {
$product_group = '';

$box_item = new Packing\OrderItem( $order_item );
$items_to_be_packed[ $product_group ]->insert( $box_item, $item['max_quantity'] );
if ( 'yes' === get_option( 'woocommerce_gzd_shipments_packing_group_by_shipping_class' ) ) {
$product_group = $product->get_shipping_class();
}
}

if ( ! array_key_exists( $product_group, $items_to_be_packed ) ) {
$items_to_be_packed[ $product_group ] = new ItemList();
}

$items_to_be_packed[ $product_group ]->insert( $box_item, $item['max_quantity'] );
} else {
$items_to_be_packed->insert( $box_item, $item['max_quantity'] );
}
}

return apply_filters( 'woocommerce_gzd_shipment_order_items_to_pack_left_for_shipping', $items_to_be_packed );
Expand Down
17 changes: 1 addition & 16 deletions src/Packing/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,7 @@ public static function enable_auto_packing() {
}

/**
* @param \WC_Product $product
*
* @return string
*/
public static function get_product_packing_group( $product ) {
$group = '';

if ( 'yes' === get_option( 'woocommerce_gzd_shipments_packing_group_by_shipping_class' ) ) {
$group = $product->get_shipping_class();
}

return apply_filters( 'woocommerce_gzd_product_packing_group', $group, $product );
}

/**
* @param PackingItem[] $items
* @param \Vendidero\Germanized\Shipments\Packing\ItemList $items
* @param PackingBox[]|BoxList $boxes
*
* @return PackedBoxList
Expand Down
15 changes: 15 additions & 0 deletions src/Packing/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public function canBePacked( $box, $already_packed_items, int $proposed_x, int $
$fits = false;
}
}

/**
* In case grouping is activated make sure that a new item with a different shipping class
* is not being packed within the same already existing package.
*/
if ( $fits && 'yes' === get_option( 'woocommerce_gzd_shipments_packing_group_by_shipping_class' ) ) {
$count = $already_packed_items->count();
$last_item = $count > 0 ? $already_packed_items->getIterator()[ $count - 1 ]->getItem() : false;

if ( $last_item && ( $last_product = $last_item->get_product() ) ) {
if ( $last_product->get_shipping_class_id() !== $shipping_class ) {
$fits = false;
}
}
}
}

return apply_filters( 'woocommerce_gzd_shipments_item_fits_packaging', $fits, $this, $box->get_packaging(), $already_packed_items, $args );
Expand Down
12 changes: 12 additions & 0 deletions src/Packing/ItemList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Vendidero\Germanized\Shipments\Packing;

defined( 'ABSPATH' ) || exit;

/**
* An item to be packed.
*/
class ItemList extends \DVDoug\BoxPacker\ItemList {

}
20 changes: 5 additions & 15 deletions src/ShippingMethod/MethodHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Vendidero\Germanized\Shipments\ShippingMethod;

use DVDoug\BoxPacker\ItemList;
use Vendidero\Germanized\Shipments\Package;
use Vendidero\Germanized\Shipments\Packing\CartItem;
use Vendidero\Germanized\Shipments\Packing\ItemList;
use Vendidero\Germanized\Shipments\ShippingProvider\Helper;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -62,26 +62,16 @@ public static function register_shipping_methods( $methods ) {
}

public static function register_cart_items_to_pack( $cart_contents ) {
if ( ! Package::is_packing_supported() ) {
if ( ! Package::is_packing_supported() || apply_filters( 'woocommerce_gzd_shipments_disable_cart_packing', false ) ) {
return $cart_contents;
}

foreach ( $cart_contents as $index => $content ) {
$items = array();
$items = new ItemList();

foreach ( $content['contents'] as $content_key => $data ) {
$cart_item = new CartItem( $data, wc()->cart->display_prices_including_tax() );
$product_group = '';

if ( $product = $cart_item->get_product() ) {
$product_group = \Vendidero\Germanized\Shipments\Packing\Helper::get_product_packing_group( $product );
}

if ( ! array_key_exists( $product_group, $items ) ) {
$items[ $product_group ] = new ItemList();
}

$items[ $product_group ]->insert( $cart_item, $data['quantity'] );
$cart_item = new CartItem( $data, wc()->cart->display_prices_including_tax() );
$items->insert( $cart_item, $data['quantity'] );
}

$cart_contents[ $index ]['items_to_pack'] = $items;
Expand Down
Loading

0 comments on commit cab80ac

Please sign in to comment.