is_callable( $order, 'get_total' ) ? $order->get_total() : 0; $order_items = ( is_object( $order ) && $this->is_callable( $order, 'get_items' ) ) ? $order->get_items( 'coupon' ) : array(); $tax_data = ( function_exists( 'wc_tax_enabled' ) && wc_tax_enabled() && is_object( $order ) && $this->is_callable( $order, 'get_taxes' ) ) ? $order->get_taxes() : array(); if ( ! class_exists( 'WC_SC_Order_Fields' ) ) { include_once 'class-wc-sc-order-fields.php'; } $sc_order_fields = WC_SC_Order_Fields::get_instance(); $total_credit_used_in_order = $sc_order_fields->get_total_credit_used_in_order( $order ); $i = 1; if ( ! empty( $order_items ) ) { $is_apply_before_tax = get_option( 'woocommerce_smart_coupon_apply_before_tax', 'no' ); $is_readonly = ( 'yes' !== $is_apply_before_tax ) ? true : false; $allow_custom_refund_amount = apply_filters( 'wc_sc_allow_custom_refund_amount', ( false === $is_readonly ), array( 'source' => $this, 'order_obj' => $order, ) ); $is_old_sc_order = $this->is_old_sc_order( $order_id ); $item_titles = array_map( function( $item ) { return ( $this->is_callable( $item, 'get_name' ) ) ? $item->get_name() : ''; }, $order_items ); $posts = $this->get_post_by_title( $item_titles, OBJECT, 'shop_coupon' ); 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_code = ( $this->is_callable( $item, 'get_name' ) ) ? $item->get_name() : ''; $sanitized_coupon_code = sanitize_title( $coupon_code ); // The generated string will be checked in an array key to locate post object. $coupon_post_obj = ( ! empty( $posts[ $sanitized_coupon_code ] ) ) ? $posts[ $sanitized_coupon_code ] : null; $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 = (float) $this->get_order_item_meta( $item_id, 'sc_refunded_discount', true ); $sc_refunded_discount_tax = (float) $this->get_order_item_meta( $item_id, 'sc_refunded_discount_tax', true ); $order_discount_tax_amount = (float) $this->get_order_item_meta( $item_id, 'discount_amount_tax', true ); } ?>
   
1 ) { ?> is_old_sc_order( $order_id ) && $total_credit_used_in_order > 0 ) { ?> 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 ) { global $store_credit_label; ?> get_items( 'coupon' ) : array(); $tax_data = ( function_exists( 'wc_tax_enabled' ) && wc_tax_enabled() && is_object( $order ) && is_callable( array( $order, 'get_taxes' ) ) ) ? $order->get_taxes() : array(); if ( ! empty( $order_items ) ) { $item_titles = array_map( function( $item ) { return $item->get_name(); }, $order_items ); $posts = $this->get_post_by_title( $item_titles, OBJECT, 'shop_coupon' ); foreach ( $order_items as $item_id => $item ) { $sc_refunded_discount = $sc_refunded_discount_tax = $sc_refunded_user = $sc_refunded_timestamp = 0; // phpcs:ignore $coupon_code = ( $this->is_callable( $item, 'get_name' ) ) ? $item->get_name() : ''; $sanitized_coupon_code = sanitize_title( $coupon_code ); // The generated string will be checked in an array key to locate post object. $coupon_post_obj = ( ! empty( $posts[ $sanitized_coupon_code ] ) ) ? $posts[ $sanitized_coupon_code ] : null; $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 = (float) $this->get_order_item_meta( $item_id, 'sc_refunded_discount', true ); $sc_refunded_discount_tax = (float) $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' ), sprintf( '%s - %s', ( ! empty( $store_credit_label['singular'] ) ? esc_html( $store_credit_label['singular'] ) : esc_html__( 'Store Credit', 'woocommerce-smart-coupons' ) ), esc_html( $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' ), sprintf( '%s - %s', ( ! empty( $store_credit_label['singular'] ) ? esc_html( $store_credit_label['singular'] ) : esc_html__( 'Store Credit', 'woocommerce-smart-coupons' ) ), esc_html( $coupon_title ) ), esc_html( $this->format_date( $sc_refunded_timestamp ) ) ); } ?>    
1 ) { ?>
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 ) ) { $item_titles = array_map( function( $item ) { return $item->get_name(); }, $order_items ); $posts = $this->get_post_by_title( $item_titles, OBJECT, 'shop_coupon' ); 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_code = ( $this->is_callable( $item, 'get_name' ) ) ? $item->get_name() : ''; $sanitized_coupon_code = sanitize_title( $coupon_code ); // The generated string will be checked in an array key to locate post object. $coupon_post_obj = ( ! empty( $posts[ $sanitized_coupon_code ] ) ) ? $posts[ $sanitized_coupon_code ] : null; $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();