optionsProvider = $optionsProvider; $this->carrierRepository = $carrierRepository; $this->calculator = $weightCalculator; $this->pickupPointsConfig = $pickupPointsConfig; } /** * Creates common order entity from WC_Order. * * @param WC_Order $wcOrder WC_Order. * @param Entity\Order $order Partial order. * * @return Entity\Order */ public function finalize( WC_Order $wcOrder, Entity\Order $order ): Entity\Order { $order->setCalculatedWeight( $this->calculator->calculateOrderWeight( $wcOrder ) ); if ( null === $order->containsAdultContent() ) { $order->setAdultContent( $this->containsAdultContent( $wcOrder ) ); } $order->setShippingCountry( strtolower( $wcOrder->get_shipping_country() ) ); $orderData = $wcOrder->get_data(); $contactInfo = ( $wcOrder->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] ); $order->setName( $contactInfo['first_name'] ); $order->setSurname( $contactInfo['last_name'] ); $order->setEshop( $this->optionsProvider->get_sender() ); if ( null === $order->getValue() ) { $order->setValue( (float) $wcOrder->get_total( 'raw' ) ); } $address = $order->getDeliveryAddress(); if ( null === $address ) { $order->setAddressValidated( false ); $address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] ); } $order->setDeliveryAddress( $address ); $order->setCustomNumber( $wcOrder->get_order_number() ); // Shipping address phone is optional. $order->setPhone( $orderData['billing']['phone'] ); if ( ! empty( $contactInfo['phone'] ) ) { $order->setPhone( $contactInfo['phone'] ); } // Additional address information. if ( ! empty( $contactInfo['address_2'] ) ) { $order->setNote( $contactInfo['address_2'] ); } $order->setEmail( $orderData['billing']['email'] ); $codMethod = $this->optionsProvider->getCodPaymentMethod(); if ( null === $order->getCod() && $orderData['payment_method'] === $codMethod ) { $order->setCod( $order->getValue() ); } if ( $orderData['payment_method'] !== $codMethod ) { $order->setCod( null ); } $order->setSize( $order->getSize() ); $carrierCode = null; if ( $order->isExternalCarrier() ) { $carrier = $this->carrierRepository->getById( (int) $order->getCarrierId() ); $order->setCarrier( $carrier ); $carrierCode = $order->getCarrierId(); } else { $shippingCountry = $order->getShippingCountry(); if ( null !== $shippingCountry ) { // TODO: for non-compound split carriers use value from database. See Metabox and PacketSubmitter. $carrierCode = $this->pickupPointsConfig->getCompoundCarrierIdByCountry( $shippingCountry ); } } $order->setCarrierCode( $carrierCode ); $order->setCurrency( $wcOrder->get_currency() ); return $order; } /** * Finds out if adult content is present. * * @param WC_Order $wcOrder WC Order. * * @return bool */ private function containsAdultContent( WC_Order $wcOrder ): bool { foreach ( $wcOrder->get_items() as $item ) { $product = $item->get_product(); if ( $product ) { $productEntity = new Product\Entity( $product ); if ( $productEntity->isPhysical() && $productEntity->isAgeVerification18PlusRequired() ) { return true; } } } return false; } }