Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AnanthFlycart/e62c214918decd35810a336786a9c72b to your computer and use it in GitHub Desktop.

Select an option

Save AnanthFlycart/e62c214918decd35810a336786a9c72b to your computer and use it in GitHub Desktop.
CUW: Compatibility for Side Cart WooCommerce
add_action('wp_ajax_cuw_get_cart_subtotal', 'cuw_get_cart_subtotal');
add_action('wp_ajax_nopriv_cuw_get_cart_subtotal', 'cuw_get_cart_subtotal');
if (!function_exists('cuw_get_cart_subtotal')) {
function cuw_get_cart_subtotal() {
$subtotal = 0;
if (function_exists('WC') && isset(WC()->cart) && function_exists('wc_price')) {
WC()->cart->calculate_totals();
$subtotal = WC()->cart->get_subtotal();
if (get_option('woocommerce_tax_display_shop') === 'incl') {
$subtotal += WC()->cart->get_subtotal_tax();
}
}
wp_send_json_success([
'cart_subtotal' => wc_price($subtotal),
]);
}
}
add_action('wp_footer', function() {
if (function_exists('is_product') && is_product()) { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
if (jQuery(".cuw-modal .cuw-cart-subtotal").length > 0) {
jQuery(document).on('added_to_cart', function () {
jQuery.ajax({
type: 'post',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
async: true,
data: {
action: 'cuw_get_cart_subtotal',
},
success: function (response) {
jQuery(".cuw-modal .cuw-cart-subtotal").html(response.data.cart_subtotal);
},
});
});
}
});
</script>
<?php }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment