* @copyright Copyright (c) 2018 Ekomi ltd (http://www.ekomi.de) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link http://www.ekomi.de */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Export Order Data to SRR ekomi. * * @param int $order_id Order id. * @param bool $is_cron Export method. * * @return void */ function ei_export_order_data( $order_id, $is_cron = false ) { $common_configurations = EKOMI_INTEGRATION\ei_get_common_options(); $export_method_check = ( $is_cron ) ? EKOMI_INTEGRATION\EXPORT_METHOD_CRON : EKOMI_INTEGRATION\EXPORT_METHOD_STATUS; if ( $export_method_check === $common_configurations['ee_export_method'] ) { $order = new WC_Order( $order_id ); $order_status = ''; if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '8.2', '>=' ) ) { $order_status = "wc-" . $order->get_status(); } else { $order_status = $order->post_status; } $order_statuses_raw = $common_configurations['ee_order_statuses'] ?? []; $order_statuses = is_array($order_statuses_raw) ? $order_statuses_raw : array_map('trim', explode(',', $order_statuses_raw)); if ( in_array( $order_status, $order_statuses, true ) ) { $site_url = site_url(); $guid = $order->post->guid; $wpml_options = EKOMI_INTEGRATION\ei_get_wpml_settings(); $default_lang = get_locale(); $lang_code = explode( '_', $default_lang )[0]; if ( $wpml_options ) { $order_lang_code=get_post_meta($order_id, 'wpml_language', true); if(!empty($order_lang_code)){ $lang_code=$order_lang_code; } else { $default_lang = $wpml_options['default_language']; if ( empty( $default_lang ) ) { $default_lang = get_locale(); } $lang_code = explode( '_', $default_lang )[0]; } if ( $guid ) { $guid = explode( $site_url, $guid )[1]; $lang_code_temp = explode( '/', $guid )[1]; if ( in_array( $lang_code_temp, $wpml_options['active_languages'], true ) ) { $lang_code = $lang_code_temp; } } } $configurations = EKOMI_INTEGRATION\ei_get_options_data( $lang_code ); if ( EKOMI_INTEGRATION\ENABLE_VALUE === $configurations['ee_active'] && EKOMI_INTEGRATION\ENABLE_VALUE === $configurations['ee_t_and_c'] ) { $fields = prepare_order_data($configurations, $order_id, $order); /** * Send order data to Plugins-Dashboard. */ ei_send_order_data( $fields ); } } } } /** * Prepares order data. * * @param array $configurations * @param int $order_id * @param object $order * * @return array */ function prepare_order_data( $configurations, $order_id, $order) { $fields = array( 'shop_id' => $configurations['ee_shop_id'], 'interface_password' => $configurations['ee_shop_pw'], 'mode' => $configurations['ee_mode'], 'product_reviews' => $configurations['ee_product_reviews'], 'plugin_name' => 'woocommerce', 'product_identifier' => $configurations['ee_product_identifier'], 'exclude_products' => $configurations['ee_exclude_products'], ); $fields['order_data'] = ei_get_order_detail( $order_id, $order ); return $fields; } add_action( 'woocommerce_order_status_changed', 'ei_export_order_data' ); /** * Gets order payment details. * * @param object $order Order data. * * @return array */ function get_order_payment_details( $order ) { $payment_details = array( 'method_id' => $order->payment_method, 'method_title' => $order->payment_method_title, 'paid_at' => ! empty( $order->paid_date ) ? $order->paid_date : '', ); return $payment_details; } /** * Gets order billing data. * * @param object $order Order data. * * @return array */ function get_order_billing_information( $order ) { $billing = array( 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address_1' => $order->billing_address_1, 'address_2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'postcode' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'phone' => $order->billing_phone, ); return $billing; } /** * Gets order shipping data. * * @param object $order Order data. * * @return array */ function get_order_shipping_information( $order ) { $shipping = array( 'first_name' => $order->shipping_first_name, 'last_name' => $order->shipping_last_name, 'company' => $order->shipping_company, 'address_1' => $order->shipping_address_1, 'address_2' => $order->shipping_address_2, 'city' => $order->shipping_city, 'state' => $order->shipping_state, 'postcode' => $order->shipping_postcode, 'country' => $order->shipping_country, ); return $shipping; } /** * Gets order details. * * @param int $id Order id. * @param object $order Order object. * @param null $fields Order details. * @param array $filter Filters. * * @return array */ function ei_get_order_detail( $id, $order, $fields = null, $filter = array() ) { if ( is_wp_error( $id ) ) { return $id; } /** * Get the decimal precession */ $dp = isset( $filter['dp'] ) ? intval( $filter['dp'] ) : 2; $order_data = array( 'id' => $order->id, 'order_number' => $order->get_order_number(), 'created_at' => $order->date_created->date('Y-m-d H:i:s'), 'updated_at' => $order->date_modified->date('Y-m-d H:i:s'), 'completed_at' => ! empty( $order->completed_date ) ? $order->completed_date : '', 'status' => $order->get_status(), 'currency' => $order->get_order_currency(), 'total' => wc_format_decimal( $order->get_total(), $dp ), 'subtotal' => wc_format_decimal( $order->get_subtotal(), $dp ), 'total_line_items_quantity' => $order->get_item_count(), 'total_tax' => wc_format_decimal( $order->get_total_tax(), $dp ), 'total_shipping' => wc_format_decimal( $order->get_total_shipping(), $dp ), 'cart_tax' => wc_format_decimal( $order->get_cart_tax(), $dp ), 'shipping_tax' => wc_format_decimal( $order->get_shipping_tax(), $dp ), 'total_discount' => wc_format_decimal( $order->get_total_discount(), $dp ), 'shipping_methods' => $order->get_shipping_method(), 'order_key' => $order->order_key, 'payment_details' => get_order_payment_details( $order ), 'billing' => get_order_billing_information( $order ), 'shipping' => get_order_shipping_information( $order ), 'note' => $order->customer_note, 'customer_ip' => $order->customer_ip_address, 'customer_user_agent' => $order->customer_user_agent, 'customer_id' => $order->get_user_id(), 'view_order_url' => $order->get_view_order_url(), 'line_items' => array(), 'shipping_lines' => array(), 'tax_lines' => array(), 'fee_lines' => array(), 'coupon_lines' => array(), ); /** * Getting all line items. */ foreach ( $order->get_items() as $item_id => $item ) { $product = $order->get_product_from_item( $item ); $product_id = null; $product_sku = null; /** * Check if the product exists. */ if ( is_object( $product ) ) { $product_id = ! empty( $product->variation_id ) ? $product->variation_id : $product->id; $product_sku = $product->get_sku(); } $meta = new WC_Order_Item_Meta( $item, $product ); $item_meta = array(); $hideprefix = ( isset( $filter['all_item_meta'] ) && 'true' === $filter['all_item_meta'] ) ? null : '_'; foreach ( $meta->get_formatted( $hideprefix ) as $meta_key => $formatted_meta ) { $item_meta[] = array( 'key' => $formatted_meta['key'], 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value'], ); } $order_data['products'][] = array( 'id' => $item_id, 'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item, false, false ), $dp ), 'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], $dp ), 'total' => wc_format_decimal( $order->get_line_total( $item, false, false ), $dp ), 'total_tax' => wc_format_decimal( $item['line_tax'], $dp ), 'price' => wc_format_decimal( $order->get_item_total( $item, false, false ), $dp ), 'quantity' => wc_stock_amount( $item['qty'] ), 'tax_class' => ( ! empty( $item['tax_class'] ) ) ? $item['tax_class'] : null, 'name' => $item['name'], 'product_id' => ! empty( $product->variation_id ) ? $product->parent_id : $product_id, 'variation_id' => ! empty( $product->variation_id ) ? $product_id : 0, 'sku' => $product_sku, 'canonical_url' => get_permalink( $product_id ), 'image_url' => wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'thumbnail', true )[0], 'meta' => $item_meta, ); } /** * Getting shipping. */ foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) { $order_data['shipping_lines'][] = array( 'id' => $shipping_item_id, 'method_id' => $shipping_item['method_id'], 'method_title' => $shipping_item['name'], 'total' => wc_format_decimal( $shipping_item['cost'], $dp ), ); } /** Getting taxes. */ foreach ( $order->get_tax_totals() as $tax_code => $tax ) { $order_data['tax_lines'][] = array( 'id' => $tax->id, 'rate_id' => $tax->rate_id, 'code' => $tax_code, 'title' => $tax->label, 'total' => wc_format_decimal( $tax->amount, $dp ), 'compound' => (bool) $tax->is_compound, ); } /** * Getting fees. */ foreach ( $order->get_fees() as $fee_item_id => $fee_item ) { $order_data['fee_lines'][] = array( 'id' => $fee_item_id, 'title' => $fee_item['name'], 'tax_class' => ( ! empty( $fee_item['tax_class'] ) ) ? $fee_item['tax_class'] : null, 'total' => wc_format_decimal( $order->get_line_total( $fee_item ), $dp ), 'total_tax' => wc_format_decimal( $order->get_line_tax( $fee_item ), $dp ), ); } foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) { $order_data['coupon_lines'][] = array( 'id' => $coupon_item_id, 'code' => $coupon_item['name'], 'amount' => wc_format_decimal( $coupon_item['discount_amount'], $dp ), ); } return $order_data; } /** * Sends order data to Plugins-Dashboard. * * @param string $fields Order details. * * @return mixed */ function ei_send_order_data( $fields ) { $boundary = md5( time() ); $headers = array( 'ContentType' => 'multipart/form-data;boundary=' . $boundary ); $response = EKOMI_INTEGRATION\ei_do_curl( EKOMI_INTEGRATION\URL_TO_SEND_DATA, 'PUT', $fields, $headers ); return $response; }