Plugin Directory

Changeset 2491101


Ignore:
Timestamp:
03/09/2021 11:37:33 PM (5 years ago)
Author:
rspective
Message:

Voucherify 2.1.0

Location:
voucherify/trunk
Files:
6 added
11 edited

Legend:

Unmodified
Added
Removed
  • voucherify/trunk/partials/admin-form.php

    r1819737 r2491101  
    4848                           class="regular-text code"></td>
    4949            </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>
    5073            </tbody>
    5174        </table>
  • voucherify/trunk/readme.txt

    r2361501 r2491101  
    77WC tested up to: 4.3.1
    88WC requires at least: 3.0.0
    9 Stable tag: 2.0.1
     9Stable tag: 2.1.0
    1010
    1111Integrates Voucherify API with woocommerce
     
    5050== Changelog ==
    5151
     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
    5260= 2.0.1 - 2020-08-14 =
    5361* Fix: Added SKUs to the API request
  • voucherify/trunk/src/class-voucherify-admin-settings.php

    r1810660 r2491101  
    2626         * Internal helper method to add a setting of given key to the right voucherify options group.
    2727         *
    28          * @param $key option key
     28         * @param $key string key
    2929         */
    3030        private function register_setting( $key ) {
     
    3939            $this->register_setting( 'voucherify_app_id' );
    4040            $this->register_setting( 'voucherify_app_secret_key' );
     41            $this->register_setting( 'voucherify_rollback_enabled' );
     42            $this->register_setting( 'voucherify_lock_ttl' );
    4143        }
    4244
  • voucherify/trunk/src/class-voucherify-cart-decorator.php

    r2361501 r2491101  
    6464                }
    6565
     66                if ( ! empty( $item['line_total'] ) ) {
     67                    $item_data['price'] = intval(100 * ($item['line_total'] + $item['line_tax']));
     68                }
     69
    6670                if ( ! empty( $item_data ) ) {
    6771                    $items_data[] = $item_data;
  • voucherify/trunk/src/class-voucherify-redemption-service.php

    r2361501 r2491101  
    77 * Time: 10:15 AM
    88 */
     9
     10use Voucherify\ClientException;
     11use Voucherify\Redemptions;
    912
    1013if ( ! defined( 'ABSPATH' ) ) {
     
    1720
    1821    class Voucherify_Redemption_Service {
    19         /** @var \Voucherify\Redemptions $redemptions */
     22        /** @var Redemptions $redemptions */
    2023        private $redemptions;
     24
     25        /** @var Voucherify_Order_Placing_Session $order_placing_session */
     26        private $order_placing_session;
     27
    2128
    2229        /**
    2330         * Voucherify_Redemption_Service constructor.
    2431         *
    25          * @param \Voucherify\Redemptions $redemptions
     32         * @param Redemptions $redemptions
     33         * @param Voucherify_Order_Placing_Session $order_placing_session
    2634         */
    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;
    2938        }
    3039
     
    3342         *
    3443         * @param string $code voucher or promotion code to be redeemed
     44         * @param WC_Order $order
    3545         *
    3646         * @return array returns array with properties `redemption_id`, `discount_amount`, `code` (promotion or voucher
    3747         * code) and `type` (whether it's `voucher` or `promotion`)
    3848         *
     49         * @throws ClientException if voucherify api could not redeem the voucher.
    3950         * @throws Exception if voucherify api could not redeem the voucher.
    4051         */
    41         public function redeem( $code ) {
     52        public function redeem( $code, WC_Order $order ) {
    4253            $coupon_code_enhanced = explode( ":", $code );
    4354
    4455            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 );
    4659            }
    4760
    48             return $this->redeem_voucher( $code );
     61            return $result;
    4962        }
    5063
     
    5366         *
    5467         * @param string $code voucher code to be redeemed
     68         * @param WC_Order $order
    5569         *
    5670         * @return array returns array with properties `redemption_id`, `discount_amount`, `code` (voucher code)
    5771         * and `type` (with value = `voucher`)
    5872         *
    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
    6075         */
    61         private function redeem_voucher( $code ) {
     76        private function redeem_voucher( $code, WC_Order $order ) {
    6277            $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 ) );
    6479
    6580            $context = apply_filters( 'voucherify_redemption_service_redemption_voucher_context', $context );
     81
     82            $this->add_shipping_to_context($context);
    6683
    6784            $response = apply_filters( 'voucherify_redemption_service_redeem',
     
    7087            $response = apply_filters( 'voucherify_redemption_service_redeem_voucher', $response, $this->redemptions );
    7188
    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 ) ) {
    7392                return apply_filters( 'voucherify_redemption_service_redeem_result', [
    7493                    'redemption_id'   => $response->id,
    7594                    'code'            => $code,
    7695                    'type'            => 'voucher',
    77                     'discount_amount' => Voucherify_Tax::calc_applicable_amount( $response->order->discount_amount )
     96                    'discount_amount' => Voucherify_Tax::calc_applicable_amount( $coupon_amount )
    7897                ], $response, $this->redemptions, 'voucher' );
    7998            }
     
    91110         * @param string $code promotion code to be redeemed
    92111         *
     112         * @param WC_Order $order
     113         *
    93114         * @return array returns array with properties `redemption_id`, `discount_amount`, `code` (promotion code)
    94115         * and `type` (with value = `promotion`)
    95116         *
    96          * @throws Exception if voucherify api could not redeem the voucher.
     117         * @throws ClientException
     118         * @throws Exception
    97119         */
    98         private function redeem_promotion( $code ) {
     120        private function redeem_promotion( $code, WC_Order $order ) {
    99121            $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 ) );
    101123
    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 );
    104126            $response = apply_filters( 'voucherify_redemption_service_redeem',
    105127                $this->redemptions->redeem( [ 'id' => $code ], $context ), $this->redemptions );
     
    108130                $this->redemptions );
    109131
    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 ) ) {
    111135                return apply_filters( 'voucherify_redemption_service_redeem_result', [
    112136                    'redemption_id'   => $response->id,
    113137                    'code'            => $code,
    114138                    'type'            => 'promotion',
    115                     'discount_amount' => Voucherify_Tax::calc_applicable_amount( $response->order->discount_amount )
     139                    'discount_amount' => Voucherify_Tax::calc_applicable_amount( $coupon_amount )
    116140                ], $response, $this->redemptions, 'promotion' );
    117141            }
     
    123147            throw new Exception( $message );
    124148        }
     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        }
    125197    }
    126198}
  • voucherify/trunk/src/class-voucherify-save-order-listener.php

    r2361501 r2491101  
    88 */
    99
     10use Voucherify\ClientException;
     11
    1012if ( ! defined( 'ABSPATH' ) ) {
    1113    exit;
     
    1820        /** @var Voucherify_Validation_Service $validation_service */
    1921        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;
    2026
    2127        /**
    2228         * 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
    2334         */
    2435        public function __construct(
    2536            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
    2740        ) {
    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        }
    5946
    6047        /**
     
    6451         * @param WC_Order $order
    6552         *
     53         * @throws ClientException
    6654         * @throws Exception
    6755         */
    6856        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();
    7063                $this->handle_order_save_by_admin( $order );
    7164
     
    7568        }
    7669
     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
    77142        private function handle_order_save_by_admin( WC_Order $order ) {
    78             if ( ! in_array( $order->get_status(), [ 'processing', 'completed' ] ) ) {
    79                 return;
    80             }
    81143            $order_vouchers    = $order->get_items( 'coupon' );
    82144            $redeemed_vouchers = $order->get_meta( "_voucherify_redeemed_voucher", false );
     
    88150                }, $redeemed_vouchers ) );
    89151
     152            if ( empty( $new_codes ) ) {
     153                return;
     154            }
     155
     156            $rolled_back_vouchers_ids = $this::get_rolled_back_vouchers_ids( $order );
     157
    90158            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 );
    92160                $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         */
    96186        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 
    104187            $order_vouchers = $order->get_items( 'coupon' );
    105188            if ( ! empty( $order->get_meta( "_voucherify_redeemed_voucher", false ) ) || empty( $order_vouchers ) ) {
    106189                return;
    107190            }
    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            }
    111213        }
    112214
     
    124226         */
    125227        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                }
    127233                $message .= apply_filters( '',
    128234                    '<div class="woocommerce-info">' .
     
    132238                    $message, $order );
    133239            }
     240            $this->order_placing_session->clear();
    134241
    135242            return $message;
  • voucherify/trunk/src/class-voucherify-valid-promotions-fetcher.php

    r2361501 r2491101  
    5959         */
    6060        public function handle_ajax_vcrf_fetch_promotions() {
     61            define('VCRF_FETCH_PROMOTIONS', true);
    6162            echo json_encode( $this->get_valid_promotions() );
    6263            die();
  • voucherify/trunk/src/class-voucherify-validation-service.php

    r2361501 r2491101  
    1111}
    1212
     13use Voucherify\ClientException;
    1314use \Voucherify\Validations;
     15use \Voucherify\Promotions;
    1416
    1517if ( ! class_exists( 'Voucherify_Validation_Service' ) ) {
     
    3537        /** @var Validations $validations */
    3638        private $validations;
    37         /** @var \Voucherify\Promotions $promotions */
     39        /** @var Promotions $promotions */
    3840        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;
    3945
    4046        /**
     
    4248         *
    4349         * @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;
    4864        }
    4965
     
    5874         * @param string $coupon_code code to be validated.
    5975         *
    60          * @return string valid coupon data
    61          * @throws \Voucherify\ClientException
     76         * @return array|bool valid coupon data
     77         * @noinspection PhpUnusedParameterInspection
    6278         */
    6379        public function on_discount_code_added( $coupon, $coupon_code ) {
     
    6682            }
    6783
    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            }
    6999        }
    70100
     
    80110         * @param string $coupon_code Voucher or promotion code.
    81111         *
    82          * @return WC_Coupon valid virtual WC coupon
    83          * @throws \Voucherify\ClientException
     112         * @return array valid virtual WC coupon
     113         * @throws Exception
    84114         */
    85115        public function get_validated_virtual_coupon( $coupon_code ) {
    86116            $coupon_code_enhanced = explode( ":", $coupon_code );
    87117
     118            $is_promotion = false;
     119            $actual_code  = $coupon_code;
    88120            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 );
    93134        }
    94135
     
    98139         * Overrides default error message for invalid coupons.
    99140         *
     141         * @param $err mixed unused
     142         * @param $err_code
     143         * @param $coupon mixed unused
     144         *
    100145         * @return string custom voucherify error message
     146         * @noinspection PhpUnusedParameterInspection
    101147         */
    102148        public function invalid_error_message( $err, $err_code, $coupon ) {
     
    124170         * @param string $code coupon code as string
    125171         *
    126          * @return array coupon values.
    127          * @throws \Voucherify\ClientException
     172         * @return array|false coupon values.
     173         * @throws ClientException
    128174         */
    129175        public function get_valid_voucher_data( $code ) {
     
    140186            }
    141187
    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 
    147188            $response = apply_filters( 'voucherify_validation_service_validate',
    148                 $this->validations->validate( $code, $context ), $this->validations );
     189                $this->validate_voucher( $code ), $this->validations );
    149190
    150191            $response = apply_filters( 'voucherify_validation_service_validate_voucher', $response,
    151192                $this->validations );
    152193
    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;
    173303        }
    174304
     
    179309         *
    180310         * @return array coupon values.
    181          * @throws \Voucherify\ClientException
     311         * @throws ClientException
    182312         */
    183313        public function get_valid_promotion_data( $code ) {
     
    195325            }
    196326
    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 
    202327            $response = apply_filters( 'voucherify_validation_service_validate',
    203                 $this->promotions->validate( $context ), $this->validations );
     328                $this->validate_promotion( $code ), $this->validations );
    204329            $response = apply_filters( 'voucherify_validation_service_validate_promotion', $response,
    205330                $this->validations );
     
    209334                $return = apply_filters( 'voucherify_validation_service_validation_result', $return, $response,
    210335                    $this->validations );
     336                $this->order_placing_session->clear();
    211337
    212338                return apply_filters( 'voucherify_validation_service_promotion_validation_result', $return, $response,
     
    226352                $return = apply_filters( 'voucherify_validation_service_validation_result', $return, $response,
    227353                    $this->validations );
     354                $this->order_placing_session->clear();
    228355
    229356                return apply_filters( 'voucherify_validation_service_promotion_validation_result', $return, $response,
     
    231358            }
    232359
    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 = [
    234363                'code'           => $promo_found->id,
    235                 'amount'         => Voucherify_Tax::calc_applicable_amount( $promo_found->discount_amount ),
     364                'amount'         => Voucherify_Tax::calc_applicable_amount( $coupon_amount ),
    236365                'discount_type'  => 'fixed_cart',
    237366                'individual_use' => true,
    238367                'description'    => apply_filters( 'voucherify_promotion_description',
    239368                    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 );
    241376
    242377            return apply_filters( 'voucherify_validation_service_promotion_validation_result',
     
    245380        }
    246381
     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
    247435        public static function get_code_type( $code ) {
    248             if ( preg_match( '/^promotion\:/', $code ) ) {
     436            if ( preg_match( '/^promotion:/', $code ) ) {
    249437                return apply_filters( 'voucherify_get_code_type', 'promotion', $code );
    250438            }
    251439
    252440            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 = [];
    253451        }
    254452    }
  • voucherify/trunk/src/class-voucherify.php

    r1821613 r2491101  
    1212}
    1313
    14 use \Voucherify\ApiClient;
    15 use \Voucherify\VoucherifyClient;
    16 
    1714require_once "class-voucherify-admin-settings.php";
    1815require_once "class-voucherify-cart-decorator.php";
     
    2118require_once "class-voucherify-redemption-service.php";
    2219require_once "class-voucherify-save-order-listener.php";
     20require_once "class-voucherify-refund-order-listener.php";
    2321require_once "class-voucherify-promotion-form-renderer.php";
    2422require_once "class-voucherify-promotion-service.php";
     
    2826require_once "class-voucherify-messaging-modificator.php";
    2927require_once "class-voucherify-form-handler.php";
     28require_once "class-voucherify-order-placing-session.php";
     29require_once "class-voucherify-client-extension.php";
     30require_once "class-voucherify-remove-coupon-service.php";
     31require_once "class-voucherify-voucher-amount-calculator.php";
    3032
    3133if ( ! class_exists( 'Voucherify' ) ) {
     
    3436        /** @var Voucherify_Admin_Settings $settings */
    3537        private $settings;
     38        /** @var Voucherify_Order_Placing_Session $order_placing_session */
     39        private $order_placing_session;
    3640        /** @var Voucherify_Validation_Service $validataion_service */
    3741        private $validataion_service;
     
    4044        /** @var Voucherify_Save_Order_Listener $save_order_listener */
    4145        private $save_order_listener;
     46        /** @var Voucherify_Refund_Order_Listener $refund_order_listener */
     47        private $refund_order_listener;
    4248        /** @var Voucherify_Promotion_Form_Renderer $promotion_form_renderer */
    4349        private $promotion_form_renderer;
     
    5460        /** @var Voucherify_Form_Handler $form_handler */
    5561        private $form_handler;
     62        /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */
     63        private $remove_coupon_service;
    5664
    5765        /**
     
    6169            $this->settings = new Voucherify_Admin_Settings();
    6270
    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' ),
    6473                get_option( 'voucherify_app_secret_key' ) );
     74            $this->remove_coupon_service    = new Voucherify_Remove_Coupon_Service( $voucherify_client, $this->order_placing_session );
    6575            $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 );
    6879            $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 );
    7082            $this->promotion_service        = new Voucherify_Promotion_Service();
    7183            $this->all_promotions_fetcher   =
     
    97109                remove_filter( 'woocommerce_coupon_code', 'wc_strtolower' );
    98110
     111                add_action( 'wp_logout', [ $this->order_placing_session, 'clear' ] );
     112                add_action( 'wp_login', [ $this->order_placing_session, 'clear' ] );
     113
    99114                add_action( 'woocommerce_register_post_type_shop_coupon', [ $this, 'remove_coupons_menu_items' ] );
    100115                add_filter( 'woocommerce_admin_reports', [ $this, 'remove_coupons_report' ] );
     
    103118                    'invalid_error_message'
    104119                ], 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                ] );
    109127
    110128                add_action( 'woocommerce_before_order_object_save', [
     
    112130                    'on_before_order_save'
    113131                ], 10 );
    114                 add_filter( 'woocommerce_before_checkout_process', [
    115                     $this->save_order_listener,
    116                     'on_before_checkout_process'
    117                 ] );
    118132                add_filter( 'woocommerce_thankyou_order_received_text', [
    119133                    $this->save_order_listener,
     
    129143                    'handle_ajax_vcrf_fetch_promotions'
    130144                ], 10 );
     145
     146                add_action( 'woocommerce_cart_item_removed', [ $this, 'on_cart_item_removed' ], 10, 2 );
    131147
    132148                add_action( 'woocommerce_cart_coupon', [ $this->promotion_form_renderer, 'render_promotion_from' ] );
     
    163179
    164180                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 );
    165212            }
    166213        }
     
    175222        public function shut_down() {
    176223            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
    177227                add_filter( 'woocommerce_coupon_code', 'wc_strtolower' );
    178228                remove_action( 'woocommerce_register_post_type_shop_coupon', [ $this, 'remove_coupons_menu_items' ] );
     
    182232                    'invalid_error_message'
    183233                ], 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                ] );
    188241
    189242                remove_action( 'woocommerce_before_order_object_save', [
     
    191244                    'on_before_order_save'
    192245                ], 10 );
    193                 remove_filter( 'woocommerce_before_checkout_process', [
    194                     $this->save_order_listener,
    195                     'on_before_checkout_process'
    196                 ] );
    197246                remove_filter( 'woocommerce_thankyou_order_received_text', [
    198247                    $this->save_order_listener,
    199248                    'add_notice_on_thankyou_page'
    200249                ], 10 );
     250
    201251                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 );
    202254
    203255                remove_action( 'wp_ajax_vcrf_apply_promotion', [
     
    227279                remove_action( 'wp_loaded', [ $this->form_handler, 'handle' ], 25 );
    228280
     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
    229311                do_action( 'voucherify_shut_down' );
    230312            }
     
    243325         * of core coupons, including coupons reports.
    244326         *
    245          * @param $reports stores information about woocommerce reports
     327         * @param $reports mixed stores information about woocommerce reports
    246328         *
    247329         * @return mixed
     
    269351            return apply_filters( 'voucherify_remove_coupons_menu_items', $settings );
    270352        }
     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        }
    271383    }
    272384}
  • voucherify/trunk/src/functions.php

    r2361501 r2491101  
    4040 */
    4141function 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" ) );
    4444    return apply_filters( 'voucherify_validation_service_block_validation', $block_vouchers );
    4545}
     
    5151}
    5252
     53function 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
    5359if ( ! function_exists( 'vcrf_get_order_data' ) ) {
    5460    /**
    5561     * Collects information about order and prepares portion of context data
    5662     * to be consumed by API.
     63     *
     64     * @param WC_Order $order
    5765     *
    5866     * @return array order data in form of array accepted by API's context.
    5967     */
    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 ) ) ) {
    6271            $cart_decorator = new Voucherify_Cart_Decorator( WC()->cart );
    6372
     
    6574        }
    6675
    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        }
    7080
    7181        if ( empty( $order ) ) {
     
    7383        }
    7484
     85        /** @var WC_Order_Item_Product $order_items */
    7586        $order_items = $order->get_items();
    7687
     
    7889        foreach ( $order_items as $item ) {
    7990            $item_data = [
    80                 'product_id' => $item->get_id(),
     91                'product_id' => $item->get_product_id(),
    8192                'quantity'   => $item->get_quantity()
    8293            ];
     
    150161     */
    151162    function vcrf_get_customer_data() {
    152         if ( ! is_admin() ) {
     163        if ( ! is_admin() || ( defined( 'VCRF_FETCH_PROMOTIONS' ) && VCRF_FETCH_PROMOTIONS ) ) {
    153164            $customer_decorator = new Voucherify_Customer_Decorator( WC()->customer );
    154165
  • voucherify/trunk/voucherify.php

    r2361501 r2491101  
    88 * Plugin URI: https://wordpress.org/plugins/voucherify/
    99 * Description: Integrates Voucherify API with woocommerce replacing core coupons functionality
    10  * Version: 2.0.1
     10 * Version: 2.1.0
    1111 * Author: rspective
    1212 * Author URI: https://www.rspective.com/
     
    2626
    2727if ( ! function_exists( 'voucherify' ) ) {
     28    /**
     29     * @return Voucherify
     30     */
    2831    function voucherify() {
    2932        static $voucherify;
Note: See TracChangeset for help on using the changeset viewer.