This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwooajaxcart.php
82 lines (70 loc) · 2.94 KB
/
wooajaxcart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
Plugin Name: Woo AJAX Cart
Plugin URI: http://ragob.com/wooajaxcart
Description: Change the default behavior of WooCommerce Cart page, making AJAX requests when quantity field changes
Version: 1.0
Author: Moises Heberle
Author URI: http://codecanyon.net/user/moiseh
*/
// templates reference: cart.php , cart-totals.php
add_action('init', 'wac_init');
function wac_init() {
// force to make is_cart() returns true, to make right calculations on class-wc-cart.php (WC_Cart::calculate_totals())
// this define fix a bug that not show Shipping calculator when is WAC ajax request
if ( !empty($_POST['is_wac_ajax']) && !defined( 'WOOCOMMERCE_CART' ) ) {
define( 'WOOCOMMERCE_CART', true );
}
}
add_action('woocommerce_before_cart_contents', 'wac_cart_demo');
function wac_cart_demo() {
if ( defined('IS_DEMO')) {
wac_demo_msg('Change the product quantity to see the AJAX update made by WooAjaxCart plugin');
}
}
add_action('woocommerce_archive_description', 'wac_shop_demo');
function wac_shop_demo() {
if ( defined('IS_DEMO')) {
wac_demo_msg('Add some items to cart and go to the "Cart" page to see this plugin in action');
}
}
// on submit AJAX form of the cart
// after calculate cart form items
add_action('woocommerce_cart_updated', 'wac_update');
function wac_update() {
// is_wac_ajax: flag defined on wooajaxcart.js
if ( !empty($_POST['is_wac_ajax'])) {
$resp = array();
$resp['update_label'] = __( 'Update Cart', 'woocommerce' );
$resp['price'] = 0;
// render the cart totals (cart-totals.php)
ob_start();
do_action( 'woocommerce_after_cart_table' );
do_action( 'woocommerce_cart_collaterals' );
do_action( 'woocommerce_after_cart' );
$resp['html'] = ob_get_clean();
// calculate the item price
if ( !empty($_POST['cart_item_key']) ) {
$items = WC()->cart->get_cart();
$cart_item_key = $_POST['cart_item_key'];
if ( array_key_exists($cart_item_key, $items)) {
$cart_item = $items[$cart_item_key];
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$price = apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
$resp['price'] = $price;
}
}
echo json_encode($resp);
exit;
}
}
// on render cart table page
add_action('woocommerce_before_cart_table', 'wac_cart_table');
function wac_cart_table() {
// add js hacks script
wp_register_script('wooajaxcart', plugins_url('wooajaxcart.js', __FILE__));
wp_enqueue_script('wooajaxcart');
}
function wac_demo_msg($text) {
echo '<div style="background-color: lightgreen; padding: 5px; margin: 5px; border-radius: 3px">* '.$text.'</div>';
}