'%%endpoint%%', ); } else { $query_args = array( 'wc-ajax' => $request, ); } $uri = explode( '?', esc_url_raw( $_SERVER['REQUEST_URI'] ), 2 ); $url = esc_url( $uri[0] ); } $query_args['wcf_checkout_id'] = $post->ID; $endpoint_url = add_query_arg( $query_args, $url ); } } return $endpoint_url; } /** * Apply coupon on submit of custom coupon form. */ public function apply_coupon() { $response = ''; if ( ! check_ajax_referer( 'wcf-apply-coupon', 'security', false ) ) { $response_data = array( 'status' => false, 'error' => __( 'Nonce validation failed', 'cartflows' ), ); wp_send_json_error( $response_data ); } ob_start(); if ( ! empty( $_POST['coupon_code'] ) ) { $result = WC()->cart->add_discount( sanitize_text_field( wp_unslash( $_POST['coupon_code'] ) ) ); } else { wc_add_notice( WC_Coupon::get_generic_coupon_error( WC_Coupon::E_WC_COUPON_PLEASE_ENTER ), 'error' ); } $response = array( 'status' => $result, 'msg' => wc_print_notices( true ), ); ob_clean(); // Clearing the uncessary echo HTML. wp_send_json( $response ); die(); } /** * Remove coupon. */ public function remove_coupon() { check_ajax_referer( 'wcf-remove-coupon', 'security' ); $coupon = isset( $_POST['coupon_code'] ) ? sanitize_text_field( wp_unslash( $_POST['coupon_code'] ) ) : false; if ( empty( $coupon ) ) { echo "
" . esc_html__( 'Sorry there was a problem removing this coupon.', 'cartflows' ); } else { WC()->cart->remove_coupon( $coupon ); echo "
" . esc_html__( 'Coupon has been removed.', 'cartflows' ) . '
'; } wc_print_notices(); wp_die(); } /** * Remove cart item. */ public function wcf_woo_remove_cart_product() { check_ajax_referer( 'wcf-remove-cart-product', 'security' ); $product_key = isset( $_POST['p_key'] ) ? sanitize_text_field( wp_unslash( $_POST['p_key'] ) ) : false; $product_id = isset( $_POST['p_id'] ) ? sanitize_text_field( wp_unslash( $_POST['p_id'] ) ) : ''; $product_title = get_the_title( $product_id ); $needs_shipping = false; if ( empty( $product_key ) ) { $msg = "
" . __( 'Sorry there was a problem removing ', 'cartflows' ) . $product_title; } else { WC()->cart->remove_cart_item( $product_key ); $msg = "
" . $product_title . __( ' has been removed.', 'cartflows' ) . '
'; } foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { if ( $values['data']->needs_shipping() ) { $needs_shipping = true; break; } } $response = array( 'need_shipping' => $needs_shipping, 'msg' => $msg, ); echo wp_json_encode( $response ); wp_die(); } /** * Check email exist. */ public function check_email_exists() { check_ajax_referer( 'check-email-exist', 'security' ); $email_address = isset( $_POST['email_address'] ) ? sanitize_email( wp_unslash( $_POST['email_address'] ) ) : false; $is_exist = email_exists( $email_address ); $response = array( 'success' => boolval( $is_exist ), 'is_login_allowed' => 'yes' === get_option( 'woocommerce_enable_checkout_login_reminder' ), 'msg' => $is_exist ? __( 'Email Exist.', 'cartflows' ) : __( 'Email not exist', 'cartflows' ), ); wp_send_json_success( $response ); } /** * Check email exist. */ public function woocommerce_user_login() { check_ajax_referer( 'woocommerce-login', 'security' ); $response = array( 'success' => false, ); $email_address = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : false; $password = isset( $_POST['password'] ) ? wp_unslash( $_POST['password'] ) : false; // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $creds = array( 'user_login' => $email_address, 'user_password' => $password, 'remember' => false, ); $user = wp_signon( $creds, false ); if ( ! is_wp_error( $user ) ) { $response = array( 'success' => true, ); } else { $response['error'] = wp_kses_post( $user->get_error_message() ); } wp_send_json_success( $response ); } } /** * Kicking this off by calling 'get_instance()' method */ Cartflows_Checkout_Ajax::get_instance();