get_taxes() : array(); $order_items = ( is_object( $order ) && is_callable( array( $order, 'get_items' ) ) ) ? $order->get_items( 'coupon' ) : array(); $i = 1; if ( ! empty( $order_items ) ) { foreach ( $order_items as $item_id => $item ) { $order_discount_amount = $sc_refunded_discount = $sc_refunded_discount_tax = $order_discount_tax_amount = 0; // phpcs:ignore $coupon_post_obj = ( function_exists( 'wpcom_vip_get_page_by_title' ) ) ? wpcom_vip_get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' ) : get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );// phpcs:ignore $coupon_id = isset( $coupon_post_obj->ID ) ? $coupon_post_obj->ID : ''; $coupon_title = isset( $coupon_post_obj->post_title ) ? $coupon_post_obj->post_title : ''; $coupon = new WC_Coupon( $coupon_id ); if ( is_a( $coupon, 'WC_Coupon' ) ) { if ( $coupon->is_type( 'smart_coupon' ) ) { if ( 1 === $i ) { ?> get_order_item_meta( $item_id, 'discount_amount', true ); $sc_refunded_discount = $this->get_order_item_meta( $item_id, 'sc_refunded_discount', true ); $sc_refunded_discount_tax = $this->get_order_item_meta( $item_id, 'sc_refunded_discount_tax', true ); $order_discount_tax_amount = $this->get_order_item_meta( $item_id, 'discount_amount_tax', true ); } ?>
   
get_order_item_meta( $item_id, 'discount_amount', true ); $refunded_amount = $this->get_order_item_meta( $item_id, 'sc_refunded_discount', true ); $refunded_tax_amount = $this->get_order_item_meta( $item_id, 'sc_refunded_discount_tax', true ); $order_discount_tax_amount = $this->get_order_item_meta( $item_id, 'discount_amount_tax', true ); } if ( floatval( $order_discount_amount ) === floatval( $refunded_amount ) && floatval( $order_discount_tax_amount ) === floatval( $refunded_tax_amount ) ) { continue; } if ( $refunded_amount ) { $total_refunded = $refund_amount + $refunded_amount; } if ( $refunded_tax_amount ) { $total_refunded_tax = $order_sc_refund_tax_amount + $refunded_tax_amount; } if ( $order_discount_amount >= $refund_amount && $order_discount_amount >= $total_refunded && $order_discount_tax_amount >= $order_sc_refund_tax_amount && $order_discount_tax_amount >= $total_refunded_tax && ! empty( $smart_coupon_id ) ) { $coupon = new WC_Coupon( $smart_coupon_id ); if ( is_a( $coupon, 'WC_Coupon' ) ) { if ( $this->is_wc_gte_30() ) { $discount_type = ( is_callable( array( $coupon, 'get_discount_type' ) ) ) ? $coupon->get_discount_type() : ''; } else { $discount_type = ( ! empty( $coupon->discount_type ) ) ? $coupon->discount_type : ''; } $coupon_amount = $this->get_amount( $coupon, true, $order ); if ( 'smart_coupon' === $discount_type && is_numeric( $refund_amount ) && is_numeric( $order_sc_refund_tax_amount ) ) { $amount = $coupon_amount + $refund_amount + $order_sc_refund_tax_amount; $this->update_post_meta( $smart_coupon_id, 'coupon_amount', $amount, true, $order ); $user = ( function_exists( 'get_current_user_id' ) ) ? get_current_user_id() : 0; $local_time = ( function_exists( 'current_datetime' ) ) ? current_datetime() : ''; $get_timestamp = ( is_object( $local_time ) && is_callable( array( $local_time, 'getTimestamp' ) ) ) ? $local_time->getTimestamp() : ''; $get_offset = ( is_object( $local_time ) && is_callable( array( $local_time, 'getOffset' ) ) ) ? $local_time->getOffset() : ''; $current_time_stamp = $get_timestamp + $get_offset; if ( 0 < $total_refunded ) { $refund_amount = $total_refunded; } if ( 0 < $total_refunded_tax ) { $order_sc_refund_tax_amount = $total_refunded_tax; } if ( is_callable( array( $this, 'update_order_item_meta' ) ) ) { $this->update_order_item_meta( $item_id, 'sc_refunded_discount_tax', $order_sc_refund_tax_amount ); $this->update_order_item_meta( $item_id, 'sc_refunded_discount', $refund_amount ); $this->update_order_item_meta( $item_id, 'sc_refunded_user_id', $user ); $this->update_order_item_meta( $item_id, 'sc_refunded_timestamp', $current_time_stamp ); $this->update_order_item_meta( $item_id, 'sc_refunded_coupon_id', $smart_coupon_id ); $message = __( 'Successfully updated store credit refund details.', 'woocommerce-smart-coupons' ); } else { $message = __( 'Failed to update store credit refund details.', 'woocommerce-smart-coupons' ); $woocommerce_smart_coupon->log( 'notice', $message . ' ' . __FILE__ . ' ' . __LINE__ ); } $response['message'] = $message; } } } } } wp_send_json_success( $response ); } else { $response['message'] = __( 'Nonce verification failed for action "wc_sc_refund_store_credit".', 'woocommerce-smart-coupons' ); wp_send_json_error( $response ); } } /** * Render refund store credit UI in order page * * @param int $order_id order id. * @return void revoke refund html */ public function render_refunded_store_credits_details( $order_id ) { ?> get_items( 'coupon' ) : array(); if ( ! empty( $order_items ) ) { foreach ( $order_items as $item_id => $item ) { $sc_refunded_discount = $sc_refunded_discount_tax = $sc_refunded_user = $sc_refunded_timestamp = 0; // phpcs:ignore $coupon_post_obj = ( function_exists( 'wpcom_vip_get_page_by_title' ) ) ? wpcom_vip_get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' ) : get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );// phpcs:ignore $coupon_id = isset( $coupon_post_obj->ID ) ? $coupon_post_obj->ID : ''; $coupon_title = isset( $coupon_post_obj->post_title ) ? $coupon_post_obj->post_title : ''; $coupon = new WC_Coupon( $coupon_id ); if ( is_callable( array( $this, 'get_order_item_meta' ) ) ) { $sc_refunded_discount = $this->get_order_item_meta( $item_id, 'sc_refunded_discount', true ); $sc_refunded_discount_tax = $this->get_order_item_meta( $item_id, 'sc_refunded_discount_tax', true ); $sc_refunded_user = $this->get_order_item_meta( $item_id, 'sc_refunded_user_id', true ); $sc_refunded_timestamp = $this->get_order_item_meta( $item_id, 'sc_refunded_timestamp', true ); } if ( empty( $sc_refunded_timestamp ) ) { $sc_refunded_timestamp = time() + $this->wc_timezone_offset(); } $sc_refunded_discount = empty( $sc_refunded_discount ) ? 0 : $sc_refunded_discount; $sc_refunded_discount_tax = empty( $sc_refunded_discount_tax ) ? 0 : $sc_refunded_discount_tax; if ( is_a( $coupon, 'WC_Coupon' ) ) { if ( $coupon->is_type( 'smart_coupon' ) && ( ! empty( $sc_refunded_discount ) || ! empty( $sc_refunded_discount_tax ) ) ) { $who_refunded = new WP_User( $sc_refunded_user ); $refunder_id = isset( $who_refunded->ID ) ? $who_refunded->ID : 0; $refunder_name = isset( $who_refunded->display_name ) ? $who_refunded->display_name : ''; ?>
exists() ) { printf( /* translators: 1: refund id 2: refund date 3: username */ esc_html__( 'Refund %1$s - %2$s by %3$s', 'woocommerce-smart-coupons' ), esc_html( 'Store Credit Coupon - ' . $coupon_title ), esc_html( $this->format_date( $sc_refunded_timestamp ) ), sprintf( '%2$s', /* translators: 1: ID who refunded */ sprintf( esc_attr__( 'ID: %d', 'woocommerce-smart-coupons' ), absint( $refunder_id ) ), esc_html( $refunder_name ) ) ); } else { printf( /* translators: 1: refund id 2: refund date */ esc_html__( 'Refund %1$s - %2$s', 'woocommerce-smart-coupons' ), esc_html( 'Store Credit Coupon -' . $coupon_title ), esc_html( $this->format_date( $sc_refunded_timestamp ) ) ); } ?>    
get_order_item_meta( $wc_sc_refunded_id, 'discount_amount', true ); $order_discount_tax_amount = $this->get_order_item_meta( $wc_sc_refunded_id, 'discount_amount_tax', true ); if ( $order_discount_amount >= $refunded_amount && $order_discount_tax_amount >= $refunded_amount_tax && ! empty( $smart_coupon_id ) ) { $coupon = new WC_Coupon( $smart_coupon_id ); if ( is_a( $coupon, 'WC_Coupon' ) ) { if ( $this->is_wc_gte_30() ) { $discount_type = ( is_callable( array( $coupon, 'get_discount_type' ) ) ) ? $coupon->get_discount_type() : ''; } else { $discount_type = ( ! empty( $coupon->discount_type ) ) ? $coupon->discount_type : ''; } $coupon_amount = $this->get_amount( $coupon, true, $order ); if ( 'smart_coupon' === $discount_type ) { if ( ! is_numeric( $refunded_amount ) ) { $refunded_amount = 0; } if ( ! is_numeric( $refunded_amount_tax ) ) { $refunded_amount_tax = 0; } $refund_amount = $refunded_amount + $refunded_amount_tax; $amount = $coupon_amount - $refund_amount; $this->update_post_meta( $smart_coupon_id, 'coupon_amount', $amount, true, $order ); $user = ( function_exists( 'get_current_user_id' ) ) ? get_current_user_id() : 0; $local_time = ( function_exists( 'current_datetime' ) ) ? current_datetime() : ''; $get_timestamp = ( is_object( $local_time ) && is_callable( array( $local_time, 'getTimestamp' ) ) ) ? $local_time->getTimestamp() : 0; $get_offset = ( is_object( $local_time ) && is_callable( array( $local_time, 'getOffset' ) ) ) ? $local_time->getOffset() : 0; $current_time_stamp = $get_timestamp + $get_offset; if ( is_callable( array( $this, 'update_order_item_meta' ) ) ) { $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_revoke_refunded_discount', $refunded_amount, true ); $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_revoke_refunded_discount_tax', $refunded_amount_tax, true ); $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_revoke_refunded_user_id', $user ); $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_revoke_refunded_timestamp', $current_time_stamp ); $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_revoke_refunded_coupon_id', $smart_coupon_id ); $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_refunded_discount', 0 ); $this->update_order_item_meta( $wc_sc_refunded_id, 'sc_refunded_discount_tax', 0 ); } } } } } wp_send_json_success(); } else { wp_send_json_error(); } } /** * Change order status when fully refunded. * * @param string $status order status. * @param number $order_id order id. * @param number $refund_id refund id. * @return false|mixed order status */ public function update_fully_refunded_status( $status, $order_id, $refund_id ) { $order = wc_get_order( $order_id ); $order_items = ( is_object( $order ) && is_callable( array( $order, 'get_items' ) ) ) ? $order->get_items( 'coupon' ) : array(); if ( ! empty( $order_items ) ) { foreach ( $order_items as $item_id => $item ) { $sc_refunded_discount = $sc_refunded_discount_tax = $order_discount_amount = $order_discount_tax_amount = 0; // phpcs:ignore $coupon_post_obj = ( function_exists( 'wpcom_vip_get_page_by_title' ) ) ? wpcom_vip_get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' ) : get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );// phpcs:ignore $coupon_id = isset( $coupon_post_obj->ID ) ? $coupon_post_obj->ID : ''; $coupon = new WC_Coupon( $coupon_id ); if ( is_callable( array( $this, 'get_order_item_meta' ) ) ) { $sc_refunded_discount = $this->get_order_item_meta( $item_id, 'sc_refunded_discount', true ); $sc_refunded_discount_tax = $this->get_order_item_meta( $item_id, 'sc_refunded_discount_tax', true ); $order_discount_amount = $this->get_order_item_meta( $item_id, 'discount_amount', true ); $order_discount_tax_amount = $this->get_order_item_meta( $item_id, 'discount_amount_tax', true ); } if ( is_a( $coupon, 'WC_Coupon' ) ) { if ( $coupon->is_type( 'smart_coupon' ) ) { $get_order_total = ( is_object( $order ) && is_callable( array( $order, 'get_total' ) ) ) ? $order->get_total() : 0; $refunded_order_total = ( is_object( $order ) && is_callable( array( $order, 'get_total_refunded' ) ) ) ? $order->get_total_refunded() : 0; if ( empty( $get_order_total ) && empty( $sc_refunded_discount_tax ) && empty( $order_discount_tax_amount ) && $order_discount_amount !== $sc_refunded_discount ) { return false; } elseif ( empty( $get_order_total ) && ! empty( $sc_refunded_discount_tax ) && ! empty( $order_discount_tax_amount ) && $order_discount_amount !== $sc_refunded_discount && $sc_refunded_discount_tax !== $order_discount_tax_amount ) { return false; } elseif ( ! empty( $get_order_total ) && empty( $sc_refunded_discount_tax ) && empty( $order_discount_tax_amount ) && $get_order_total === $refunded_order_total && $order_discount_amount !== $sc_refunded_discount ) { return false; } elseif ( ! empty( $get_order_total ) && ! empty( $sc_refunded_discount_tax ) && ! empty( $order_discount_tax_amount ) && $get_order_total === $refunded_order_total && $order_discount_amount !== $sc_refunded_discount && $sc_refunded_discount_tax !== $order_discount_tax_amount ) { return false; } elseif ( empty( $sc_refunded_discount_tax ) && ! empty( $order_discount_tax_amount ) ) { return false; } } } } } return $status; } } } WC_SC_Coupon_Refund_Process::get_instance();