PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.2.3
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.2.3
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / funnel / steps / upsell.php

upsell.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.2.3, at includes/funnel/steps/upsell.php

197 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Funnel\Steps;
4
5 use Sellkit\Funnel\Analytics\Data_Updater;
6 use Sellkit_Funnel;
7
8 defined( 'ABSPATH' ) || die();
9
10 /**
11 * Class Sellkit_Upsell.
12 *
13 * @since 1.1.0
14 */
15 class Upsell {
16 /**
17 * Upsell constructor.
18 *
19 * @since 1.1.0
20 */
21 public function __construct() {
22 $this->funnel = Sellkit_Funnel::get_instance();
23
24 add_action( 'wp_ajax_sellkit_upsell_operations', [ $this, 'upsell_handler' ] );
25 add_action( 'wp_ajax_nopriv_sellkit_upsell_operations', [ $this, 'upsell_handler' ] );
26 }
27
28 /**
29 * Ajax function to handle accepted offer actions.
30 *
31 * @since 1.1.0
32 * @SuppressWarnings(PHPMD.NPathComplexity)
33 */
34 public function upsell_handler() {
35 $order_key = sellkit_htmlspecialchars( INPUT_POST, 'order_key' );
36 $offer_type = sellkit_htmlspecialchars( INPUT_POST, 'offer_type' );
37
38 if ( empty( $order_key ) ) {
39 wp_send_json_error( __( 'Each upsell needs a main order.', 'sellkit' ) );
40 }
41
42 $order_id = wc_get_order_id_by_order_key( $order_key );
43 $main_order = new \WC_Order( $order_id );
44
45 if ( empty( $this->funnel->next_step_data['page_id'] ) ) {
46 wp_send_json_error( __( 'Please add thank you page step', 'sellkit' ) );
47 }
48
49 if ( 'reject' === $offer_type && ! empty( $this->funnel->next_step_data['page_id'] ) ) {
50 $next_step_link = add_query_arg( [ 'order-key' => $main_order->get_order_key() ], get_permalink( $this->funnel->next_step_data['page_id'] ) );
51
52 wp_send_json_success( $next_step_link );
53 }
54
55 $billing_address = $main_order->get_address();
56 $shipping_address = $main_order->get_address( 'shipping' );
57 $products = $this->funnel->current_step_data['data']['products']['list'];
58
59 $order = wc_create_order();
60
61 $order_total = 0;
62
63 foreach ( $products as $product_id => $product ) {
64 $product_object = wc_get_product( $product_id );
65 $regular_price = $product_object->get_regular_price();
66 $quantity = ! empty( $product['quantity'] ) ? $product['quantity'] : 1;
67 $discount = ! empty( $product['discount'] ) ? $product['discount'] : 0;
68 $sale_price = self::calculate_sale_price( $regular_price, $discount, $product['discountType'] );
69 $order_total = floatval( $order_total ) + intval( $quantity ) * floatval( $sale_price );
70
71 $product_object->set_price( $sale_price );
72 $order->add_product( $product_object, $quantity );
73 }
74
75 $order->set_address( $billing_address );
76 $order->set_address( $shipping_address, 'shipping' );
77 $order->set_payment_method( $main_order->get_payment_method() );
78 $order->set_total( $order_total );
79 $order->update_meta_data( 'sellkit_main_order_id', $main_order->get_id() );
80
81 if ( ! empty( $this->funnel->next_step_data ) ) {
82 $order->update_meta_data( 'sellkit_funnel_next_step_data', $this->funnel->next_step_data );
83 }
84
85 $order->save();
86
87 $analytics_updater = new Data_Updater();
88
89 // Adding order logs.
90 $analytics_updater->set_funnel_id( $this->funnel->funnel_id );
91 $analytics_updater->add_new_order_log( $order, 'upsell' );
92
93 $main_order->update_meta_data( 'sellkit_upsell_order_id', $order->get_id() );
94 $main_order->save();
95
96 $gateways = WC()->payment_gateways->payment_gateways();
97
98 if ( 'ppcp-gateway' !== $main_order->get_payment_method() ) {
99 $payment_process = $gateways[ $main_order->get_payment_method() ]->process_payment( $order->get_id() );
100 }
101
102 if ( 'ppcp-gateway' === $main_order->get_payment_method() ) {
103 $payment_process = self::ppcp_process_payment( $order->get_id() );
104 }
105
106 if ( 'success' === $payment_process['result'] && ! empty( $payment_process['redirect'] ) ) {
107 wp_send_json_success( $payment_process['redirect'] );
108 }
109
110 wp_send_json_error( __( 'Something went wrong', 'sellkit' ) );
111 }
112
113 /**
114 * Calculate the sale price.
115 *
116 * @param string $price Main price.
117 * @param string $discount Discount value.
118 * @param string $discount_type Discount type.
119 */
120 public static function calculate_sale_price( $price, $discount, $discount_type ) {
121 if ( $discount <= 0 ) {
122 return $price;
123 }
124
125 if ( 'fixed' === $discount_type ) {
126 return floatval( $price ) - floatval( $discount );
127 }
128
129 if ( 'percentage' === $discount_type ) {
130 return floatval( $price ) - ( ( floatval( $price ) * floatval( $discount ) ) / 100 );
131 }
132 }
133
134 /**
135 * Upsell actions.
136 *
137 * @since 1.1.0
138 * @SuppressWarnings(PHPMD.NPathComplexity)
139 */
140 public function do_actions() {
141 $conditions = ! empty( $this->funnel->current_step_data['data']['conditions'] ) ? $this->funnel->current_step_data['data']['conditions'] : [];
142 $is_valid = true;
143 $condition_type = ! empty( $condition[1]['type'] ) ? $condition[1]['type'] : 'and';
144
145 if ( 'or' === $condition_type ) {
146 $is_valid = false;
147 }
148
149 foreach ( $conditions as $condition ) {
150 if ( is_array( $condition['condition_value'] ) ) {
151 $condition['condition_value'] = sellkit_get_multi_select_values( $condition['condition_value'] );
152 }
153
154 $result = sellkit_condition_match( $condition['condition_subject'], $condition['condition_operator'], $condition['condition_value'] );
155
156 if ( is_wp_error( $result ) ) {
157 continue;
158 }
159
160 if ( ! $result ) {
161 $is_valid = false;
162 }
163
164 if ( $result && 'or' === $condition_type ) {
165 $is_valid = true;
166 break;
167 }
168 }
169
170 if ( ! $is_valid ) {
171 $args = [];
172
173 if ( ! empty( $_GET['order-key'] ) ) { // phpcs:ignore
174 $args['order-key'] = $_GET['order-key']; // phpcs:ignore
175 }
176
177 wp_safe_redirect( add_query_arg( $args, get_permalink( $this->funnel->next_step_data['page_id'] ) ) );
178 }
179 }
180
181 /**
182 * Process the payment and return the result.
183 *
184 * @param int $order_id Order ID.
185 * @since 1.1.0
186 */
187 public static function ppcp_process_payment( $order_id ) {
188 if ( ! class_exists( 'WC_Gateway_Paypal' ) ) {
189 return false;
190 }
191
192 $paypal = new \WC_Gateway_Paypal();
193
194 return $paypal->process_payment( $order_id );
195 }
196 }
197