← All changes
|
includes/elementor/modules/checkout/classes/global-hooks.php
+115
-59
2.6.0
→
2.8.0
View file →
| @@ -28,15 +28,8 @@ | ||
| 28 | 28 | */ |
| 29 | 29 | private $helper_id = 0; |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | - * List of upsell product with price | |
| 33 | - * | |
| 34 | - * @var array | |
| 35 | - */ | |
| 36 | - private static $sellkit_upsell_products; | |
| 37 | - | |
| 38 | - /** | |
| 39 | 32 | * Create instance of class without construct. |
| 40 | 33 | * |
| 41 | 34 | * @since 1.1.0 |
| 42 | 35 | * @return object |
| @@ -1022,12 +1015,8 @@ | ||
| 1022 | 1015 | $discount_type = isset( $product_details['discountType'] ) ? $product_details['discountType'] : ''; |
| 1023 | 1016 | $discount_value = isset( $product_details['discount'] ) ? $product_details['discount'] : ''; |
| 1024 | 1017 | |
| 1025 | 1018 | $price = Helper::calculate_discount( $item_id, $discount_type, $discount_value ); |
| 1026 | - | |
| 1027 | - if ( 'upsell' === $source ) { | |
| 1028 | - self::$sellkit_upsell_products[ $product_id ] = $price; | |
| 1029 | - } | |
| 1030 | 1019 | } |
| 1031 | 1020 | } |
| 1032 | 1021 | |
| 1033 | 1022 | if ( false !== $price ) { |
| @@ -1176,69 +1165,35 @@ | ||
| 1176 | 1165 | if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 && wp_doing_ajax() ) { |
| 1177 | 1166 | return; |
| 1178 | 1167 | } |
| 1179 | 1168 | |
| 1180 | - // Gather checkout id. | |
| 1181 | - $checkout_id = get_queried_object_id(); | |
| 1169 | + $checkout_id = self::get_valid_checkout_id(); | |
| 1182 | 1170 | |
| 1183 | - if ( wp_doing_ajax() ) { | |
| 1184 | - // First try to catch id using our ajax call. | |
| 1185 | - $checkout_id = filter_input( INPUT_POST, 'related_checkout', FILTER_SANITIZE_NUMBER_INT ); | |
| 1186 | - | |
| 1187 | - // Second try to catch id using woocommerce ajax. | |
| 1188 | - if ( empty( $checkout_id ) ) { | |
| 1189 | - $checkout_id = filter_input( INPUT_POST, 'sellkit_current_page_id', FILTER_SANITIZE_NUMBER_INT ); | |
| 1190 | - } | |
| 1191 | - | |
| 1192 | - // Catch id after pressing place order button. none of above methods worked. | |
| 1193 | - if ( empty( $checkout_id ) ) { | |
| 1194 | - $data = filter_input( INPUT_POST, 'post_data', FILTER_DEFAULT ); | |
| 1195 | - | |
| 1196 | - if ( ! empty( $data ) ) { | |
| 1197 | - parse_str( $data, $data ); | |
| 1198 | - } | |
| 1199 | - | |
| 1200 | - if ( is_array( $data ) && array_key_exists( 'sellkit_current_page_id', $data ) ) { | |
| 1201 | - $checkout_id = $data['sellkit_current_page_id']; | |
| 1202 | - } | |
| 1203 | - } | |
| 1204 | - } | |
| 1205 | - | |
| 1206 | - // Empty id ? no action. | |
| 1207 | - if ( empty( $checkout_id ) ) { | |
| 1171 | + if ( 0 === $checkout_id ) { | |
| 1208 | 1172 | return; |
| 1209 | 1173 | } |
| 1210 | 1174 | |
| 1211 | - $checkout_id = (int) $checkout_id; | |
| 1212 | - | |
| 1213 | 1175 | // Apply funnel prices. |
| 1214 | 1176 | self::apply_discounted_prices( wc()->cart, $checkout_id ); |
| 1215 | 1177 | self::apply_discounted_prices( wc()->cart, $checkout_id, 'bumps' ); |
| 1216 | 1178 | |
| 1217 | 1179 | // Apply upsell discount. |
| 1218 | - $sellkit_upsell_prices = null; | |
| 1180 | + $sellkit_upsell_ids = null; | |
| 1219 | 1181 | if ( isset( $_POST['woocommerce-process-checkout-nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['woocommerce-process-checkout-nonce'] ) ), 'woocommerce-process_checkout' ) ) { |
| 1220 | - $sellkit_upsell_prices = isset( $_POST['sellkit_product_prices'] ) ? sanitize_textarea_field( wp_unslash( $_POST['sellkit_product_prices'] ) ) : null; | |
| 1182 | + $sellkit_upsell_ids = isset( $_POST['sellkit_upsell_ids'] ) ? sanitize_textarea_field( wp_unslash( $_POST['sellkit_upsell_ids'] ) ) : null; | |
| 1221 | 1183 | } |
| 1222 | - $upsell_product_ids; | |
| 1223 | 1184 | |
| 1224 | - if ( isset( $sellkit_upsell_prices ) && ! empty( $sellkit_upsell_prices ) ) { | |
| 1225 | - $sellkit_upsell_prices = json_decode( $sellkit_upsell_prices, true ); | |
| 1226 | - $upsell_product_ids = array_keys( $sellkit_upsell_prices ); | |
| 1227 | - } | |
| 1185 | + if ( ! empty( $sellkit_upsell_ids ) ) { | |
| 1186 | + $upsell_ids = explode( ',', $sellkit_upsell_ids ); | |
| 1187 | + $list = self::upsell_downsell_ids_of_funnel( $checkout_id ); | |
| 1228 | 1188 | |
| 1229 | - if ( isset( $upsell_product_ids ) ) { | |
| 1230 | - foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
| 1231 | - $product = $cart_item['data']; | |
| 1232 | - $product_name = $product->get_name(); | |
| 1233 | - $product_id = $product->get_id(); | |
| 1189 | + if ( ! empty( $list ) ) { | |
| 1190 | + foreach ( $upsell_ids as $id ) { | |
| 1234 | 1191 | |
| 1235 | - if ( in_array( $product_id, $upsell_product_ids, true ) ) { | |
| 1236 | - $upsell_price = $sellkit_upsell_prices[ $product_id ]; | |
| 1237 | - $product->set_price( $upsell_price ); | |
| 1192 | + if ( in_array( $id, $list, true ) ) { | |
| 1193 | + self::applay_upsell_downsell_discount( $id ); | |
| 1194 | + } | |
| 1238 | 1195 | } |
| 1239 | - | |
| 1240 | - $price = $product->get_price(); | |
| 1241 | 1196 | } |
| 1242 | 1197 | } |
| 1243 | 1198 | |
| 1244 | 1199 | $auto_apply_coupon_cookie_value = null; |
| @@ -1266,8 +1221,109 @@ | ||
| 1266 | 1221 | } |
| 1267 | 1222 | } |
| 1268 | 1223 | |
| 1269 | 1224 | /** |
| 1225 | + * Extract upsell and downsell ides from funnel. | |
| 1226 | + * | |
| 1227 | + * @since 2.8.0 | |
| 1228 | + * | |
| 1229 | + * @param int $checkout_id id of checkout. | |
| 1230 | + * | |
| 1231 | + * @return array | |
| 1232 | + */ | |
| 1233 | + private static function upsell_downsell_ids_of_funnel( $checkout_id ) { | |
| 1234 | + $step_data = get_post_meta( $checkout_id, 'step_data', true ); | |
| 1235 | + $funnel_id = $step_data['funnel_id']; | |
| 1236 | + $nodes = get_post_meta( $funnel_id, 'nodes', true ); | |
| 1237 | + $ids = []; | |
| 1238 | + | |
| 1239 | + foreach ( $nodes as $node ) { | |
| 1240 | + if ( isset( $node['type'] ) && in_array( $node['type']['key'], [ 'upsell', 'downsell' ], true ) ) { | |
| 1241 | + $ids[] = $node['page_id']; | |
| 1242 | + } | |
| 1243 | + } | |
| 1244 | + | |
| 1245 | + return $ids; | |
| 1246 | + } | |
| 1247 | + | |
| 1248 | + /** | |
| 1249 | + * Apply discount to upsell or downsell. | |
| 1250 | + * | |
| 1251 | + * @param int $id id of checkout. | |
| 1252 | + * | |
| 1253 | + * @since 2.8.0 | |
| 1254 | + */ | |
| 1255 | + private static function applay_upsell_downsell_discount( $id ) { | |
| 1256 | + $upsell_data = get_post_meta( $id, 'step_data', true ); | |
| 1257 | + $product = $upsell_data['data']['products']['list']; | |
| 1258 | + | |
| 1259 | + foreach ( $product as $key => $value ) { | |
| 1260 | + foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
| 1261 | + $product = $cart_item['data']; | |
| 1262 | + $product_id = $product->get_id(); | |
| 1263 | + | |
| 1264 | + if ( $key === $product_id ) { | |
| 1265 | + $discount_type = isset( $value['discountType'] ) ? $value['discountType'] : ''; | |
| 1266 | + $discount_value = isset( $value['discount'] ) ? $value['discount'] : ''; | |
| 1267 | + | |
| 1268 | + $price = Helper::calculate_discount( $product_id, $discount_type, $discount_value ); | |
| 1269 | + $product->set_price( $price ); | |
| 1270 | + } | |
| 1271 | + } | |
| 1272 | + } | |
| 1273 | + } | |
| 1274 | + | |
| 1275 | + /** | |
| 1276 | + * Get the checkout step associated with the current visitor. | |
| 1277 | + * | |
| 1278 | + * @since 2.8.0 | |
| 1279 | + * | |
| 1280 | + * @return int | |
| 1281 | + */ | |
| 1282 | + private static function get_valid_checkout_id() { | |
| 1283 | + $checkout_id = get_queried_object_id(); | |
| 1284 | + | |
| 1285 | + if ( wp_doing_ajax() ) { | |
| 1286 | + $checkout_id = filter_input( INPUT_POST, 'related_checkout', FILTER_SANITIZE_NUMBER_INT ); | |
| 1287 | + | |
| 1288 | + if ( empty( $checkout_id ) ) { | |
| 1289 | + $checkout_id = filter_input( INPUT_POST, 'sellkit_current_page_id', FILTER_SANITIZE_NUMBER_INT ); | |
| 1290 | + } | |
| 1291 | + | |
| 1292 | + if ( empty( $checkout_id ) ) { | |
| 1293 | + $data = filter_input( INPUT_POST, 'post_data', FILTER_DEFAULT ); | |
| 1294 | + | |
| 1295 | + if ( ! empty( $data ) ) { | |
| 1296 | + parse_str( $data, $data ); | |
| 1297 | + } | |
| 1298 | + | |
| 1299 | + if ( is_array( $data ) && array_key_exists( 'sellkit_current_page_id', $data ) ) { | |
| 1300 | + $checkout_id = $data['sellkit_current_page_id']; | |
| 1301 | + } | |
| 1302 | + } | |
| 1303 | + } | |
| 1304 | + | |
| 1305 | + $checkout_id = absint( $checkout_id ); | |
| 1306 | + $step_data = get_post_meta( $checkout_id, 'step_data', true ); | |
| 1307 | + | |
| 1308 | + if ( | |
| 1309 | + 0 === $checkout_id || | |
| 1310 | + 'publish' !== get_post_status( $checkout_id ) || | |
| 1311 | + ! is_array( $step_data ) || | |
| 1312 | + ! isset( $step_data['type']['key'] ) || | |
| 1313 | + 'checkout' !== $step_data['type']['key'] | |
| 1314 | + ) { | |
| 1315 | + return 0; | |
| 1316 | + } | |
| 1317 | + | |
| 1318 | + if ( wp_doing_ajax() && ! function_exists( 'WC' ) ) { | |
| 1319 | + return 0; | |
| 1320 | + } | |
| 1321 | + | |
| 1322 | + return $checkout_id; | |
| 1323 | + } | |
| 1324 | + | |
| 1325 | + /** | |
| 1270 | 1326 | * Check sellkit step through ajax. |
| 1271 | 1327 | * And in order decide to show upsell or downsell steps through a popup. |
| 1272 | 1328 | * |
| 1273 | 1329 | * @since 1.6.2 |
| @@ -1468,9 +1524,9 @@ | ||
| 1468 | 1524 | wp_send_json_success( |
| 1469 | 1525 | [ |
| 1470 | 1526 | 'next_id' => $funnel->end_node_step_data['page_id'], |
| 1471 | 1527 | 'next_type' => $funnel->end_node_step_data['type']['key'], |
| 1472 | - 'upsell_prices' => wp_json_encode( self::$sellkit_upsell_products ), | |
| 1528 | + 'upsell_ids' => $upsell_id, | |
| 1473 | 1529 | ] |
| 1474 | 1530 | ); |
| 1475 | 1531 | } |
| 1476 | 1532 | |
| @@ -1481,9 +1537,9 @@ | ||
| 1481 | 1537 | |
| 1482 | 1538 | $response = [ |
| 1483 | 1539 | 'next_id' => apply_filters( 'wpml_object_id', $next_step['page_id'], 'sellkit_step', true ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML API. |
| 1484 | 1540 | 'next_type' => $next_step['type']['key'], |
| 1485 | - 'upsell_prices' => wp_json_encode( self::$sellkit_upsell_products ), | |
| 1541 | + 'upsell_ids' => $upsell_id, | |
| 1486 | 1542 | ]; |
| 1487 | 1543 | |
| 1488 | 1544 | wp_send_json_success( $response ); |
| 1489 | 1545 | } |