v = $v; } public function boot() { add_filter( 'woocommerce_cart_shipping_packages', array( $this, 'split_packages' ), 20 ); add_filter( 'woocommerce_shipping_methods', function ( $m ) { $m['frontglut_vendor_shipping'] = 'FRONTGLUT_Vendor_Shipping'; return $m; } ); add_action( 'woocommerce_shipping_init', function () { if ( ! class_exists( 'FRONTGLUT_Vendor_Shipping' ) ) { require_once __DIR__ . '/ShippingMethod.php'; } } ); add_action( 'woocommerce_checkout_create_order_shipping_item', function ( $item, $package_key, $package ) { if ( ! empty( $package['frontglut_vendor'] ) ) { $item->add_meta_data( '_frontglut_vendor_id', (int) $package['frontglut_vendor'], true ); $item->add_meta_data( __( 'Shipped by', 'frontglut' ), Vendors::store( (int) $package['frontglut_vendor'] )['name'], true ); } }, 10, 3 ); } public function split_packages( $packages ) { if ( ! Settings::get( 'vendors.vendor_shipping' ) || 'native' !== Settings::get( 'vendors.provider', 'native' ) ) { return $packages; } $out = array(); foreach ( $packages as $pkg ) { $groups = array(); foreach ( $pkg['contents'] as $key => $item ) { $vid = Vendors::product_vendor( $item['product_id'] ); $groups[ $vid ][ $key ] = $item; } if ( count( $groups ) <= 1 && ! isset( $groups[0] ) ) { $pkg['frontglut_vendor'] = (int) key( $groups ); $out[] = $pkg; continue; } foreach ( $groups as $vid => $contents ) { $p = $pkg; $p['contents'] = $contents; $p['contents_cost'] = array_sum( wp_list_pluck( $contents, 'line_total' ) ); $p['frontglut_vendor'] = (int) $vid; $out[] = $p; } } return $out ? $out : $packages; } }