create_shipping( $order, $parent_order ) { dokan_log( sprintf( '#%d - Creating Shipping.', $order->get_id() ) ); // Get all shipping methods for parent order $shipping_methods = $parent_order->get_shipping_methods(); $order_seller_id = dokan_get_seller_id_by_order( $order->get_id() ); $applied_shipping_method = ''; if ( $shipping_methods ) { foreach ( $shipping_methods as $method_item_id => $shipping_object ) { $shipping_seller_id = wc_get_order_item_meta( $method_item_id, 'seller_id', true ); if ( $order_seller_id == $shipping_seller_id ) { $applied_shipping_method = $shipping_object; break; } } } $shipping_method = apply_filters( 'dokan_shipping_method', $applied_shipping_method, $order->get_id(), $parent_order ); // bail out if no shipping methods found if ( ! $shipping_method ) { dokan_log( sprintf( '#%d - No shipping method found. Aborting.', $order->get_id() ) ); return; } if ( is_a( $shipping_method, 'WC_Order_Item_Shipping' ) ) { $item = new \WC_Order_Item_Shipping(); dokan_log( sprintf( '#%d - Adding shipping item.', $order->get_id() ) ); $item->set_props( array( 'method_title' => $shipping_method->get_name(), 'method_id' => $shipping_method->get_method_id(), 'total' => $shipping_method->get_total(), 'taxes' => $shipping_method->get_taxes(), ) ); $metadata = $shipping_method->get_meta_data(); if ( $metadata ) { foreach ( $metadata as $meta ) { $item->add_meta_data( $meta->key, $meta->value ); } } $order->add_item( $item ); $order->set_shipping_total( $shipping_method->get_total() ); $order->save(); } } /** * Create coupons for a sub-order if neccessary * * @param \WC_Order $order * @param \WC_Order $parent_order * @param array $products * * @return void */ public function create_coupons( $order, $parent_order, $products ) { $used_coupons = $parent_order->get_items( 'coupon' ); $product_ids = array_map( function ( $item ) { return $item->get_product_id(); }, $products ); if ( ! $used_coupons ) { return; } $seller_id = dokan_get_seller_id_by_order( $order->get_id() ); if ( ! $seller_id ) { return; } foreach ( $used_coupons as $item ) { $coupon = new \WC_Coupon( $item->get_code() ); if ( $coupon && ! is_wp_error( $coupon ) && ( array_intersect( $product_ids, $coupon->get_product_ids() ) || apply_filters( 'dokan_is_order_have_admin_coupon', false, $coupon, [ $seller_id ], $product_ids ) ) ) { $new_item = new \WC_Order_Item_Coupon(); $new_item->set_props( array( 'code' => $item->get_code(), 'discount' => $item->get_discount(), 'discount_tax' => $item->get_discount_tax(), ) ); $new_item->add_meta_data( 'coupon_data', $coupon->get_data() ); $order->add_item( $new_item ); } } $order->save(); } /** * Monitors a new order and attempts to create sub-orders * * If an order contains products from multiple vendor, we can't show the order * to each seller dashboard. That's why we need to divide the main order to * some sub-orders based on the number of sellers. * * @param int $parent_order_id * * @return void */ public function maybe_split_orders( $parent_order_id ) { $parent_order = dokan()->order->get( $parent_order_id ); dokan_log( sprintf( 'New Order #%d created. Init sub order.', $parent_order_id ) ); if ( $parent_order->get_meta( 'has_sub_order' ) == true ) { $args = array( 'post_parent' => $parent_order_id, 'post_type' => 'shop_order', 'numberposts' => - 1, 'post_status' => 'any', ); $child_orders = get_children( $args ); foreach ( $child_orders as $child ) { wp_delete_post( $child->ID, true ); } } $vendors = dokan_get_sellers_by( $parent_order_id ); // return if we've only ONE seller if ( count( $vendors ) == 1 ) { dokan_log( '1 vendor only, skipping sub order.' ); $temp = array_keys( $vendors ); $seller_id = reset( $temp ); do_action( 'dokan_create_parent_order', $parent_order, $seller_id ); $parent_order->update_meta_data( '_dokan_vendor_id', $seller_id ); $parent_order->save(); // if the request is made from rest api then insert the order data to the sync table if ( defined( 'REST_REQUEST' ) ) { do_action( 'dokan_checkout_update_order_meta', $parent_order_id, $seller_id ); } return; } // flag it as it has a suborder $parent_order->update_meta_data( 'has_sub_order', true ); $parent_order->save(); dokan_log( sprintf( 'Got %s vendors, starting sub order.', count( $vendors ) ) ); // seems like we've got multiple sellers foreach ( $vendors as $seller_id => $seller_products ) { dokan()->order->create_sub_order( $parent_order, $seller_id, $seller_products ); } dokan_log( sprintf( 'Completed sub order for #%d.', $parent_order_id ) ); } /** * This will check if given var is empty or not. * * @since 3.6.3 * * @param mixed $var * * @return bool */ protected function is_empty( $var ) { if ( empty( $var ) ) { return true; } // if var is an array, check if it's empty or not if ( is_array( $var ) && isset( $var[0] ) && intval( $var[0] ) === 0 ) { return true; } return false; } }