Skip to content

Commit

Permalink
Version bump to 3.2.0. Order endpoint improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Apr 9, 2024
1 parent 8f33923 commit b9785ce
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "woocommerce-germanized-shipments",
"title": "Shipments for WooCommerce",
"version": "3.1.1",
"version": "3.2.0",
"homepage": "https://vendidero.de",
"repository": {
"type": "git",
Expand Down
103 changes: 70 additions & 33 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public static function init() {
add_filter( 'woocommerce_rest_api_get_rest_namespaces', array( __CLASS__, 'register_controllers' ) );

add_filter( 'woocommerce_rest_shop_order_schema', array( __CLASS__, 'order_shipments_schema' ), 10 );
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'prepare_order_shipments' ), 10, 3 );
add_filter( 'woocommerce_rest_shop_order_schema', array( __CLASS__, 'order_schema' ) );
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'prepare_order_shipments' ), 15, 3 );
add_filter( 'woocommerce_rest_pre_insert_shop_order_object', array( __CLASS__, 'insert_order_shipments' ), 10, 3 );

add_filter( 'woocommerce_rest_product_schema', array( __CLASS__, 'product_schema' ) );
add_filter( 'woocommerce_rest_product_variation_schema', array( __CLASS__, 'product_variation_schema' ) );
Expand Down Expand Up @@ -111,29 +111,6 @@ public static function remove_status_prefix( $status ) {
return $status;
}

/**
* Extend schema.
*
* @since 1.0.0
*
* @param array $schema_properties Data used to create the order.
*
* @return array
*/
public static function order_schema( $schema_properties ) {
$statuses = array_map( array( __CLASS__, 'remove_status_prefix' ), array_keys( wc_gzd_get_shipment_order_shipping_statuses() ) );

$schema_properties['shipping_status'] = array(
'description' => _x( 'Shipping status', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'string',
'enum' => $statuses,
'context' => array( 'view', 'edit' ),
'readonly' => true,
);

return $schema_properties;
}

/**
* Extend product variation schema.
*
Expand Down Expand Up @@ -255,6 +232,25 @@ public static function product_schema( $schema_properties ) {
return $schema_properties;
}

/**
* @param \WC_Order $order
* @param WP_REST_Request $request
* @param bool $creating
*
* @return \WC_Order
*/
public static function insert_order_shipments( $order, $request, $creating ) {
if ( isset( $request['pickup_location_code'] ) ) {
$order->update_meta_data( '_pickup_location_code', wc_clean( $request['pickup_location_code'] ) );
}

if ( isset( $request['pickup_location_customer_number'] ) ) {
$order->update_meta_data( '_pickup_location_customer_number', wc_clean( $request['pickup_location_customer_number'] ) );
}

return $order;
}

/**
* @param WP_REST_Response $response
* @param $post
Expand All @@ -263,39 +259,80 @@ public static function product_schema( $schema_properties ) {
* @return mixed
*/
public static function prepare_order_shipments( $response, $post, $request ) {
$order = wc_get_order( $post );
$response_order_data = $response->get_data();
$response_order_data['shipments'] = array();
$response_order_data['shipping_status'] = 'no-shipping-needed';
$order = wc_get_order( $post );
$response_order_data = $response->get_data();
$response_order_data = wp_parse_args(
$response_order_data,
array(
'shipments' => array(),
'shipping_status' => 'no-shipping-needed',
'shipping_provider' => '',
'pickup_location_code' => '',
'pickup_location_customer_number' => '',
)
);

if ( $order ) {
$order_shipment = wc_gzd_get_shipment_order( $order );
$shipments = $order_shipment->get_shipments();
$provider = $order_shipment->get_shipping_provider();

if ( ! empty( $shipments ) ) {
foreach ( $shipments as $shipment ) {
$response_order_data['shipments'][] = ShipmentsController::prepare_shipment( $shipment, 'view', $request['dp'] );
}
}

$response_order_data['shipping_status'] = $order_shipment->get_shipping_status();
$response_order_data['shipping_status'] = $order_shipment->get_shipping_status();
$response_order_data['shipping_provider'] = $provider ? $provider->get_name() : '';
$response_order_data['pickup_location_code'] = $order_shipment->get_pickup_location_code();
$response_order_data['pickup_location_customer_number'] = $order_shipment->get_pickup_location_customer_number();
}

$response->set_data( $response_order_data );

return $response;
}

public static function order_shipments_schema( $schema ) {
$schema['shipments'] = array(
public static function order_shipments_schema( $schema_properties ) {
$statuses = array_map( array( __CLASS__, 'remove_status_prefix' ), array_keys( wc_gzd_get_shipment_order_shipping_statuses() ) );

$schema_properties['shipments'] = array(
'description' => _x( 'List of shipments.', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => ShipmentsController::get_single_item_schema(),
);

return $schema;
$schema_properties['shipping_status'] = array(
'description' => _x( 'Shipping status', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'string',
'enum' => $statuses,
'context' => array( 'view', 'edit' ),
'readonly' => true,
);

$schema_properties['shipping_provider'] = array(
'description' => _x( 'Shipping provider', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
);

$schema_properties['pickup_location_code'] = array(
'description' => _x( 'Pickup location code', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
);

$schema_properties['pickup_location_customer_number'] = array(
'description' => _x( 'Pickup location customer number', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
);

return $schema_properties;
}

protected static function get_shipment_statuses() {
Expand Down
2 changes: 1 addition & 1 deletion src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Package {
*
* @var string
*/
const VERSION = '3.1.1';
const VERSION = '3.2.0';

public static $upload_dir_suffix = '';

Expand Down
3 changes: 3 additions & 0 deletions src/ShippingMethod/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,13 @@ public function calculate_shipping( $package = array() ) {
* included within the package.
*/
$item_map = array();
$weight = 0.0;

foreach ( $items as $item ) {
$cart_item_wrapper = $item->getItem();
$product = $cart_item_wrapper->get_product();
$product_key = $product->get_parent_id() . '_' . $product->get_id();
$weight += $cart_item_wrapper->getWeight();

if ( array_key_exists( $product_key, $item_map ) ) {
$item_map[ $product_key ]++;
Expand All @@ -711,6 +713,7 @@ public function calculate_shipping( $package = array() ) {
'packaging_id' => $packaging->get_id(),
'rules' => $package_applied_rules,
'items' => $item_map,
'weight' => $weight + $packaging->getEmptyWeight(),
);

$rule_ids = array_unique( array_merge( $rule_ids, $package_applied_rules ) );
Expand Down
2 changes: 1 addition & 1 deletion woocommerce-germanized-shipments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: The Germanized Shipments integration, installed as a feature plugin for development and testing purposes.
* Author: vendidero
* Author URI: https://vendidero.de
* Version: 3.1.1
* Version: 3.2.0
* Requires PHP: 5.6
* License: GPLv3
*
Expand Down

0 comments on commit b9785ce

Please sign in to comment.