diff --git a/package.json b/package.json index c6b0633a..4b868afa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Api.php b/src/Api.php index d9100947..c2babe6f 100644 --- a/src/Api.php +++ b/src/Api.php @@ -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' ) ); @@ -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. * @@ -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 @@ -263,14 +259,23 @@ 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 ) { @@ -278,7 +283,10 @@ public static function prepare_order_shipments( $response, $post, $request ) { } } - $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 ); @@ -286,8 +294,10 @@ public static function prepare_order_shipments( $response, $post, $request ) { 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' ), @@ -295,7 +305,34 @@ public static function order_shipments_schema( $schema ) { '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() { diff --git a/src/Package.php b/src/Package.php index ffcd3c7c..a91e0cb3 100644 --- a/src/Package.php +++ b/src/Package.php @@ -18,7 +18,7 @@ class Package { * * @var string */ - const VERSION = '3.1.1'; + const VERSION = '3.2.0'; public static $upload_dir_suffix = ''; diff --git a/src/ShippingMethod/ShippingMethod.php b/src/ShippingMethod/ShippingMethod.php index c8afde27..9726af99 100644 --- a/src/ShippingMethod/ShippingMethod.php +++ b/src/ShippingMethod/ShippingMethod.php @@ -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 ]++; @@ -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 ) ); diff --git a/woocommerce-germanized-shipments.php b/woocommerce-germanized-shipments.php index d65f4405..c5a557b5 100644 --- a/woocommerce-germanized-shipments.php +++ b/woocommerce-germanized-shipments.php @@ -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 *