I am trying to fire this script when the user updates cart quantity on my WordPress Cart page.
The script needs to have updated values for total, subtotal etc., whenever there is an update made on the Cart page. My idea was to use a WooCommerce action hook to fire the script after the cart product quantity has changed, so I can grab the latest, most up-to-date price. Below I am only testing one value, the grandTotal
.
To give more context, there is an Update Cart button on the cart for when someone uses the arrows to increase or decrease the quantity, I need this to happen when someone hits the Update Cart button. I do not need the price to dynamically update when the quantity is changed. It should be done only once the Update Cart button has been clicked.
This does not work. Currently, with the below code, the Update Cart button breaks and does not update the quantity at all. The function also does not fire and nothing happens.
function updateCartPrice( $cart_item_key, $quantity, $old_quantity ) {
if ( is_cart() ) { ?>
<script type="text/javascript">
bronto('cart:send', {
"customerCartId": "cart_ABC123", // Set for testing. This value will be auto-generated when not set.
"phase": "ORDER_COMPLETE", // Set as SHOPPING for Cart Recovery
"currency": "USD",
"subtotal": 35.98,
"grandTotal": <?php echo WC()->cart->get_total() ?>,
"customerOrderId": "123ABC", // Not necessary for Cart Recovery
"emailAddress": "example@example.com",
});
console.log('test log');
</script>
<?php
}
}
// add the action
add_action( 'woocommerce_after_cart_item_quantity_update', 'updateCartPrice' );