PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.6.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.6.2
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.6.2, at includes/funnel/steps/upsell.php

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