Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to automatically sort items into parcels #8

Open
bilfeldt opened this issue Mar 19, 2019 · 0 comments
Open

Add method to automatically sort items into parcels #8

bilfeldt opened this issue Mar 19, 2019 · 0 comments
Milestone

Comments

@bilfeldt
Copy link
Member

We should add a method to automatically sort items into parcels with a weight below a given limit.

This method should be automatically fired when an order is placed. It will split the items into parcels and then save the configuration in the order meta data.

That way it will be used whether or not the webshop over creates labels automatically, via bulk or via order details.

Example

/*
 * Function to split items into parcels
 *
 * This function can be run any time you like, as long as it happens before creating a label with Smart Send
 *
 */
public function split_items_into_parcels($order) {

	$packaging_weight = 0.5;//TODO: packaging_weight should be a setting
	$max_parcel_weight_excluding_packaging = 20;//TODO: max_box_weight should be a setting
	$max_parcel_weight_including_packaging = $max_parcel_weight_excluding_packaging + $packaging_weight;
	
	$parcels = array();//Array that will contain parcels and items
	$package_count = 1;

	foreach( $order->get_items() as $item ) {
		if( $item['product_id'] > 0 ) {
			$_product = $order->get_product_from_item( $item );
			if ( !$_product->is_virtual() ) {
				for ($x = 1; $x <= $item['qty']; $x++) {//TODO: Find a better way to "split" the different items. They should be divided into as few parcels as possible
					if( $weight + $_product->get_weight() > $max_parcel_weight_including_packaging ){
						//Create a new package
						$package_count++;
						$weight = $packaging_weight + $_product->get_weight();
					} else {
						$weight += $_product->get_weight();
					}
					
					// Add item
					$parcels[] = array(
						'id' => $_product['product_id'],//TODO: account for variation_id see line 204 @ smart-send-logistics/includes/class-ss-shipping-wc-order.php
						'name' => $_product['name'],
						'value' => $box_count,
					);
				}
			}
		}
	}
	
	// This is where we save the parcel/item configuration
	SS_SHIPPING_WC()->get_ss_shipping_wc_order()->save_ss_shipping_order_parcels($order_id, $parcels);
	//It can also be done manually using: update_post_meta($order_id, 'ss_shipping_order_parcels', $parcels);
}
@bilfeldt bilfeldt added this to the v9.0.0 milestone Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant