PluginProbe ʕ •ᴥ•ʔ
ShopPress – Shop Builder for Elementor and WooCommerce / trunk
ShopPress – Shop Builder for Elementor and WooCommerce vtrunk
shop-press / Modules / SingleAjaxAddToCart.php
shop-press / Modules Last commit date
VariationSwatches 2 months ago Wishlist 2 months ago AdminMessage.php 2 months ago AjaxSearch.php 2 months ago Backorder.php 2 months ago CatalogMode.php 2 years ago Compare.php 2 months ago DefaultTemplates.php 2 years ago FlashSalesCountdown.php 1 year ago MenuCart.php 6 months ago MobilePanel.php 2 months ago MultiStep.php 1 year ago Notifications.php 2 months ago QuickView.php 2 months ago RecentlyViewedProducts.php 2 years ago RenameLabel.php 6 months ago Shopify.php 1 year ago SingleAjaxAddToCart.php 2 months ago SizeChart.php 2 months ago StickyAddToCart.php 2 months ago
SingleAjaxAddToCart.php
114 lines
1 <?php
2 /**
3 * Single Ajax Add To Cart.
4 *
5 * @package ShopPress
6 */
7
8 namespace ShopPress\Modules;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class SingleAjaxAddToCart {
13 /**
14 * Init.
15 *
16 * @since 1.2.0
17 */
18 public static function init() {
19 add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 99 );
20 add_action( 'woocommerce_before_add_to_cart_button', array( __CLASS__, 'add_fields_to_add_to_cart_form' ) );
21 add_action( 'wp_ajax_shoppress_single_add_to_cart_by_ajax', array( __CLASS__, 'add_to_cart_by_ajax' ), 99 );
22 add_action( 'wp_ajax_nopriv_shoppress_single_add_to_cart_by_ajax', array( __CLASS__, 'add_to_cart_by_ajax' ), 99 );
23 }
24
25 /**
26 * Enqueue scripts.
27 *
28 * @since 1.2.0
29 */
30 public static function enqueue_scripts() {
31
32 if ( ! is_product() ) {
33 return;
34 }
35
36 wp_enqueue_script( 'sp-single-ajax-add-to-cart' );
37 }
38
39 /**
40 * Add to cart by ajax
41 *
42 * @since 1.2.0
43 *
44 * @return void
45 */
46 public static function add_to_cart_by_ajax() {
47 check_ajax_referer( 'shoppress_nonce', 'nonce' );
48 $product_id = isset( $_POST['product_id'] ) ? absint( wp_unslash( $_POST['product_id'] ) ) : 0;
49 $variation_id = isset( $_POST['variation_id'] ) ? absint( wp_unslash( $_POST['variation_id'] ) ) : 0;
50 $p_id = $variation_id ? $variation_id : $product_id;
51 $quantity_raw = isset( $_POST['quantity'] ) ? wp_unslash( $_POST['quantity'] ) : 1; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Array for variable products
52 $quantity = is_array( $quantity_raw ) ? array_map( 'absint', $quantity_raw ) : absint( $quantity_raw );
53 if ( is_array( $quantity ) ) {
54 $products = $quantity;
55 } else {
56 $products = array( $p_id => $quantity );
57 }
58
59 $passed_validation = count( $products ) ? true : false;
60 foreach ( $products as $product_id => $quantity ) {
61
62 if ( $quantity ) {
63
64 $passed_validation = $passed_validation && apply_filters(
65 'woocommerce_add_to_cart_validation',
66 true,
67 $product_id,
68 $quantity
69 );
70 }
71 }
72
73 foreach ( $products as $product_id => $quantity ) {
74
75 $r = $passed_validation ? WC()->cart->add_to_cart( $product_id, $quantity ) : false;
76 if ( $r && is_string( $r ) && $quantity ) {
77 // wc_add_to_cart_message( array( $product_id => $quantity ), true );
78 }
79 }
80
81 $message_html = wc_print_notices( true );
82
83 ob_start();
84 woocommerce_mini_cart();
85 $mini_cart = ob_get_clean();
86
87 $data = array(
88 'fragments' => apply_filters(
89 'woocommerce_add_to_cart_fragments',
90 array(
91 'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>',
92 )
93 ),
94 'cart_hash' => WC()->cart->get_cart_hash(),
95 );
96 $data['message_html'] = $message_html;
97
98 wp_send_json( $data );
99 }
100
101 /**
102 * Add fields to add to cart form
103 *
104 * @since 1.2.0
105 *
106 * @return void
107 */
108 public static function add_fields_to_add_to_cart_form() {
109
110 echo '<input type="hidden" name="action" value="shoppress_single_add_to_cart_by_ajax">';
111 wp_nonce_field( 'shoppress_nonce', 'nonce', false );
112 }
113 }
114