PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.5.2
ShopBuilder – WooCommerce Builder For Elementor v2.5.2
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Controllers / Frontend / Ajax / AddToCart.php

AddToCart.php in ShopBuilder – WooCommerce Builder For Elementor 2.5.2, at app/Controllers/Frontend/Ajax/AddToCart.php

73 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Add to Cart Ajax Class.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Controllers\Frontend\Ajax;
9
10 use WC_AJAX;
11 use RadiusTheme\SB\Helpers\Fns;
12 use RadiusTheme\SB\Traits\SingletonTrait;
13
14 // Do not allow directly accessing this file.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 'This script cannot be accessed directly.' );
17 }
18
19 /**
20 * Add to Cart Ajax Class.
21 */
22 class AddToCart {
23 /**
24 * Singleton.
25 */
26 use SingletonTrait;
27
28 /**
29 * Class Constructor.
30 *
31 * @return void
32 */
33 private function __construct() {
34 \add_action( 'wp_ajax_rtsb_ajax_add_to_cart', [ $this, 'response' ] );
35 \add_action( 'wp_ajax_nopriv_rtsb_ajax_add_to_cart', [ $this, 'response' ] );
36 }
37
38 /**
39 * Ajax Response.
40 *
41 * @return void
42 */
43 public function response() {
44 if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) {
45 wp_send_json_error();
46 }
47
48 $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', ( isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : null ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
49 $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', absint( $_POST['quantity'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
50 $variation_id = isset( $_POST['variation_id'] ) ? absint( $_POST['variation_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing
51 $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
52
53 if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id ) ) {
54 do_action( 'woocommerce_ajax_added_to_cart', $product_id );
55 WC_AJAX::get_refreshed_fragments();
56 } else {
57 $data = [
58 'error' => true,
59 'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id ),
60 ];
61
62 wp_send_json( $data );
63 }
64
65 wp_send_json(
66 [
67 'error' => true,
68 'msg' => esc_html__( 'WooCommerce not installed', 'shopbuilder' ),
69 ]
70 );
71 }
72 }
73