PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.2
ShopBuilder – WooCommerce Builder For Elementor v3.2.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 3.2.2, at app/Controllers/Frontend/Ajax/AddToCart.php

81 lines 2.4 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 // Safe fetch of the variation array.
53 $raw_variation = filter_input( INPUT_POST, 'variation', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
54 $variation = [];
55 // Build variation attributes safely.
56 if ( ! empty( $raw_variation ) && is_array( $raw_variation ) ) {
57 foreach ( $raw_variation as $key => $value ) {
58 $variation[ $key ] = wc_clean( $value );
59 }
60 }
61 if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ) ) {
62 do_action( 'woocommerce_ajax_added_to_cart', $product_id );
63 WC_AJAX::get_refreshed_fragments();
64 } else {
65 $data = [
66 'error' => true,
67 'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id ),
68 ];
69
70 wp_send_json( $data );
71 }
72
73 wp_send_json(
74 [
75 'error' => true,
76 'msg' => esc_html__( 'WooCommerce not installed', 'shopbuilder' ),
77 ]
78 );
79 }
80 }
81