| @@ -1,72 +1,72 @@ | ||
| 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 ( ! Fns::verify_nonce() ) { | |
| 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 | -} | |
| 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 ( ! Fns::verify_nonce() ) { | |
| 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 ) ); | |
| 49 | + $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', absint( $_POST['quantity'] ) ); | |
| 50 | + $variation_id = isset( $_POST['variation_id'] ) ? absint( $_POST['variation_id'] ) : 0; | |
| 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 | +} | |