Changeset 2491101
- Timestamp:
- 03/09/2021 11:37:33 PM (5 years ago)
- Location:
- voucherify/trunk
- Files:
-
- 6 added
- 11 edited
-
partials/admin-form.php (modified) (1 diff)
-
partials/admin-order-item-partial-refund-rollback.php (added)
-
readme.txt (modified) (2 diffs)
-
src/class-voucherify-admin-settings.php (modified) (2 diffs)
-
src/class-voucherify-cart-decorator.php (modified) (1 diff)
-
src/class-voucherify-client-extension.php (added)
-
src/class-voucherify-order-placing-session.php (added)
-
src/class-voucherify-redemption-service.php (modified) (8 diffs)
-
src/class-voucherify-refund-order-listener.php (added)
-
src/class-voucherify-remove-coupon-service.php (added)
-
src/class-voucherify-save-order-listener.php (modified) (7 diffs)
-
src/class-voucherify-valid-promotions-fetcher.php (modified) (1 diff)
-
src/class-voucherify-validation-service.php (modified) (15 diffs)
-
src/class-voucherify-voucher-amount-calculator.php (added)
-
src/class-voucherify.php (modified) (18 diffs)
-
src/functions.php (modified) (6 diffs)
-
voucherify.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
voucherify/trunk/partials/admin-form.php
r1819737 r2491101 48 48 class="regular-text code"></td> 49 49 </tr> 50 <tr> 51 <th scope="row" colspan="2"> 52 <input type="checkbox" name="voucherify_rollback_enabled" id="voucherify_rollback_enabled" 53 value="yes" <?php checked( get_option( 'voucherify_rollback_enabled', 'yes' ), 'yes', true ); ?>/> 54 <label for="voucherify_rollback_enabled"> 55 <?php _e( 'Voucherify rollback enabled', 'voucherify' ); ?> 56 </label> 57 </th> 58 </tr> 59 <tr> 60 <th scope="row"> 61 <label for="voucherify_lock_ttl"> 62 <?php _e( 'Length of coupon validity time window after application (days)', 63 'voucherify' ); ?> 64 </label> 65 </th> 66 <td><input name="voucherify_lock_ttl" 67 type="number" 68 min="1" 69 id="voucherify_lock_ttl" 70 value="<?php echo esc_attr( get_option( 'voucherify_lock_ttl', 7 ) ); ?>" 71 class="regular-text code"></td> 72 </tr> 50 73 </tbody> 51 74 </table> -
voucherify/trunk/readme.txt
r2361501 r2491101 7 7 WC tested up to: 4.3.1 8 8 WC requires at least: 3.0.0 9 Stable tag: 2. 0.19 Stable tag: 2.1.0 10 10 11 11 Integrates Voucherify API with woocommerce … … 50 50 == Changelog == 51 51 52 = 2.1.0 - 2021-03-10 = 53 * Improvement: Added support for free shipping coupons 54 * Improvement: Added support for redeem rollback operation 55 * Improvement: Added support for separate discounts per products 56 * Improvement: Changed redeem to lock operation for orders placed by customers 57 * Fix: Sending product ID to the api 58 * Fix: Multiple issues with supporting vouchers in the WC admin panel 59 52 60 = 2.0.1 - 2020-08-14 = 53 61 * Fix: Added SKUs to the API request -
voucherify/trunk/src/class-voucherify-admin-settings.php
r1810660 r2491101 26 26 * Internal helper method to add a setting of given key to the right voucherify options group. 27 27 * 28 * @param $key optionkey28 * @param $key string key 29 29 */ 30 30 private function register_setting( $key ) { … … 39 39 $this->register_setting( 'voucherify_app_id' ); 40 40 $this->register_setting( 'voucherify_app_secret_key' ); 41 $this->register_setting( 'voucherify_rollback_enabled' ); 42 $this->register_setting( 'voucherify_lock_ttl' ); 41 43 } 42 44 -
voucherify/trunk/src/class-voucherify-cart-decorator.php
r2361501 r2491101 64 64 } 65 65 66 if ( ! empty( $item['line_total'] ) ) { 67 $item_data['price'] = intval(100 * ($item['line_total'] + $item['line_tax'])); 68 } 69 66 70 if ( ! empty( $item_data ) ) { 67 71 $items_data[] = $item_data; -
voucherify/trunk/src/class-voucherify-redemption-service.php
r2361501 r2491101 7 7 * Time: 10:15 AM 8 8 */ 9 10 use Voucherify\ClientException; 11 use Voucherify\Redemptions; 9 12 10 13 if ( ! defined( 'ABSPATH' ) ) { … … 17 20 18 21 class Voucherify_Redemption_Service { 19 /** @var \Voucherify\Redemptions $redemptions */22 /** @var Redemptions $redemptions */ 20 23 private $redemptions; 24 25 /** @var Voucherify_Order_Placing_Session $order_placing_session */ 26 private $order_placing_session; 27 21 28 22 29 /** 23 30 * Voucherify_Redemption_Service constructor. 24 31 * 25 * @param \Voucherify\Redemptions $redemptions 32 * @param Redemptions $redemptions 33 * @param Voucherify_Order_Placing_Session $order_placing_session 26 34 */ 27 public function __construct( \Voucherify\Redemptions $redemptions ) { 28 $this->redemptions = $redemptions; 35 public function __construct( Redemptions $redemptions, Voucherify_Order_Placing_Session $order_placing_session ) { 36 $this->redemptions = $redemptions; 37 $this->order_placing_session = $order_placing_session; 29 38 } 30 39 … … 33 42 * 34 43 * @param string $code voucher or promotion code to be redeemed 44 * @param WC_Order $order 35 45 * 36 46 * @return array returns array with properties `redemption_id`, `discount_amount`, `code` (promotion or voucher 37 47 * code) and `type` (whether it's `voucher` or `promotion`) 38 48 * 49 * @throws ClientException if voucherify api could not redeem the voucher. 39 50 * @throws Exception if voucherify api could not redeem the voucher. 40 51 */ 41 public function redeem( $code ) {52 public function redeem( $code, WC_Order $order ) { 42 53 $coupon_code_enhanced = explode( ":", $code ); 43 54 44 55 if ( count( $coupon_code_enhanced ) === 2 && $coupon_code_enhanced[0] === "promotion" ) { 45 return $this->redeem_promotion( $coupon_code_enhanced[1] ); 56 $result = $this->redeem_promotion( $coupon_code_enhanced[1], $order ); 57 } else { 58 $result = $this->redeem_voucher( $code, $order ); 46 59 } 47 60 48 return $ this->redeem_voucher( $code );61 return $result; 49 62 } 50 63 … … 53 66 * 54 67 * @param string $code voucher code to be redeemed 68 * @param WC_Order $order 55 69 * 56 70 * @return array returns array with properties `redemption_id`, `discount_amount`, `code` (voucher code) 57 71 * and `type` (with value = `voucher`) 58 72 * 59 * @throws Exception if voucherify api could not redeem the voucher. 73 * @throws ClientException if voucherify api could not redeem the voucher. 74 * @throws Exception 60 75 */ 61 private function redeem_voucher( $code ) {76 private function redeem_voucher( $code, WC_Order $order ) { 62 77 $context = apply_filters( 'voucherify_redemption_service_redemption_context', 63 vcrf_get_customer_data() + vcrf_get_order_data( ) );78 vcrf_get_customer_data() + vcrf_get_order_data( $order ) + $this->create_session_lock_data( $order, $code ) ); 64 79 65 80 $context = apply_filters( 'voucherify_redemption_service_redemption_voucher_context', $context ); 81 82 $this->add_shipping_to_context($context); 66 83 67 84 $response = apply_filters( 'voucherify_redemption_service_redeem', … … 70 87 $response = apply_filters( 'voucherify_redemption_service_redeem_voucher', $response, $this->redemptions ); 71 88 72 if ( isset( $response->id ) && isset( $response->order->discount_amount ) ) { 89 $coupon_amount = Voucherify_Voucher_Amount_Calculator::calculate_coupon_amount( $response ); 90 91 if ( isset( $response->id ) && isset( $coupon_amount ) ) { 73 92 return apply_filters( 'voucherify_redemption_service_redeem_result', [ 74 93 'redemption_id' => $response->id, 75 94 'code' => $code, 76 95 'type' => 'voucher', 77 'discount_amount' => Voucherify_Tax::calc_applicable_amount( $ response->order->discount_amount )96 'discount_amount' => Voucherify_Tax::calc_applicable_amount( $coupon_amount ) 78 97 ], $response, $this->redemptions, 'voucher' ); 79 98 } … … 91 110 * @param string $code promotion code to be redeemed 92 111 * 112 * @param WC_Order $order 113 * 93 114 * @return array returns array with properties `redemption_id`, `discount_amount`, `code` (promotion code) 94 115 * and `type` (with value = `promotion`) 95 116 * 96 * @throws Exception if voucherify api could not redeem the voucher. 117 * @throws ClientException 118 * @throws Exception 97 119 */ 98 private function redeem_promotion( $code ) {120 private function redeem_promotion( $code, WC_Order $order ) { 99 121 $context = apply_filters( 'voucherify_redemption_service_redemption_context', 100 vcrf_get_customer_data() + vcrf_get_order_data() );122 vcrf_get_customer_data() + vcrf_get_order_data() + $this->create_session_lock_data( $order, $code ) ); 101 123 102 $context = apply_filters( 'voucherify_redemption_service_redemption_promotion_context', $context );103 124 $context = apply_filters( 'voucherify_redemption_service_redemption_promotion_context', $context ); 125 $context = $this->add_shipping_to_context( $context ); 104 126 $response = apply_filters( 'voucherify_redemption_service_redeem', 105 127 $this->redemptions->redeem( [ 'id' => $code ], $context ), $this->redemptions ); … … 108 130 $this->redemptions ); 109 131 110 if ( isset( $response->id ) && isset( $response->order->discount_amount ) ) { 132 $coupon_amount = Voucherify_Voucher_Amount_Calculator::calculate_coupon_amount( $response ); 133 134 if ( isset( $response->id ) && isset( $coupon_amount ) ) { 111 135 return apply_filters( 'voucherify_redemption_service_redeem_result', [ 112 136 'redemption_id' => $response->id, 113 137 'code' => $code, 114 138 'type' => 'promotion', 115 'discount_amount' => Voucherify_Tax::calc_applicable_amount( $ response->order->discount_amount )139 'discount_amount' => Voucherify_Tax::calc_applicable_amount( $coupon_amount ) 116 140 ], $response, $this->redemptions, 'promotion' ); 117 141 } … … 123 147 throw new Exception( $message ); 124 148 } 149 150 /** 151 * @param WC_Order $order 152 * @param $code 153 * 154 * @return array|array[] 155 */ 156 private function create_session_lock_data( WC_Order $order, $code ) { 157 if ( empty( $order ) ) { 158 $order = vcrf_get_admin_order(); 159 } 160 if ( ! empty( $order ) && $order->meta_exists( '_voucherify_session_key' ) ) { 161 $session_key = $order->get_meta( '_voucherify_session_key', true ); 162 } else { 163 $session_key = $this->order_placing_session->get_validation_session_key( $code ); 164 $order->add_meta_data( '_voucherify_session_key', $session_key, true ); 165 } 166 if ( empty( $session_key ) ) { 167 return []; 168 } 169 170 return [ 171 'session' => [ 172 'key' => $session_key 173 ] 174 ]; 175 } 176 177 private function add_shipping_to_context( $context ) { 178 array_push( $context['order']['items'], [ 179 'product_id' => 'prod_5h1pp1ng', 180 'quantity' => 1 181 ] ); 182 183 return $context; 184 } 185 186 /** 187 * Makes the API call to rollback redemption. 188 * 189 * @param string $redemption_id id of redemption to rollback 190 * 191 * @throws ClientException if voucherify api could not rollback redemption. 192 */ 193 public function rollback_redemption( $redemption_id ) { 194 apply_filters( 'voucherify_redemption_service_rollback', 195 $this->redemptions->rollback( $redemption_id ) ); 196 } 125 197 } 126 198 } -
voucherify/trunk/src/class-voucherify-save-order-listener.php
r2361501 r2491101 8 8 */ 9 9 10 use Voucherify\ClientException; 11 10 12 if ( ! defined( 'ABSPATH' ) ) { 11 13 exit; … … 18 20 /** @var Voucherify_Validation_Service $validation_service */ 19 21 private $validation_service; 22 /** @var Voucherify_Order_Placing_Session $order_placing_session */ 23 private $order_placing_session; 24 /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */ 25 private $remove_coupon_service; 20 26 21 27 /** 22 28 * Voucherify_Save_Order_Listener constructor. 29 * 30 * @param Voucherify_Validation_Service $validation_service 31 * @param Voucherify_Redemption_Service $redemption_service 32 * @param Voucherify_Order_Placing_Session $order_placing_session 33 * @param Voucherify_Remove_Coupon_Service $remove_coupon_service 23 34 */ 24 35 public function __construct( 25 36 Voucherify_Validation_Service $validation_service, 26 Voucherify_Redemption_Service $redemption_service 37 Voucherify_Redemption_Service $redemption_service, 38 Voucherify_Order_Placing_Session $order_placing_session, 39 Voucherify_Remove_Coupon_Service $remove_coupon_service 27 40 ) { 28 $this->redemption_service = $redemption_service; 29 $this->validation_service = $validation_service; 30 } 31 32 /** 33 * Hooks to `woocommerce_before_checkout_process`. 34 * 35 * It should jump in before default voucher-in-cart validation ({@hook woocommerce_get_shop_coupon_data}, 36 * {@link Voucherify_Validation_Service::on_discount_code_added}) that would be performed right before 37 * the checkout. 38 * 39 * The reason it's added here is: if the voucher has been redeemed in the meanwhile, we don't want to block 40 * order placement with error message (which would be the case with default voucher validation). 41 * Instead we want to allow placing the order without the voucher, leaving a message on the thankyou page. 42 */ 43 public function on_before_checkout_process() { 44 WC()->session->set( 'voucherify_checkout_removed_coupon', false ); 45 46 $applied_voucher_codes = WC()->cart->get_applied_coupons(); 47 if ( empty( $applied_voucher_codes ) ) { 48 return; 49 } 50 51 $validated_voucher = $this->validation_service->get_validated_virtual_coupon( current( $applied_voucher_codes ) ); 52 53 if ( ! $validated_voucher ) { 54 WC()->session->set( 'voucherify_checkout_removed_coupon', 'yes' ); 55 WC()->cart->remove_coupons(); 56 } 57 } 58 41 $this->redemption_service = $redemption_service; 42 $this->validation_service = $validation_service; 43 $this->order_placing_session = $order_placing_session; 44 $this->remove_coupon_service = $remove_coupon_service; 45 } 59 46 60 47 /** … … 64 51 * @param WC_Order $order 65 52 * 53 * @throws ClientException 66 54 * @throws Exception 67 55 */ 68 56 public function on_before_order_save( WC_Order $order ) { 69 if ( is_admin() ) { 57 if ( ! in_array( $order->get_status(), [ 'processing', 'completed' ] ) ) { 58 return; 59 } 60 61 if ( is_admin() && ( ! defined( 'VCRF_FETCH_PROMOTIONS' ) || ! VCRF_FETCH_PROMOTIONS ) ) { 62 $this->maybe_remove_free_shipping_coupon(); 70 63 $this->handle_order_save_by_admin( $order ); 71 64 … … 75 68 } 76 69 70 /** 71 * @throws Exception 72 */ 73 public function maybe_remove_free_shipping_coupon() { 74 $shipping_methods = $this->get_shipping_methods(); 75 $coupon_codes = $this->get_applied_coupons(); 76 if ( empty( $coupon_codes ) || empty( $shipping_methods ) ) { 77 return; 78 } 79 80 $shipping_method = current( $shipping_methods ); 81 if ( strpos( $shipping_method, 'free_shipping' ) === 0 ) { 82 return; 83 } 84 85 foreach ( $coupon_codes as $coupon_code ) { 86 $validated_coupon = $this->validation_service->get_validated_virtual_coupon( $coupon_code ); 87 88 if ( empty( $validated_coupon ) ) { 89 continue; 90 } 91 92 if ( $validated_coupon['free_shipping'] ) { 93 $this->remove_coupon( $coupon_code ); 94 } 95 } 96 } 97 98 private function remove_coupon( $coupon_code ) { 99 if ( ! is_admin() ) { 100 WC()->cart->remove_coupon( $coupon_code ); 101 $this->remove_coupon_service->remove_coupon_from_voucherify_session( $coupon_code, true ); 102 103 return; 104 } 105 106 $order = vcrf_get_admin_order(); 107 if ( ! empty( $order ) ) { 108 $order->remove_coupon( $coupon_code ); 109 $order->save(); 110 $this->remove_coupon_service->remove_coupon_from_voucherify_session( $coupon_code ); 111 } 112 } 113 114 private function get_shipping_methods() { 115 if ( ! is_admin() ) { 116 return WC()->session->get( 'chosen_shipping_methods' ); 117 } 118 119 $order = vcrf_get_admin_order(); 120 if ( ! empty( $order ) ) { 121 return array_map( function ( WC_Order_Item_Shipping $method ) { 122 return $method->get_method_id(); 123 }, $order->get_shipping_methods() ); 124 } 125 126 return null; 127 } 128 129 private function get_applied_coupons() { 130 if ( ! is_admin() ) { 131 return WC()->cart->get_applied_coupons(); 132 } 133 134 $order = vcrf_get_admin_order(); 135 if ( ! empty( $order ) ) { 136 return $order->get_coupon_codes(); 137 } 138 139 return null; 140 } 141 77 142 private function handle_order_save_by_admin( WC_Order $order ) { 78 if ( ! in_array( $order->get_status(), [ 'processing', 'completed' ] ) ) {79 return;80 }81 143 $order_vouchers = $order->get_items( 'coupon' ); 82 144 $redeemed_vouchers = $order->get_meta( "_voucherify_redeemed_voucher", false ); … … 88 150 }, $redeemed_vouchers ) ); 89 151 152 if ( empty( $new_codes ) ) { 153 return; 154 } 155 156 $rolled_back_vouchers_ids = $this::get_rolled_back_vouchers_ids( $order ); 157 90 158 foreach ( $new_codes as $new_code ) { 91 $redeemed_voucher = $this->redemption_service->redeem( $new_code );159 $redeemed_voucher = $this->redemption_service->redeem( $new_code, $order ); 92 160 $order->add_meta_data( '_voucherify_redeemed_voucher', $redeemed_voucher, false ); 93 } 94 } 95 161 if ( array_key_exists( $new_code, $rolled_back_vouchers_ids ) ) { 162 $order->delete_meta_data_by_mid( $rolled_back_vouchers_ids[ $new_code ] ); 163 $order->add_order_note( sprintf( __( "Voucherify: %s was redeemed again", 'voucherify' ), $new_code ), false ); 164 } 165 } 166 } 167 168 private function get_rolled_back_vouchers_ids( WC_Order $order ) { 169 $rolled_back_vouchers = $order->get_meta( "_voucherify_rolled_back_voucher", false ); 170 $rolled_back_codes = []; 171 foreach ( $rolled_back_vouchers as $voucher ) { 172 if ( ! empty( $voucher->id ) ) { 173 $rolled_back_codes[ $voucher->value['code'] ] = $voucher->id; 174 } 175 } 176 177 return $rolled_back_codes; 178 } 179 180 /** 181 * @param WC_Order $order 182 * 183 * @throws ClientException 184 * @throws Exception 185 */ 96 186 private function handle_new_order_save_by_customer( WC_Order $order ) { 97 $coupon_removed = WC()->session->get( 'voucherify_checkout_removed_coupon' ) === 'yes';98 WC()->session->set( 'voucherify_checkout_removed_coupon', 'no' );99 100 if ( $coupon_removed ) {101 $order->update_meta_data( '_voucherify_checkout_removed_coupon', 'yes' );102 }103 104 187 $order_vouchers = $order->get_items( 'coupon' ); 105 188 if ( ! empty( $order->get_meta( "_voucherify_redeemed_voucher", false ) ) || empty( $order_vouchers ) ) { 106 189 return; 107 190 } 108 109 $redeemed_voucher = $this->redemption_service->redeem( current( $order_vouchers )->get_code() ); 110 $order->update_meta_data( '_voucherify_redeemed_voucher', $redeemed_voucher ); 191 $code = current( $order_vouchers )->get_code(); 192 if ( in_array( $code, $this->order_placing_session->get_removed_coupons(), true ) ) { 193 return; 194 } 195 try { 196 $redeemed_voucher = $this->redemption_service->redeem( $code, $order ); 197 $order->update_meta_data( '_voucherify_redeemed_voucher', $redeemed_voucher ); 198 } catch ( ClientException $e ) { 199 if ( $e->getKey() === 'quantity_exceeded' ) { 200 $this->order_placing_session->add_removed_coupon( $code ); 201 $coupons_to_remove = $this->order_placing_session->get_removed_coupons(); 202 $order->update_meta_data( '_voucherify_checkout_removed_coupon', $coupons_to_remove ); 203 } else { 204 $logger = wc_get_logger(); 205 $logger->error( __( 'Redeem was unsuccessful', 'voucherify' ), [ 'original_message' => $e->getMessage() ] ); 206 throw $e; 207 } 208 } catch ( Exception $e ) { 209 $logger = wc_get_logger(); 210 $logger->error( __( 'Unexpected error occured', 'voucherify' ), [ 'original_message' => $e->getMessage() ] ); 211 throw $e; 212 } 111 213 } 112 214 … … 124 226 */ 125 227 public function add_notice_on_thankyou_page( $message, $order ) { 126 if ( ! empty( $order ) && $order->get_meta( '_voucherify_checkout_removed_coupon' ) === 'yes' ) { 228 if ( ! empty( $order ) && $order->meta_exists( '_voucherify_checkout_removed_coupon' ) ) { 229 $codes = $order->get_meta( '_voucherify_checkout_removed_coupon', true ); 230 foreach ( $codes as $code ) { 231 $order->remove_coupon( $code ); 232 } 127 233 $message .= apply_filters( '', 128 234 '<div class="woocommerce-info">' . … … 132 238 $message, $order ); 133 239 } 240 $this->order_placing_session->clear(); 134 241 135 242 return $message; -
voucherify/trunk/src/class-voucherify-valid-promotions-fetcher.php
r2361501 r2491101 59 59 */ 60 60 public function handle_ajax_vcrf_fetch_promotions() { 61 define('VCRF_FETCH_PROMOTIONS', true); 61 62 echo json_encode( $this->get_valid_promotions() ); 62 63 die(); -
voucherify/trunk/src/class-voucherify-validation-service.php
r2361501 r2491101 11 11 } 12 12 13 use Voucherify\ClientException; 13 14 use \Voucherify\Validations; 15 use \Voucherify\Promotions; 14 16 15 17 if ( ! class_exists( 'Voucherify_Validation_Service' ) ) { … … 35 37 /** @var Validations $validations */ 36 38 private $validations; 37 /** @var \Voucherify\Promotions $promotions */39 /** @var Promotions $promotions */ 38 40 private $promotions; 41 /** @var Voucherify_Order_Placing_Session $order_placing_session */ 42 private $order_placing_session; 43 /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */ 44 private $remove_coupon_service; 39 45 40 46 /** … … 42 48 * 43 49 * @param Validations $validations Validations endpoint from Voucherify SDK 44 */ 45 public function __construct( Validations $validations, \Voucherify\Promotions $promotions ) { 46 $this->validations = $validations; 47 $this->promotions = $promotions; 50 * @param Promotions $promotions 51 * @param Voucherify_Order_Placing_Session $order_placing_session 52 * @param Voucherify_Remove_Coupon_Service $remove_coupon_service 53 */ 54 public function __construct( 55 Validations $validations, 56 Promotions $promotions, 57 Voucherify_Order_Placing_Session $order_placing_session, 58 Voucherify_Remove_Coupon_Service $remove_coupon_service 59 ) { 60 $this->validations = $validations; 61 $this->promotions = $promotions; 62 $this->order_placing_session = $order_placing_session; 63 $this->remove_coupon_service = $remove_coupon_service; 48 64 } 49 65 … … 58 74 * @param string $coupon_code code to be validated. 59 75 * 60 * @return stringvalid coupon data61 * @ throws \Voucherify\ClientException76 * @return array|bool valid coupon data 77 * @noinspection PhpUnusedParameterInspection 62 78 */ 63 79 public function on_discount_code_added( $coupon, $coupon_code ) { … … 66 82 } 67 83 68 return $this->get_validated_virtual_coupon( $coupon_code ); 84 //remove coupon in the admin panel 85 if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'woocommerce_remove_order_coupon' ) { 86 $this->remove_coupon_service->remove_coupon_from_voucherify_session( $coupon_code, true ); 87 88 return false; 89 } 90 91 try { 92 return $this->get_validated_virtual_coupon( $coupon_code ); 93 } catch ( Exception $e ) { 94 $logger = wc_get_logger(); 95 $logger->error( __( 'Validation was unsuccessful', 'voucherify' ), [ 'original_message' => $e->getMessage() ] ); 96 97 return false; 98 } 69 99 } 70 100 … … 80 110 * @param string $coupon_code Voucher or promotion code. 81 111 * 82 * @return WC_Couponvalid virtual WC coupon83 * @throws \Voucherify\ClientException112 * @return array valid virtual WC coupon 113 * @throws Exception 84 114 */ 85 115 public function get_validated_virtual_coupon( $coupon_code ) { 86 116 $coupon_code_enhanced = explode( ":", $coupon_code ); 87 117 118 $is_promotion = false; 119 $actual_code = $coupon_code; 88 120 if ( count( $coupon_code_enhanced ) === 2 && $coupon_code_enhanced[0] === "promotion" ) { 89 return $this->get_valid_promotion_data( $coupon_code_enhanced[1] ); 90 } 91 92 return $this->get_valid_voucher_data( $coupon_code ); 121 $is_promotion = true; 122 $actual_code = $coupon_code_enhanced[1]; 123 } 124 125 if ( ! empty( self::$vouchers_requested ) && ! array_key_exists( $actual_code, self::$vouchers_requested ) ) { 126 throw new Exception(); 127 } 128 129 if ( $is_promotion ) { 130 return $this->get_valid_promotion_data( $actual_code ); 131 } 132 133 return $this->get_valid_voucher_data( $actual_code ); 93 134 } 94 135 … … 98 139 * Overrides default error message for invalid coupons. 99 140 * 141 * @param $err mixed unused 142 * @param $err_code 143 * @param $coupon mixed unused 144 * 100 145 * @return string custom voucherify error message 146 * @noinspection PhpUnusedParameterInspection 101 147 */ 102 148 public function invalid_error_message( $err, $err_code, $coupon ) { … … 124 170 * @param string $code coupon code as string 125 171 * 126 * @return array coupon values.127 * @throws \Voucherify\ClientException172 * @return array|false coupon values. 173 * @throws ClientException 128 174 */ 129 175 public function get_valid_voucher_data( $code ) { … … 140 186 } 141 187 142 $context = apply_filters( 'voucherify_validation_service_validation_context',143 vcrf_get_customer_data() + vcrf_get_order_data() );144 145 $context = apply_filters( 'voucherify_validation_service_voucher_validation_context', $context );146 147 188 $response = apply_filters( 'voucherify_validation_service_validate', 148 $this->validat ions->validate( $code, $context), $this->validations );189 $this->validate_voucher( $code ), $this->validations ); 149 190 150 191 $response = apply_filters( 'voucherify_validation_service_validate_voucher', $response, 151 192 $this->validations ); 152 193 153 if ( $response->valid && isset( $response->order->discount_amount ) ) { 154 self::$vouchers_requested[ $code ] = apply_filters( 'voucherify_validation_service_validation_result', [ 155 'code' => $response->code, 156 'amount' => Voucherify_Tax::calc_applicable_amount( $response->order->discount_amount ), 157 'discount_type' => 'fixed_cart', 158 'individual_use' => true, 159 'description' => apply_filters( 'voucherify_voucher_description', 160 sprintf( __( 'Voucher: %s', 'voucherify' ), $code ), $response ) 161 ], $response, $this->validations ); 162 163 return apply_filters( 'voucherify_validation_service_voucher_validation_result', 164 self::$vouchers_requested[ $code ], $response, $this->validations ); 165 } 166 167 $return = false; 168 $return = apply_filters( 'voucherify_validation_service_validation_result', $return, $response, 169 $this->validations ); 170 171 return apply_filters( 'voucherify_validation_service_voucher_validation_result', $return, $response, 172 $this->validations ); 194 if ( $this->is_free_shipping_validation_response( $response ) ) { 195 self::$vouchers_requested[ $code ] = $this->prepare_free_shipping_coupon( $response ); 196 197 return $this->process_validation_result( self::$vouchers_requested[ $code ], $response ); 198 } else if ( $response->valid ) { 199 self::$vouchers_requested[ $code ] = $this->prepare_discount_coupon( $response ); 200 201 return $this->process_validation_result( self::$vouchers_requested[ $code ], $response ); 202 } 203 204 return false; 205 } 206 207 private function process_validation_result( $voucher_requested, $response ) { 208 $voucher_requested = apply_filters( 'voucherify_validation_service_validation_result', $voucher_requested, 209 $response, $this->validations ); 210 211 if ( false === $voucher_requested ) { 212 $this->order_placing_session->clear(); 213 } elseif ( $this->is_free_shipping_validation_response( $response ) ) { 214 $voucher_requested = 215 apply_filters( 'voucherify_validation_service_free_shipping_voucher_validation_result', 216 $voucher_requested, $response, $this->validations ); 217 } 218 219 return apply_filters( 'voucherify_validation_service_voucher_validation_result', $voucher_requested, 220 $response, $this->validations ); 221 } 222 223 /** 224 * @param $code 225 * 226 * @return mixed|stdClass|null 227 * @throws ClientException 228 */ 229 private function validate_voucher( $code ) { 230 $response = $this->order_placing_session->get_validation_response( $code ); 231 if ( empty( $response ) ) { 232 $context = apply_filters( 'voucherify_validation_service_validation_context', 233 vcrf_get_customer_data() + vcrf_get_order_data() + $this->create_session_lock_data( $code ) ); 234 $context = $this->add_shipping_to_context( $context ); 235 $context = apply_filters( 'voucherify_validation_service_voucher_validation_context', $context ); 236 $response = $this->validations->validate( $code, $context ); 237 $this->save_session_key( $code, $response ); 238 } 239 240 return $response; 241 } 242 243 private function is_free_shipping_validation_response( $response ) { 244 return $response->valid && 245 ! empty( $response->discount->unit_type ) && 246 $response->discount->unit_type === 'prod_5h1pp1ng'; 247 } 248 249 private function prepare_free_shipping_coupon( $response ) { 250 $coupon = $this->prepare_coupon( $response ); 251 252 return wp_parse_args( [ 253 'amount' => 0, 254 'free_shipping' => true, 255 ], $coupon ); 256 } 257 258 private function prepare_coupon( $response ) { 259 return [ 260 'code' => $response->code, 261 'discount_type' => 'fixed_cart', 262 'individual_use' => true, 263 'description' => apply_filters( 'voucherify_voucher_description', 264 sprintf( __( 'Voucher: %s', 'voucherify' ), $response->code ), $response ) 265 ]; 266 } 267 268 private function prepare_discount_coupon( $response ) { 269 $coupon_amount = Voucherify_Voucher_Amount_Calculator::calculate_coupon_amount( $response ); 270 271 return wp_parse_args( 272 [ 'amount' => Voucherify_Tax::calc_applicable_amount( $coupon_amount ), ], 273 $this->prepare_coupon( $response ) 274 ); 275 } 276 277 private function create_session_lock_data( $code ) { 278 $session_data = [ 279 'session' => [ 280 'type' => 'LOCK', 281 'ttl' => get_option( 'voucherify_lock_ttl', 7 ), 282 'ttl_unit' => 'DAYS' 283 ] 284 ]; 285 286 $order = vcrf_get_admin_order(); 287 if ( empty( $order ) ) { 288 return $session_data; 289 } 290 291 $redeemed_vouchers = $order->get_meta( "_voucherify_redeemed_voucher", false ); 292 foreach ( $redeemed_vouchers as $redeemed_voucher ) { 293 if ( $redeemed_voucher->value['code'] === $code ) { 294 return []; 295 } 296 } 297 298 if ( $order->meta_exists( '_voucherify_session_key' ) ) { 299 $session_data['session']['key'] = $order->get_meta( '_voucherify_session_key', true ); 300 } 301 302 return $session_data; 173 303 } 174 304 … … 179 309 * 180 310 * @return array coupon values. 181 * @throws \Voucherify\ClientException311 * @throws ClientException 182 312 */ 183 313 public function get_valid_promotion_data( $code ) { … … 195 325 } 196 326 197 $context = apply_filters( 'voucherify_validation_service_validation_context',198 vcrf_get_customer_data() + vcrf_get_order_data() );199 200 $context = apply_filters( 'voucherify_validation_service_validation_promotion_context', $context );201 202 327 $response = apply_filters( 'voucherify_validation_service_validate', 203 $this-> promotions->validate( $context), $this->validations );328 $this->validate_promotion( $code ), $this->validations ); 204 329 $response = apply_filters( 'voucherify_validation_service_validate_promotion', $response, 205 330 $this->validations ); … … 209 334 $return = apply_filters( 'voucherify_validation_service_validation_result', $return, $response, 210 335 $this->validations ); 336 $this->order_placing_session->clear(); 211 337 212 338 return apply_filters( 'voucherify_validation_service_promotion_validation_result', $return, $response, … … 226 352 $return = apply_filters( 'voucherify_validation_service_validation_result', $return, $response, 227 353 $this->validations ); 354 $this->order_placing_session->clear(); 228 355 229 356 return apply_filters( 'voucherify_validation_service_promotion_validation_result', $return, $response, … … 231 358 } 232 359 233 self::$vouchers_requested[ $code ] = apply_filters( 'voucherify_validation_service_validation_result', [ 360 $coupon_amount = Voucherify_Voucher_Amount_Calculator::calculate_coupon_amount( $promo_found ); 361 362 $wc_coupon = [ 234 363 'code' => $promo_found->id, 235 'amount' => Voucherify_Tax::calc_applicable_amount( $ promo_found->discount_amount ),364 'amount' => Voucherify_Tax::calc_applicable_amount( $coupon_amount ), 236 365 'discount_type' => 'fixed_cart', 237 366 'individual_use' => true, 238 367 'description' => apply_filters( 'voucherify_promotion_description', 239 368 sprintf( __( 'Promotion: %s', 'voucherify' ), $promo_found->banner ), $promo_found ) 240 ], $response, $this->validations ); 369 ]; 370 371 if ( $this->is_free_shipping_promotion( $promo_found ) ) { 372 $wc_coupon['free_shipping'] = true; 373 } 374 375 self::$vouchers_requested[ $code ] = apply_filters( 'voucherify_validation_service_validation_result', $wc_coupon, $response, $this->validations ); 241 376 242 377 return apply_filters( 'voucherify_validation_service_promotion_validation_result', … … 245 380 } 246 381 382 private function is_free_shipping_promotion( $promo ) { 383 if ( ! empty( $promo->order->items ) ) { 384 foreach ( $promo->order->items as $item ) { 385 if ( $item->product_id === 'prod_5h1pp1ng' ) { 386 return true; 387 } 388 } 389 } 390 391 return false; 392 } 393 394 private function validate_promotion( $code ) { 395 $response = $this->order_placing_session->get_validation_response( $code ); 396 if ( empty( $response ) ) { 397 $context = apply_filters( 'voucherify_validation_service_validation_context', 398 vcrf_get_customer_data() + vcrf_get_order_data() + $this->create_session_lock_data( $code ) ); 399 400 $context = apply_filters( 'voucherify_validation_service_validation_promotion_context', $context ); 401 $context = $this->add_shipping_to_context( $context ); 402 $response = $this->promotions->validate( $context ); 403 $this->save_session_key( $code, $response ); 404 } 405 406 return $response; 407 } 408 409 private function save_session_key( $code, $response ) { 410 //add coupon by customer 411 if ( ! is_admin() ) { 412 $this->order_placing_session->set_validation_response( $code, $response ); 413 414 return; 415 } 416 417 //add coupon by admin 418 $order = vcrf_get_admin_order(); 419 if ( ! empty( $order ) 420 && ! $order->meta_exists( '_voucherify_session_key' ) 421 && isset( $response, $response->session, $response->session->key ) ) { 422 $order->add_meta_data( '_voucherify_session_key', $response->session->key, true ); 423 } 424 } 425 426 private function add_shipping_to_context( $context ) { 427 array_push( $context['order']['items'], [ 428 'product_id' => 'prod_5h1pp1ng', 429 'quantity' => 1 430 ] ); 431 432 return $context; 433 } 434 247 435 public static function get_code_type( $code ) { 248 if ( preg_match( '/^promotion \:/', $code ) ) {436 if ( preg_match( '/^promotion:/', $code ) ) { 249 437 return apply_filters( 'voucherify_get_code_type', 'promotion', $code ); 250 438 } 251 439 252 440 return apply_filters( 'voucherify_get_code_type', 'voucher', $code ); 441 } 442 443 public function remove_coupons() { 444 $all_coupons = $this->order_placing_session->get_all_validation_responses(); 445 446 foreach ( $all_coupons as $code => $coupon ) { 447 $this->remove_coupon_service->on_removed_coupon( $code ); 448 } 449 450 self::$vouchers_requested = []; 253 451 } 254 452 } -
voucherify/trunk/src/class-voucherify.php
r1821613 r2491101 12 12 } 13 13 14 use \Voucherify\ApiClient;15 use \Voucherify\VoucherifyClient;16 17 14 require_once "class-voucherify-admin-settings.php"; 18 15 require_once "class-voucherify-cart-decorator.php"; … … 21 18 require_once "class-voucherify-redemption-service.php"; 22 19 require_once "class-voucherify-save-order-listener.php"; 20 require_once "class-voucherify-refund-order-listener.php"; 23 21 require_once "class-voucherify-promotion-form-renderer.php"; 24 22 require_once "class-voucherify-promotion-service.php"; … … 28 26 require_once "class-voucherify-messaging-modificator.php"; 29 27 require_once "class-voucherify-form-handler.php"; 28 require_once "class-voucherify-order-placing-session.php"; 29 require_once "class-voucherify-client-extension.php"; 30 require_once "class-voucherify-remove-coupon-service.php"; 31 require_once "class-voucherify-voucher-amount-calculator.php"; 30 32 31 33 if ( ! class_exists( 'Voucherify' ) ) { … … 34 36 /** @var Voucherify_Admin_Settings $settings */ 35 37 private $settings; 38 /** @var Voucherify_Order_Placing_Session $order_placing_session */ 39 private $order_placing_session; 36 40 /** @var Voucherify_Validation_Service $validataion_service */ 37 41 private $validataion_service; … … 40 44 /** @var Voucherify_Save_Order_Listener $save_order_listener */ 41 45 private $save_order_listener; 46 /** @var Voucherify_Refund_Order_Listener $refund_order_listener */ 47 private $refund_order_listener; 42 48 /** @var Voucherify_Promotion_Form_Renderer $promotion_form_renderer */ 43 49 private $promotion_form_renderer; … … 54 60 /** @var Voucherify_Form_Handler $form_handler */ 55 61 private $form_handler; 62 /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */ 63 private $remove_coupon_service; 56 64 57 65 /** … … 61 69 $this->settings = new Voucherify_Admin_Settings(); 62 70 63 $voucherify_client = new VoucherifyClient( get_option( 'voucherify_app_id' ), 71 $this->order_placing_session = new Voucherify_Order_Placing_Session(); 72 $voucherify_client = new Voucherify_Client_Extension( get_option( 'voucherify_app_id' ), 64 73 get_option( 'voucherify_app_secret_key' ) ); 74 $this->remove_coupon_service = new Voucherify_Remove_Coupon_Service( $voucherify_client, $this->order_placing_session ); 65 75 $this->validataion_service = new Voucherify_Validation_Service( $voucherify_client->validations, 66 $voucherify_client->promotions ); 67 $this->redemption_service = new Voucherify_Redemption_Service( $voucherify_client->redemptions ); 76 $voucherify_client->promotions, $this->order_placing_session, $this->remove_coupon_service ); 77 $this->redemption_service = new Voucherify_Redemption_Service( $voucherify_client->redemptions, 78 $this->order_placing_session ); 68 79 $this->save_order_listener = new Voucherify_Save_Order_Listener( $this->validataion_service, 69 $this->redemption_service ); 80 $this->redemption_service, $this->order_placing_session, $this->remove_coupon_service ); 81 $this->refund_order_listener = new Voucherify_Refund_Order_Listener( $this->redemption_service ); 70 82 $this->promotion_service = new Voucherify_Promotion_Service(); 71 83 $this->all_promotions_fetcher = … … 97 109 remove_filter( 'woocommerce_coupon_code', 'wc_strtolower' ); 98 110 111 add_action( 'wp_logout', [ $this->order_placing_session, 'clear' ] ); 112 add_action( 'wp_login', [ $this->order_placing_session, 'clear' ] ); 113 99 114 add_action( 'woocommerce_register_post_type_shop_coupon', [ $this, 'remove_coupons_menu_items' ] ); 100 115 add_filter( 'woocommerce_admin_reports', [ $this, 'remove_coupons_report' ] ); … … 103 118 'invalid_error_message' 104 119 ], 10, 3 ); 105 add_filter( 'woocommerce_get_shop_coupon_data', [ 106 $this->validataion_service, 107 'on_discount_code_added' 108 ], 10, 2 ); 120 121 $this->enable_coupons_validation(); 122 123 add_action( 'woocommerce_checkout_process', [ 124 $this->save_order_listener, 125 'maybe_remove_free_shipping_coupon' 126 ] ); 109 127 110 128 add_action( 'woocommerce_before_order_object_save', [ … … 112 130 'on_before_order_save' 113 131 ], 10 ); 114 add_filter( 'woocommerce_before_checkout_process', [115 $this->save_order_listener,116 'on_before_checkout_process'117 ] );118 132 add_filter( 'woocommerce_thankyou_order_received_text', [ 119 133 $this->save_order_listener, … … 129 143 'handle_ajax_vcrf_fetch_promotions' 130 144 ], 10 ); 145 146 add_action( 'woocommerce_cart_item_removed', [ $this, 'on_cart_item_removed' ], 10, 2 ); 131 147 132 148 add_action( 'woocommerce_cart_coupon', [ $this->promotion_form_renderer, 'render_promotion_from' ] ); … … 163 179 164 180 do_action( 'voucherify_initialize' ); 181 182 add_action( 'woocommerce_order_fully_refunded', [ 183 $this->refund_order_listener, 184 'on_after_order_full_refund' 185 ], 10, 1 ); 186 187 add_action( 'woocommerce_order_partially_refunded', [ 188 $this->refund_order_listener, 189 'on_after_order_partial_refund' 190 ], 10, 1 ); 191 192 add_action( 'woocommerce_admin_order_item_headers', [ 193 $this->refund_order_listener, 194 'add_partial_refund_rollback_checkbox' 195 ], 10, 1 ); 196 197 add_action( 'woocommerce_removed_coupon', 198 [ 199 $this->remove_coupon_service, 200 'on_removed_coupon' 201 ], 10, 1 ); 202 203 add_action( 'woocommerce_before_calculate_totals', [ 204 $this->order_placing_session, 205 'clear_on_before_calculate_totals' 206 ], 10, 0 ); 207 208 add_filter( 'woocommerce_update_cart_action_cart_updated', [ 209 $this, 210 'on_update_cart_action_cart_updated' 211 ], 10 ); 165 212 } 166 213 } … … 175 222 public function shut_down() { 176 223 if ( is_voucherify_enabled() ) { 224 remove_action( 'wp_logout', [ $this->order_placing_session, 'clear' ] ); 225 remove_action( 'wp_login', [ $this->order_placing_session, 'clear' ] ); 226 177 227 add_filter( 'woocommerce_coupon_code', 'wc_strtolower' ); 178 228 remove_action( 'woocommerce_register_post_type_shop_coupon', [ $this, 'remove_coupons_menu_items' ] ); … … 182 232 'invalid_error_message' 183 233 ], 10 ); 184 remove_filter( 'woocommerce_get_shop_coupon_data', [ 185 $this->validataion_service, 186 'on_discount_code_added' 187 ], 10 ); 234 235 $this->disable_coupons_validation(); 236 237 remove_action( 'woocommerce_checkout_process', [ 238 $this->save_order_listener, 239 'maybe_remove_free_shipping_coupon' 240 ] ); 188 241 189 242 remove_action( 'woocommerce_before_order_object_save', [ … … 191 244 'on_before_order_save' 192 245 ], 10 ); 193 remove_filter( 'woocommerce_before_checkout_process', [194 $this->save_order_listener,195 'on_before_checkout_process'196 ] );197 246 remove_filter( 'woocommerce_thankyou_order_received_text', [ 198 247 $this->save_order_listener, 199 248 'add_notice_on_thankyou_page' 200 249 ], 10 ); 250 201 251 remove_action( 'woocommerce_cart_coupon', [ $this->promotion_form_renderer, 'render_promotion_from' ] ); 252 253 remove_action( 'woocommerce_cart_item_removed', [ $this, 'on_cart_item_removed' ], 10 ); 202 254 203 255 remove_action( 'wp_ajax_vcrf_apply_promotion', [ … … 227 279 remove_action( 'wp_loaded', [ $this->form_handler, 'handle' ], 25 ); 228 280 281 remove_action( 'woocommerce_order_fully_refunded', [ 282 $this->refund_order_listener, 283 'on_after_order_full_refund' 284 ] ); 285 286 remove_action( 'woocommerce_order_partially_refunded', [ 287 $this->refund_order_listener, 288 'on_after_order_partial_refund' 289 ] ); 290 291 remove_action( 'woocommerce_admin_order_item_headers', [ 292 $this->refund_order_listener, 293 'add_partial_refund_rollback_checkbox' 294 ] ); 295 296 remove_action( 'woocommerce_removed_coupon', [ 297 $this->remove_coupon_service, 298 'on_removed_coupon' 299 ] ); 300 301 remove_action( 'woocommerce_before_calculate_totals', [ 302 $this->order_placing_session, 303 'clear_on_before_calculate_totals' 304 ] ); 305 306 remove_filter( 'woocommerce_update_cart_action_cart_updated', [ 307 $this, 308 'on_update_cart_action_cart_updated' 309 ] ); 310 229 311 do_action( 'voucherify_shut_down' ); 230 312 } … … 243 325 * of core coupons, including coupons reports. 244 326 * 245 * @param $reports stores information about woocommerce reports327 * @param $reports mixed stores information about woocommerce reports 246 328 * 247 329 * @return mixed … … 269 351 return apply_filters( 'voucherify_remove_coupons_menu_items', $settings ); 270 352 } 353 354 /** @noinspection PhpUnusedParameterInspection */ 355 public function on_cart_item_removed( $_, WC_Cart $cart ) { 356 if ( $cart->is_empty() ) { 357 $this->validataion_service->remove_coupons(); 358 $cart->remove_coupons(); 359 } 360 } 361 362 public function on_update_cart_action_cart_updated( $cart_updated ) { 363 if ( $cart_updated ) { 364 $this->order_placing_session->clear(); 365 } 366 367 return $cart_updated; 368 } 369 370 public function enable_coupons_validation() { 371 add_filter( 'woocommerce_get_shop_coupon_data', [ 372 $this->validataion_service, 373 'on_discount_code_added' 374 ], 10, 2 ); 375 } 376 377 public function disable_coupons_validation() { 378 remove_filter( 'woocommerce_get_shop_coupon_data', [ 379 $this->validataion_service, 380 'on_discount_code_added' 381 ], 10 ); 382 } 271 383 } 272 384 } -
voucherify/trunk/src/functions.php
r2361501 r2491101 40 40 */ 41 41 function is_validation_blocked() { 42 $block_vouchers = ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && is_admin();43 42 $block_vouchers = is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX 43 && ! ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === "woocommerce_refund_line_items" ) ); 44 44 return apply_filters( 'voucherify_validation_service_block_validation', $block_vouchers ); 45 45 } … … 51 51 } 52 52 53 function is_voucherify_rollback_enabled() { 54 $voucherify_rollback_enabled = get_option( 'voucherify_rollback_enabled', 'yes' ) === 'yes'; 55 56 return apply_filters( 'voucherify_rollback_enabled', $voucherify_rollback_enabled ); 57 } 58 53 59 if ( ! function_exists( 'vcrf_get_order_data' ) ) { 54 60 /** 55 61 * Collects information about order and prepares portion of context data 56 62 * to be consumed by API. 63 * 64 * @param WC_Order $order 57 65 * 58 66 * @return array order data in form of array accepted by API's context. 59 67 */ 60 function vcrf_get_order_data() { 61 if ( ! is_admin() ) { 68 function vcrf_get_order_data( WC_Order $order = null ) { 69 $is_pending = ! empty( $order ) && $order->get_status() === 'pending'; 70 if ( ! $is_pending && ( ! is_admin() || ( defined( 'VCRF_FETCH_PROMOTIONS' ) && VCRF_FETCH_PROMOTIONS ) ) ) { 62 71 $cart_decorator = new Voucherify_Cart_Decorator( WC()->cart ); 63 72 … … 65 74 } 66 75 67 // we're in admin panel 68 /** @var WC_Order $order */ 69 $order = vcrf_get_admin_order(); 76 // we're in admin panel or customer payment page 77 if ( empty( $order ) ) { 78 $order = vcrf_get_admin_order(); 79 } 70 80 71 81 if ( empty( $order ) ) { … … 73 83 } 74 84 85 /** @var WC_Order_Item_Product $order_items */ 75 86 $order_items = $order->get_items(); 76 87 … … 78 89 foreach ( $order_items as $item ) { 79 90 $item_data = [ 80 'product_id' => $item->get_ id(),91 'product_id' => $item->get_product_id(), 81 92 'quantity' => $item->get_quantity() 82 93 ]; … … 150 161 */ 151 162 function vcrf_get_customer_data() { 152 if ( ! is_admin() ) {163 if ( ! is_admin() || ( defined( 'VCRF_FETCH_PROMOTIONS' ) && VCRF_FETCH_PROMOTIONS ) ) { 153 164 $customer_decorator = new Voucherify_Customer_Decorator( WC()->customer ); 154 165 -
voucherify/trunk/voucherify.php
r2361501 r2491101 8 8 * Plugin URI: https://wordpress.org/plugins/voucherify/ 9 9 * Description: Integrates Voucherify API with woocommerce replacing core coupons functionality 10 * Version: 2. 0.110 * Version: 2.1.0 11 11 * Author: rspective 12 12 * Author URI: https://www.rspective.com/ … … 26 26 27 27 if ( ! function_exists( 'voucherify' ) ) { 28 /** 29 * @return Voucherify 30 */ 28 31 function voucherify() { 29 32 static $voucherify;
Note: See TracChangeset
for help on using the changeset viewer.