php – Automated Cart Update With Alert Box Each Time
New To PHP and, I am currently attempting to create a plug in that creates an alert box every time the quantity in a cart is changed. So far I have successfully implemented updating the cart automatically, but I am having trouble determining how to create an alert box on update each time that references the new cart total. Any advice would be appreciated.
TLDR: Creating a plug in that will display an alert box when you change the quantity of an item in the cart with the following text:
“You just changed the quantity of ABC to 2”, where ABC is the SKU name, and 2 is the current (new) quantity of the item.
<?php
/**
* Plugin Name: Order Check 2
**/
add_action( 'wp_footer', 'cart_update_qty_script', 'persistent_cart_update');
function persistent_cart_update() {
foreach ( WC()->cart->get_cart() as $cart_item ) {
// gets the cart item quantity
$quantity = $cart_item['quantity'];}
}
function phpAlert($msg) {
echo '<script type="text/javascript">alert("' . $msg . '")</script>';
}
function cart_update_qty_script() {
if (is_cart()) :
?>
<script>
jQuery('div.woocommerce').on('change', '.qty', function(){
jQuery("[name="update_cart"]").prop("disabled", false);
jQuery("[name="update_cart"]").trigger("click");
});
</script>
<?php phpAlert( "Your have changed the quantity of sweatshirts to " .$quantity."."); ?>
<?php
endif;
}
?>
Leave an answer