* @since 1.0 */ public function __construct( $id = 0 ) { $post = $id ? get_post( $id ) : null; if ( $post && $post->post_type == SOFTOBA_EXCHANGE_POST_TYPE ) { $order_id = get_post_meta( $post->ID, 'softoba_ex_order_id', true ); $order = wc_get_order( $order_id ); if ( $order ) { $this->ID = $post->ID; $this->status = $post->post_status; $this->title = $post->post_title; $this->order_id = $order_id; $this->product_id = get_post_meta( $post->ID, 'softoba_ex_product_id', true ); $this->products_data = get_post_meta( $post->ID, 'softoba_ex_products', true ); $this->variation_id = get_post_meta( $post->ID, 'softoba_ex_variation_id', true ); $this->order_variation_id= get_post_meta( $post->ID, 'softoba_ex_order_variation_id', true ); $this->item_id = get_post_meta( $post->ID, 'softoba_ex_item_id', true ); $this->item_value = get_post_meta( $post->ID, 'softoba_ex_item_value', true ); $this->item_total = get_post_meta( $post->ID, 'softoba_ex_item_total', true ); $this->item_tax_data = get_post_meta( $post->ID, 'softoba_ex_item_tax_data', true ); $this->tax_value = get_post_meta( $post->ID, 'softoba_ex_tax_value', true ); $this->tax_total = get_post_meta( $post->ID, 'softoba_ex_tax_total', true ); $this->qty = get_post_meta( $post->ID, 'softoba_ex_qty', true ); $this->qty_total = get_post_meta( $post->ID, 'softoba_ex_qty_total', true ); $this->item_refund_total = get_post_meta( $post->ID, 'softoba_ex_item_refund_total', true ); $this->tax_refund_total = get_post_meta( $post->ID, 'softoba_ex_tax_refund_total', true ); $this->refund_total = get_post_meta( $post->ID, 'softoba_ex_refund_total', true ); $this->refund_id = get_post_meta( $post->ID, 'softoba_ex_refund_id', true ); $this->refunded_amount = get_post_meta( $post->ID, 'softoba_ex_refunded', true ); $this->coupon_id = get_post_meta( $post->ID, 'softoba_ex_coupon_id', true ); $this->is_closed = get_post_meta( $post->ID, 'softoba_ex_is_closed', true ); $this->customer_id = get_post_meta( $post->ID, 'softoba_ex_customer_id', true ); $this->billing_email = get_post_meta( $post->ID, 'softoba_ex_billing_email', true ); $this->exchange_fee = get_post_meta( $post->ID, 'softoba_exchange_fee', true ); $this->unique_id = get_post_meta( $post->ID, 'ex_unique_id', true ); $this->price = get_post_meta( $post->ID, 'exchange_price', true ); } } } /** * Save the current object */ public function save() { // Create post object args $args = array( 'post_title' => 'Exchange Request', 'post_type' => SOFTOBA_EXCHANGE_POST_TYPE, ); if ( $this->ID == 0 ) { $args['post_status'] = 'softoba-new'; $args['post_author'] = $this->customer_id; $this->ID = wp_insert_post( $args ); } else { $args['ID'] = $this->ID; $args['post_status'] = $this->status; $this->ID = wp_update_post( $args ); } wp_update_post( array( 'ID' => $this->ID, 'post_title' => sprintf( esc_html__( 'Exchange request #%d', 'astra' ), $this->order_id ) ) ); $this->unique_id = mt_rand( 1, time() ); $order = wc_get_order( $this->order_id ); $this->customer_id = $order->get_user_id(); $this->billing_email = $order->get_billing_email(); update_post_meta( $this->ID, 'softoba_ex_order_id', $this->order_id ); update_post_meta( $this->ID, 'softoba_ex_products', $this->products_data ); update_post_meta( $this->ID, 'softoba_ex_product_id', $this->product_id ); update_post_meta( $this->ID, 'softoba_ex_variation_id', $this->variation_id ); update_post_meta( $this->ID, 'softoba_ex_order_variation_id', $this->order_variation_id ); update_post_meta( $this->ID, 'softoba_ex_item_id', $this->item_id ); update_post_meta( $this->ID, 'softoba_ex_item_value', $this->item_value ); update_post_meta( $this->ID, 'softoba_ex_item_total', $this->item_total ); update_post_meta( $this->ID, 'softoba_ex_item_tax_data', $this->item_tax_data ); update_post_meta( $this->ID, 'softoba_ex_tax_value', $this->tax_value ); update_post_meta( $this->ID, 'softoba_ex_tax_total', $this->tax_total ); update_post_meta( $this->ID, 'softoba_ex_qty', $this->qty ); update_post_meta( $this->ID, 'softoba_ex_qty_total', $this->qty_total ); update_post_meta( $this->ID, 'softoba_ex_item_refund_total', $this->item_refund_total ); update_post_meta( $this->ID, 'softoba_ex_tax_refund_total', $this->tax_refund_total ); update_post_meta( $this->ID, 'softoba_ex_refund_total', $this->refund_total ); update_post_meta( $this->ID, 'softoba_ex_refund_id', $this->refund_id ); update_post_meta( $this->ID, 'softoba_ex_refunded', $this->refunded_amount ); update_post_meta( $this->ID, 'softoba_ex_customer_id', $this->customer_id ); update_post_meta( $this->ID, 'softoba_ex_billing_email', $this->billing_email ); update_post_meta( $this->ID, 'softoba_ex_is_closed', $this->is_closed ); update_post_meta( $this->ID, 'softoba_ex_coupon_id', $this->coupon_id ); update_post_meta( $this->ID, 'softoba_exchange_fee', $this->exchange_fee ); update_post_meta( $this->ID, 'ex_unique_id', $this->unique_id ); update_post_meta( $this->ID, 'exchange_price', $this->price ); $requests = softoba_get_prop( $order, 'softoba_exchange_requests', true ); if ( $requests && is_array( $requests ) ) { if ( ! in_array( $this->ID, $requests ) ) { $requests[] = $this->ID; softoba_save_prop( $order, 'softoba_exchange_requests', $requests ); } } else if ( empty( $requests ) ) { $requests = array( $this->ID ); softoba_save_prop( $order, 'softoba_exchange_requests', $requests ); } if( $this->status === 'softoba-approved' ) { $original_order_id = $this->order_id; $exchange_details = get_post_meta( $original_order_id, 'sobaoba_exchange_products', true ); // if ( isset( $exchange_details ) && ! empty( $exchange_details ) ) { // if ( 'pending' == $exchange_details['status'] ) { // $exchange_details['status'] = 'complete'; // $exchange_details['approve'] = date( 'd-m-Y' ); // } // } $order_detail = wc_get_order( $original_order_id ); // $includeTax = isset( $order_detail->prices_include_tax ) ? $order_detail->prices_include_tax : false; $user_id = $order_detail->user_id; if( empty( $user_id ) ) { $user_id = 0; } $order_data = array( 'post_name' => 'order-' . date( 'M-d-Y-hi-a' ), // 'order-jun-19-2014-0648-pm' 'post_type' => 'shop_order', 'post_title' => 'Order – ' . date( 'F d, Y @ h:i A' ), // 'June 19, 2014 @ 07:19 PM' 'post_status' => 'wc-processing', 'ping_status' => 'closed', 'post_excerpt' => 'requested', 'post_author' => 1, 'post_password' => uniqid( 'order_' ), 'post_date' => date( 'Y-m-d H:i:s e' ), 'comment_status' => 'open', ); $new_order_id = wp_insert_post( $order_data, true ); $softoba_dis_tot = 0; update_post_meta( $new_order_id, 'softoba_exchange_old_order_id', $original_order_id ); update_post_meta( $original_order_id, "softoba_exchange_new_order_id", $new_order_id ); update_post_meta( $original_order_id, 'softoba_exchange_status', $softoba_dis_tot ); $ex_fr = ''; foreach ( $order_detail->get_items() as $item_id => $item ) { if ( isset( $exchange_details ) && ! empty( $exchange_details ) ) { foreach ( $exchange_details['products'] as $key => $product_old_data ) { if ( $item['product_id'] == $product_old_data['product_id'] && $item['variation_id'] == $product_old_data['variation_id'] ) { $product = apply_filters( 'woocommerce_order_item_product', $item->get_product(), $item ); $item['qty'] = $item['qty'] - $product_old_data['eqty']; $args['qty'] = $item['qty']; $ex_fr = $ex_fr . $item['name'] . '(SKU : ' . $product->get_sku() . ') x ' . $product_old_data['eqty'] . ' | '; if ( WC()->version < '3.0.0' ) { $order->update_product( $item_id, $product, $args ); } else { wc_update_order_item_meta( $item_id, '_qty', $item['qty'] ); if ( $product->backorders_require_notification() && $product->is_on_backorder( $args['qty'] ) ) { $item->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ) ), $args['qty'] - max( 0, $product->get_stock_quantity() ), true ); } $item_data = $item->get_data(); $price_excluded_tax = wc_get_price_excluding_tax( $product, array( 'qty' => 1 ) ); if ( $item_data['quantity'] < 1 ){ $item_data['quantity'] = 1; } $price_tax_excluded = $item_data['total'] / $item_data['quantity']; $args['subtotal'] = $price_excluded_tax * $args['qty']; $args['total'] = $price_tax_excluded * $args['qty']; $item->set_order_id( $original_order_id ); $item->set_props( $args ); $item->save(); } break; } } } } $order_detail->calculate_totals(); $order_detail->update_status( 'wc-completed' ); $order = (object) $order_detail->get_address( 'shipping' ); $oont_governorate = get_post_meta( $original_order_id, 'oont_governorate', true ); $oont_area = get_post_meta( $original_order_id, 'oont_area', true ); $oont_block = get_post_meta( $original_order_id, 'oont_block', true ); $oont_avenue = get_post_meta( $original_order_id, 'oont_avenue', true ); $oont_house_number = get_post_meta( $original_order_id, 'oont_house_number', true ); // Shipping info. update_post_meta( $new_order_id, '_customer_user', $user_id ); update_post_meta( $new_order_id, '_shipping_address_1', $order->address_1 ); update_post_meta( $new_order_id, '_shipping_address_2', $order->address_2 ); update_post_meta( $new_order_id, '_shipping_city', $order->city ); update_post_meta( $new_order_id, '_shipping_state', $order->state ); update_post_meta( $new_order_id, '_shipping_postcode', $order->postcode ); update_post_meta( $new_order_id, '_shipping_country', $order->country ); update_post_meta( $new_order_id, '_shipping_company', $order->company ); update_post_meta( $new_order_id, '_shipping_first_name', $order->first_name ); update_post_meta( $new_order_id, '_shipping_last_name', $order->last_name ); // billing info. $order_detail = wc_get_order( $original_order_id ); $order_detail->calculate_totals(); $order = (object) $order_detail->get_address( 'billing' ); add_post_meta( $new_order_id, '_billing_first_name', $order->first_name, true ); add_post_meta( $new_order_id, '_billing_last_name', $order->last_name, true ); add_post_meta( $new_order_id, '_billing_company', $order->company, true ); add_post_meta( $new_order_id, '_billing_address_1', $order->address_1, true ); add_post_meta( $new_order_id, '_billing_address_2', $order->address_2, true ); add_post_meta( $new_order_id, '_billing_city', $order->city, true ); add_post_meta( $new_order_id, '_billing_state', $order->state, true ); add_post_meta( $new_order_id, '_billing_postcode', $order->postcode, true ); add_post_meta( $new_order_id, '_billing_country', $order->country, true ); add_post_meta( $new_order_id, '_billing_email', $order->email, true ); add_post_meta( $new_order_id, '_billing_phone', $order->phone, true ); add_post_meta( $new_order_id, 'oont_governorate', $oont_governorate, true ); add_post_meta( $new_order_id, 'oont_area', $oont_area, true ); add_post_meta( $new_order_id, 'oont_block', $oont_block, true ); add_post_meta( $new_order_id, 'oont_avenue', $oont_avenue, true ); add_post_meta( $new_order_id, 'oont_house_number', $oont_house_number, true ); // $exchanged_products $new_order = wc_get_order( $new_order_id ); if ( WC()->version >= '3.0.0' ) { if ( ! $new_order->get_order_key() ) { update_post_meta( $new_order_id, '_order_key', 'wc-' . uniqid( 'order_' ) ); } } $ex_to = ''; if ( isset( $exchange_details ) && ! empty( $exchange_details ) ) { foreach ( $exchange_details['products'] as $key => $product_new_data ) { if( isset( $product_new_data['ex_variation_id'] ) ) { $product = wc_get_product( $product_new_data['ex_variation_id'] ); $variation_product = new WC_Product_Variation( $product_new_data['ex_variation_id'] ); $variation_attributes = $variation_product->get_variation_attributes(); if ( isset( $product_new_data['ex_variation_id'] ) && ! empty( $product_new_data['ex_variation_id'] ) ) { $variation_attributes = $product_new_data['ex_variation_id']; } $variation_product_price = wc_get_price_excluding_tax( $variation_product, array( 'qty' => 1 ) ); $variation_att['variation'] = $variation_attributes; $variation_att['totals']['subtotal'] = $product_new_data['eqty'] * $variation_product_price; $variation_att['totals']['total'] = $product_new_data['eqty'] * $variation_product_price; $item_id = $new_order->add_product( $variation_product, $product_new_data['eqty'], $variation_att ); if ( $product->managing_stock() ) { $qty = $product_new_data['eqty']; $new_stock = $product->reduce_stock( $qty ); } $ex_to = $ex_to . $product->get_name() . '(SKU : ' . $product->get_sku() . ') x ' . $product_new_data['eqty'] . ' | '; }else{ $product = wc_get_product( $product_new_data['product_id'] ); $item_id = $new_order->add_product( $product, $product_new_data['eqty'] ); if ( $product->managing_stock() ) { $qty = $exchanged_product['eqty']; $new_stock = $product->reduce_stock( $qty ); } $ex_to = $ex_to . $product->get_name() . '(SKU : ' . $product->get_sku() . ') x ' . $product_new_data['eqty'] . ' | '; } } } // $ex_fr = trim( $ex_fr, '| ' ); // $ex_to = trim( $ex_to, '| ' ); // $exchange_note = __( 'Product Exchange Request from', 'astra' ) . ' { ' . $ex_fr . ' } ' . __( 'to', 'astra' ) . ' { ' . $ex_to . ' } ' . __( ' has been approved.', 'astra' ); $exchange_note = __("Product Exchange Request is approved.. new order id is #$new_order_id", 'astra'); $exchange_new_note = __("Product Exchange Request is processing.. old order id is #$original_order_id", 'astra'); wc_get_order( $original_order_id )->add_order_note( $exchange_note ); wc_get_order( $new_order_id )->add_order_note( $exchange_new_note ); $discount = 0; $manage_stock = 'yes'; $order_total = $new_order->calculate_totals(); $new_order->set_total( $order_total, 'total' ); if ( 0 == $order_total ) { $new_order->update_status( 'wc-processing' ); } if ( 'yes' == $manage_stock ) { if ( is_array( $exchange_details ) && ! empty( $exchange_details ) ) { foreach ( $exchange_details['products'] as $key => $requested_data ) { if ( $requested_data['variation_id'] > 0 ) { $product = wc_get_product( $requested_data['variation_id'] ); } else { $product = wc_get_product( $requested_data['product_id'] ); } if ( $product->managing_stock() ) { $avaliable_qty = $requested_data['eqty']; if ( WC()->version < '3.0.0' ) { $product->set_stock( $avaliable_qty, 'add' ); } else { if ( $requested_data['variation_id'] > 0 ) { $total_stock = get_post_meta( $requested_data['variation_id'], '_stock', true ); $total_stock = $total_stock + $avaliable_qty; wc_update_product_stock( $requested_data['variation_id'], $total_stock, 'set' ); } else { $total_stock = get_post_meta( $requested_data['product_id'], '_stock', true ); $total_stock = $total_stock + $avaliable_qty; wc_update_product_stock( $requested_data['product_id'], $total_stock, 'set' ); } } } } } } } return $this->ID; } public function get_id() { return $this->ID; } public function get_view_request_url() { $view_request_url = wc_get_endpoint_url( 'view-request', $this->ID, wc_get_page_permalink( 'myaccount' ) ); return apply_filters( 'softoba_get_view_request_url', $view_request_url, $this ); } public function get_date() { $post = get_post( $this->ID ); return $post->post_date; } public function get_customer_link_legacy() { $customer = get_userdata( $this->customer_id ); $order = wc_get_order( $this->order_id ); if ( ! empty( $customer ) ) { $url = admin_url( 'user-edit.php?user_id=' . absint( $customer->ID ) ); $username = ''; if ( $customer->first_name || $customer->last_name ) { $username .= esc_html( sprintf( esc_html_x( '%1$s %2$s', 'full name', 'astra' ), ucfirst( $customer->first_name ), ucfirst( $customer->last_name ) ) ); } else { $username .= esc_html( ucfirst( $customer->display_name ) ); } $username .= ''; } else { if ( $order->billing_first_name || $order->billing_last_name ) { $username = trim( sprintf( esc_html_x( '%1$s %2$s', 'full name', 'astra' ), $order->billing_first_name, $order->billing_last_name ) ); } else if ( $order->billing_company ) { $username = trim( $order->billing_company ); } else { $username = esc_html__( 'Guest', 'astra' ); } } return $username; } public function get_customer_link() { $customer = get_userdata( $this->customer_id ); $order = wc_get_order( $this->order_id ); if ( ! $customer ) { return false; } if ( $order->get_customer_id() ) { $user = get_user_by( 'id', $order->get_customer_id() ); $url = admin_url( 'user-edit.php?user_id=' . absint( $order->get_customer_id() ) ); $username = ''; $username .= esc_html( ucwords( $user->display_name ) ); $username .= ''; } elseif ( $order->get_billing_first_name() || $order->get_billing_last_name() ) { $username = trim( sprintf( esc_html_x( '%1$s %2$s', 'full name', 'astra' ), $order->get_billing_first_name(), $order->get_billing_last_name() ) ); } elseif ( $order->get_billing_company() ) { $username = trim( $order->get_billing_company() ); } else { $username = esc_html__( 'Guest', 'astra' ); } return $username; } public function exists() { return $this->ID; } public function set_approved() { $this->status = 'softoba-approved'; $this->save(); WC()->mailer(); // do_action( 'softoba_send_approved_user', $this->ID ); } public function set_rejected() { $this->status = 'softoba-rejected'; $this->save(); WC()->mailer(); do_action( 'softoba_send_rejected_user', $this->ID ); } public function set_coupon_offered() { $this->status = 'softoba-coupon'; $this->save(); WC()->mailer(); do_action( 'softoba_send_coupon_user', $this->ID ); } public function set_processing() { $this->status = 'softoba-processing'; $this->save(); WC()->mailer(); do_action( 'softoba_send_processing_user', $this->ID ); } public function set_on_hold() { $this->status = 'softoba-on-hold'; $this->save(); WC()->mailer(); do_action( 'softoba_send_on_hold_user', $this->ID ); } public function close_request() { $this->is_closed = true; update_post_meta( $this->ID, 'softoba_ex_is_closed', $this->is_closed ); } } }