get_values( $order ) ); } /** * Get variable values for order. * * @param mixed $order WooCommerce order. * @return array */ private function get_values( $order ): array { $values = array_fill_keys( self::allowed_variables(), '' ); if ( ! $order || ! is_object( $order ) ) { $values['{StoresName}'] = get_bloginfo( 'name' ); return $values; } $ttn = $this->get_ttn_number( $order ); $values['{StoresName}'] = get_bloginfo( 'name' ); $values['{ClientName}'] = $this->call_order_getter( $order, 'get_billing_first_name' ); $values['{ClientLastName}'] = $this->call_order_getter( $order, 'get_billing_last_name' ); $values['{OrderStatus}'] = $this->get_order_status_label( $order ); $values['{AddrOrderDelivery}'] = $this->get_order_delivery_address( $order ); $values['{ClientEmail}'] = $this->call_order_getter( $order, 'get_billing_email' ); $values['{ClientPhone}'] = $this->call_order_getter( $order, 'get_billing_phone' ); $values['{MethodPayment}'] = $this->call_order_getter( $order, 'get_payment_method_title' ); $values['{OrderID}'] = $this->call_order_getter( $order, 'get_id' ); $values['{OrderSum}'] = $this->call_order_getter( $order, 'get_total' ); $values['{OrderCurrency}'] = $this->call_order_getter( $order, 'get_currency' ); $values['{TTN_Number}'] = $ttn; $values['{TrackingURL}'] = '' === $ttn ? '' : 'https://novaposhta.ua/tracking/index/cargo_number/' . rawurlencode( $ttn ); $values['{PaymentURL}'] = $this->call_order_getter( $order, 'get_checkout_payment_url' ); return $values; } /** * Call order getter safely. * * @param mixed $order Order. * @param string $method Method. */ private function call_order_getter( $order, string $method ): string { if ( ! is_object( $order ) || ! method_exists( $order, $method ) ) { return ''; } $value = $order->{$method}(); return is_scalar( $value ) ? (string) $value : ''; } /** * Get localized order status label. * * @param mixed $order Order. */ private function get_order_status_label( $order ): string { $status = $this->call_order_getter( $order, 'get_status' ); if ( '' === $status ) { return ''; } if ( function_exists( 'wc_get_order_status_name' ) ) { return wc_get_order_status_name( $status ); } return $status; } /** * Get delivery address. * * @param mixed $order Order. */ private function get_order_delivery_address( $order ): string { if ( is_object( $order ) && method_exists( $order, 'get_formatted_shipping_address' ) ) { $shipping = wp_strip_all_tags( (string) $order->get_formatted_shipping_address() ); if ( '' !== $shipping ) { return $shipping; } } if ( is_object( $order ) && method_exists( $order, 'get_formatted_billing_address' ) ) { return wp_strip_all_tags( (string) $order->get_formatted_billing_address() ); } return ''; } /** * Get TTN from supported meta fields. * * @param mixed $order Order. */ private function get_ttn_number( $order ): string { if ( ! is_object( $order ) || ! method_exists( $order, 'get_meta' ) ) { return ''; } foreach ( array( '_shipping_ttn', '_novaposhta_consignment_number', '_morkva_shipping_number' ) as $meta_key ) { $value = (string) $order->get_meta( $meta_key, true ); if ( '' !== $value ) { return $value; } } return ''; } }