| 1 |
<?php |
| 2 |
/** |
| 3 |
* Merchant_Ajax_Callbacks Class. |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
if ( ! class_exists( 'Merchant_Ajax_Callbacks' ) ) { |
| 11 |
|
| 12 |
class Merchant_Ajax_Callbacks { |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor. |
| 16 |
* |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
|
| 20 |
// Custom Add To Cart Button. |
| 21 |
add_action( 'wp_ajax_merchant_custom_addtocart', array( $this, 'custom_addtocart_button' ) ); |
| 22 |
add_action( 'wp_ajax_nopriv_merchant_custom_addtocart', array( $this, 'custom_addtocart_button' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Custom add to cart button callback. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public function custom_addtocart_button() { |
| 30 |
check_ajax_referer( 'merchant-custom-addtocart-nonce', 'nonce' ); |
| 31 |
|
| 32 |
if ( ! isset( $_POST['product_id'] ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
WC()->cart->add_to_cart( absint( $_POST['product_id'] ) ); |
| 37 |
|
| 38 |
wp_die(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
new Merchant_Ajax_Callbacks(); |
| 43 |
|
| 44 |
} |
| 45 |
|